Java Reference
In-Depth Information
Display 5.7
Using
Math.PI
1
import
java.util.Scanner;
2
/**
3
Class with static methods for circles and spheres.
4
*/
5
public class
RoundStuff3
6{
7
/**
8
Return the area of a circle of the given radius.
9
*/
10
public static double
area(
double
radius)
11
{
12
return
(Math.PI*radius*radius);
13
}
14
15
/**
16
Return the volume of a sphere of the given radius.
17
*/
18
public static double
volume(
double
radius)
19
{
20
return
((4.0/3.0)*Math.PI*radius*radius*radius);
21
}
22
public static void
main(String[] args)
23
{
24
Scanner keyboard =
new
Scanner(System.in);
25
System.out.println("Enter radius:");
26
double
radius = keyboard.nextDouble();
27
System.out.println("A
circle
of
radius "
28
29
+ radius + " inches");
30
System.out.println("has an area of " +
31
RoundStuff.area(radius) + " square inches.");
System.out.println("A
sphere
of
radius "
32
33
+ radius + " inches");
34
System.out.println("has a volume of " +
35
RoundStuff.volume(radius) + " cubic inches.");
36
}
The dialogue is the same as in
Display 5.1.
37
}
38
39