Class Exercise
Operators
& Expressions Exercise
|
This
exercise should be carried out after reading
the
tutorial on Operators
& Expressions
Exercise
1: Sphere Application
The
formula for calculating the volume of a sphere is given by...
V
= 4¸3
p
r3
Where,
V
is the volume, r
is the radius and p
is
pi, which you can approximate as 3.141.
The
formula for calculating the surface area of a sphere is given by...
A
= 4 p
r2
Where,
V
is the volume, r
is the radius and p
is
pi, which again you can approximate as 3.141.
-
Write
Java code that prints out to the command prompt windows the volume and
surface area of a sphere. Your code should use variables such as V,
PI (for p)
and r
and declare them using appropriate data types.
-
Check
that your program works correctly by getting it to calculate the volume and
surface area of a sphere with a radius of 8cm. You should get an
answer of approximately 2150 for the volume and 804 for the surface are.
Have
fun!
|