Java Reference
In-Depth Information
Display 5.1
Static Methods (part 1 of 2)
1 /**
2 Class with static methods for circles and spheres.
3 */
4 public class RoundStuff
5 {
6 public static final double PI = 3.14159;
7
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 }
This is the file
RoundStuff.java.
1 import java.util.Scanner;
This is the file
RoundStuffDemo.java.
2 public class RoundStuffDemo
3 {
4 public static void main(String[] args)
5 {
6 Scanner keyboard = new Scanner(System.in);
7 System.out.println("Enter radius:");
8 double radius = keyboard.nextDouble();
9 System.out.println("A circle of radius"
10 + radius + "inches");
11 System.out.println("has an area of " +
12 RoundStuff.area(radius) + " square inches.");
13 System.out.println("A sphere of radius"
14 + radius + "inches");
15 System.out.println("has an volume of " +
16 RoundStuff.volume(radius) + "cubic inches.");
17 }
18 }
Search WWH ::




Custom Search