Back to 111 Main Page

Mobius strip

Assignment 2

Task

Print the requested details of a sphere to the screen.

Textbook: 2.4 - 2.5
New concepts: operators (again), expressions, Math methods and constants.

Steps

Write a program that starts with a radius and prints out the following details for a sphere:

  • Diameter (2r)
  • Circumference (πd)
  • Surface area (4πr2)
  • Volume (4/3 * πr3)

Use only double variables (not ints). For π, use the pre-defined Java constant Math.PI. You may create additional variables as needed, however all output/results should change appropriately when only the initial value of the radius is changed.

Print out the radius and the result of each of the above calculations. Print one result per line. Be sure to label what each value is in your output. Thus, your output should be formatted something like this:

For a sphere with radius 10.0:
Diameter = 20.0
Circumference = 62.83185307179586
Surface area = 1256.6370614359173
Volume = 4188.790204786391

When you're done, submit your code with the radius initialized to 5.0.

What to Submit

Upload your UsernameA02.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Variables
You use a variable, not a literal, for the radius in all your calculations and printing. (0.5) You display the value of the radius before you do the rest of the calculations. (0.5) Results should still be correct if only the value of radius variable is changed.
2 - Calculations
Program displays the correct results of the 4 additional calculations (with r=5.0), properly labelled, one per line. (0.5 each)

FAQs

So wait--the program I'm submitting is not going to have the same output as shown above?
Correct. The example above shows out your output should be formatted--each number labelled as to what it is, with one result printed per line. The actual numbers shown, however, will be different for your submission. All the details above are for a sphere of radius 10.0. When you change your code to initialize your radius variable to 5.0 instead, all of your calculation/results should change accordingly.

The reason I'm having you make this change is just as a check for you to make sure that your output really relies only on the radius variable, and not on a few literal 10.0s accidentally scattered here and there throughout your code. That's the whole point of a variable: change the value in the variable, and your program's behavior/output changes accordingly (hopefully correctly).

How do I do powers, such as r3?
There is a way to do it using the Math.pow() function, such as in int cubeVolume = Math.pow(side, 3). However, it's not really necessary for this assignment. You can just compute it "manually": radius * radius * radius.
My volume calculation isn't coming out correctly!
Be careful of integer math. Remember that System.out.println(4 / 3); prints out 1, not 1.3333333. So you want to use double (floating point) literals, rather than int (integer) literals here.
How do I do the square root (√) again?
For the curious (since you don't need it for this assignment): Use Math.sqrt(). So, to take the square root of 9, you could do something like this:
  double answer = Math.sqrt(9);
You could also replace the literal 9 with a variable or a longer expression.


~ztomasze Index : TA Details: ICS111: A02
http://www2.hawaii.edu/~ztomasze
Last Edited: 23 Jan 2009
©2008 by Z. Tomaszewski.