Print the requested details of a sphere to the screen.
Textbook: 2.4 - 2.5; 3.5
New concepts: operators (again), expressions; Math
methods and constants.
Write a program that starts with a radius and prints out the following details for a sphere:
Use only double
variables (not int
s). 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 variable is changed. So, as in A01a, the value assigned to your radius variable will occur only once in your code.
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.
Upload your UsernameA02.java
file to Tamarin.
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 literals 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).
Math.PI
and it will give you a very precise version of π. So, for example:
double circumference = 2 * Math.PI * radius;where
radius
is a variable you already declared and initialized before this line.
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" if you want: side * side * side
.
System.out.println(1 / 3);
prints out 0
, not
0.3333333
. So you want to use double (floating
point) literals, rather than int (integer) literals here.