Java Reference
In-Depth Information
Directory "Create Spheres 2"
The statements in the default constructor that set three fields to zero are not really necessary, as the fields
are set to zero by default. They are there just to emphasize that the primary purpose of a constructor is to
enable you to set initial values for the fields.
If you add the following statements to the CreateSpheres class, you can test out the new constructors:
public class CreateSpheres {
public static void main(String[] args) {
System.out.println("Number of objects = " + Sphere.getCount());
Sphere ball = new Sphere(4.0, 0.0, 0.0, 0.0); //
Create a sphere
System.out.println("Number of objects = " + ball.getCount());
Sphere globe = new Sphere(12.0, 1.0, 1.0, 1.0); //
Create a sphere
System.out.println("Number of objects = " + Sphere.getCount());
Sphere eightBall = new Sphere(10.0, 10.0, 0.0);
Sphere oddBall = new Sphere();
System.out.println("Number of objects = " + Sphere.getCount());
// Output the volume of each sphere
System.out.println("ball volume = " + ball.volume());
System.out.println("globe volume = " + globe.volume());
System.out.println("eightBall volume = " + eightBall.volume());
System.out.println("oddBall volume = " + oddBall.volume());
}
}
Directory "Create Spheres 2"
Now the program should produce the following output:
Number of objects = 0
Number of objects = 1
Number of objects = 2
Number of objects = 4
ball volume = 267.94666666666666
globe volume = 7234.559999999999
eightBall volume = 4.1866666666666665
oddBall volume = 4.1866666666666665
How It Works
Search WWH ::




Custom Search