Java Reference
In-Depth Information
Both files will need to be in the same directory or folder - I suggest you name the directory
CreateSpheres . Then copy or move the last version of Sphere.java to this directory.
Try It Out - Using the Sphere Class
Enter the following code for the file CreateSpheres.java :
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());
// Output the volume of each sphere
System.out.println("ball volume = " + ball.volume());
System.out.println("globe volume = " + globe.volume());
}
}
Compile the source files and then run CreateSpheres , and you should get the output:
Number of objects = 0
Number of objects = 1
Number of objects = 2
ball volume = 267.94666666666666
globe volume = 7234.559999999999
This is the first time we have run a program involving two source files. If you are using the JDK
compiler, then compile CreateSpheres.java with the current directory as CreateSpheres using
the command:
javac CreateSpheres.java
The compiler will find and compile the Sphere.java source file automatically. If all the source files
for a program are in the current directory, then compiling the file containing a definition of main()
will compile all the source files for the program.
Note that by default, the .class files generated by the compiler will be stored in the current directory,
that is, the directory containing your sourcecode. If you want the .class files stored in a different
directory, then you can use the -d option with the Java compiler to specify where they should go. For
example, to store the class files in a directory called C:\classes you would type:
javac -d C:/classes CreateSpheres.java
Search WWH ::




Custom Search