Java Reference
In-Depth Information
// Output the total length
System.out.println("\nTotal line length = " + totalLength);
}
}
Directory "TryPackage"
You should save this as TryPackage.java in the directory TryPackage . If the path to your Geometry
directory on a PC running Windows is C:\Packages\Geometry , you can compile this with the following
command:
javac -classpath "C:\Packages" TryPackage.java
This assumes the current directory is the one containing the TryPackage.java file, which is the
TryPackage directory if you followed my suggestion. The -classpath option specifies the directory
containing your Geometry package. Without this the compiler is not able to find the classes in the Geo-
metry package, and the compilation fails.
After you have a successful compilation, you can execute the program with the following command:
java -classpath ".;C:\Packages" TryPackage
Here -classpath includes a period for the current directory. Without it, the program will not execute be-
cause TryPackage will not be found. When the program executes, you should see the following output:
Line 1 (1.0, 0.0):(6.0, 0.0) Length is 5.0
Line 2 (6.0, 0.0):(6.0, 10.0) Length is 10.0
Line 3 (6.0, 10.0):(10.0, 10.0) Length is 4.0
Line 4 (10.0, 10.0):(10.0, -14.0) Length is 24.0
Line 5 (10.0, -14.0):(8.0, -14.0) Length is 2.0
Total line length = 45.0
How It Works
This example is a handy review of how you can define arrays and also shows that you can declare an
array of objects in the same way as you declare an array of one of the basic types. The dimensions of
the array of arrays, coords , are determined by the initial values that you specified between the braces.
The number of values within the outer braces determines the first dimension. Each of the elements in the
array is itself an array of length two, with each pair of element values enclosed within their own braces.
Because there are six sets of these, you have an array of six elements, each of which is itself an array of
two elements. Each of these elements corresponds to the ( x,y ) coordinates of a point.
NOTE Youcanseefromthisthatyoucouldcreateanarrayofarrayswitheachrow
havingadifferentnumberofelements.Thenumberofinitializingvaluesthatappear
Search WWH ::




Custom Search