Listing 8 . Beginning of the class named PointLine02. |
---|
class PointLine02{
public static void main(String[]args){
System.out.println("Instantiate and display the contents\n"
+ "of a new ColMatrix object");GM2D01.ColMatrix colMatrix =
new GM2D01.ColMatrix(2.5,6.8);System.out.println(colMatrix);
try{System.out.println(colMatrix.getData(0));
System.out.println(colMatrix.getData(1));//This statement will throw an exception on purposeSystem.out.println(colMatrix.getData(2));
}catch(Exception e){System.out.println("Bad index");
}//end catch |
The GM2D01.ColMatrix class
You learned about a column matrix in the Kjell tutorial. The GM2D01 class contains a class named ColMatrix . (You will see the code for that class definition later.) An object of the ColMatrix class is intended to represent a column matrix as described by Kjell.
The code in Listing 8 instantiates and displays the contents of a new object of the ColMatrix class. (Note the syntax required to instantiate an object of a static top-level class belonging to another class as shown in Listing 8 .)
After instantiating the object, the remaining statements in Listing 8 display the numeric contents of the ColMatrix object using two different approaches.
The overridden toString method
The first approach causes the overridden toString method belonging to the ColMatrix class to be executed.
(The overridden toString method is executed automatically by the call to the System.out.println method, passing the object's reference as a parameter to the println method.)
The overridden toString method returns a string that is displayed on the standard output device. That stringcontains the values of the two real numbers stored in the column matrix.
The getData method
The second approach used to display the data in Listing 8 calls the getData method on the ColMatrix object twice in succession to get the two numeric values stored in the object and to display those two values on thestandard output device.
As you will see shortly, the getData method requires an incoming index value of either 0 or 1 to identify the numeric value that is to be returned.
Listing 8 purposely calls the getData method with an index value of 2 to demonstrate that this will cause the method to throw an IndexOutOfBoundsException .
(The text output produced by the code in Listing 8 is shown near the top of Figure 5 .)
Beginning of the class named GM2D01
Listing 9 shows the beginning of the library class named GM2D01 , including the entire static top-level class named ColMatrix .
Listing 9 . Beginning of the class named GM2D01. |
---|
public class GM2D01{
public static class ColMatrix{double[] data = new double[2];ColMatrix(double data0,double data1){//constructor
data[0]= data0;
data[1]= data1;
}//end constructorpublic String toString(){return data[0] + "," + data[1];
}//end overridden toString methodpublic double getData(int index){if((index<0) || (index>1))
throw new IndexOutOfBoundsException();return data[index];}//end getData}//end class ColMatrix |