WebReference.com - Part 4 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (8/8)

To page 1To page 2To page 3To page 4To page 5To page 6To page 7current page
[previous]

Beginning Java 2 SDK 1.4 Edition

Try It Out--Testing the Geometry Package

We can create a succession of points, and a line joining each pair of successive points in the sequence, and then calculate the total line length.

import Geometry.*;    // Import the Point and Line classes
public class TryPackage {
  public static void main(String[] args) {
    double[][] coords = { {1.0, 0.0}, {6.0, 0.0}, {6.0, 10.0},
                          {10.0,10.0}, {10.0, -14.0}, {8.0, -14.0}};
    // Create an array of points and fill it with Point objects
    Point[] points = new Point[coords.length]; 
    for(int i = 0; i < coords.length; i++)
      points[i] = new Point(coords[i][0],coords[i][1]);
    // Create an array of lines and fill it using Point pairs
    Line[] lines = new Line[points.length--1]; 
    double totalLength = 0.0;           // Store total line length here
    for(int i = 0; i < points.length--1; i++) {
      lines[i] = new Line(points[i], points[i+1]); // Create a Line
      totalLength += lines[i].length();            // Add its length
      System.out.println("Line "+(i+1)+' ' +lines[i] + 
                         "  Length is " + lines[i].length());
    }
 
    // Output the total length
    System.out.println("\nTotal line length = " + totalLength);
  }
}

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 command:

javac –classpath ".;C:\Packages" TryPackage.java

This assumes the current directory contains the TryPackage.java file. The -classpath option specifies two paths separated by a semi-colon. The first path, specified by a period, is the current directory. This is necessary to enable the TryPackage.java source file to be found. The second path is C:\Packages, which is the directory containing our Geometry package. Without this the compiler will not be able to find the classes in the Geometry package and the compilation will fail.

Once you have a successful compilation, you can execute the program with the command:

java –classpath ".;C:\Packages" TryPackage

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 are 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 being enclosed within their own braces.

Since there are six sets of these, we have an array of six elements, each of which is itself an array of two elements. Each of these elements correspond to the (x, y) coordinates of a point.

You can see from this that, if necessary, you can create an array of arrays with each row having a different number of elements. The number of initializing values that appear, so they could all be different in the most general case, determines the length of each row.

We declare an array of Point objects with the same length as the number of (x, y) pairs in the coords array. This array is filled with Point objects in the for loop, which we created using the pairs of coordinate values from the coords array.

Since each pair of Point objects will define a Line object, we need one less element in the array lines than we have in the points array. We create the elements of the lines array in the second for loop using successive Point objects, and accumulate the total length of all the line segments by adding the length of each Line object to totalLength as it is created. On each iteration of the for loop, we output the details of the current line. Finally, we output the value of totalLength, which in this case is 45.

Note that the import statement adds the classes from the package Geometry to our program. These classes can be added to any application using the same import statement. You might like to try putting the classes in the Geometry package in a JAR file and try it out as an extension. Let's look at one other aspect of generating your own packages--compiling just the classes in the package without any program that makes use of them. We can demonstrate how this can be done on our Geometry package if you delete the Line.class and Point.class files from the package directory.

First make the directory, C:\TryPackage, that contains the package directory, current. Now you can compile just the classes in the Geometry package with the command:

javac -classpath "C:\TryPackage" Geometry/*.java

This will compile both the Line and Point classes so you should see the .class files restored in the Geometry directory. The files to be compiled are specified relative to the current directory as Geometry/*.java. Under Microsoft Windows this could equally well be Geometry\*.java. This specifies all files in the Geometry subdirectory to the current directory. The classpath must contain the path to the package directory, otherwise the compiler will not be able to find the package. We have defined it here using the -classpath option. We have not specified the current directory in classpath since we do not have any files there that need to be compiled. If we had included it in classpath it would not have made any difference--the classes in the Geometry package would compile just the same.


To page 1To page 2To page 3To page 4To page 5To page 6To page 7current page
[previous]

Created: July 29, 2002
Revised: July 29, 2002


URL: http://webreference.com/programming/java/beginning/chap5/4/8.html