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