<< Chapter < Page Chapter >> Page >

The GM2D01.ColMatrix class

As mentioned earlier, an object of the ColMatrix class represents a 2D column matrix as described by Kjell. Furthermore, an object of thisclass is the fundamental building block for several of the other classes in the library.

(The static ColMatrix class will be expanded in future modules to provide various matrix arithmetic capabilities.)

Constructor for the ColMatrix class

The constructor for the class requires two incoming parameters of type double .

(For this example, the code in Listing 8 passes the values 2.5 and 6.8 to the constructor for the class.)

The two incoming parameter values are stored in the first two elements of a two-element array of type double where they can be easily accessed later for whatever purpose they may be needed.

Overridden toString method

Listing 9 also overrides the toString method to construct and return a reference to a String object containing the two values stored in the array.

When the println method is called for the second time, (near the middle of Listing 8 ), the overridden toString method is called automatically and the output shown in the third line of text in Figure 5 is produced.

The getData method

Finally, Listing 9 defines a method named getData . The purpose of this method is to retrieve and to return the individual values stored in the array.

The method requires an incoming parameter value of 0 or 1. This value is used as anindex to identify the specific data value that is to be returned. If the method receives any other value, it throws an IndexOutOfBoundsException .

As mentioned earlier, the code in Listing 8 calls this method three times in succession. The first two calls get and display the two data values shown at the topof Figure 5 . The third call causes the method to throw an exception producing the first "Bad index" message shown in Figure 5 .

The Point class

The GM2D01 class contains a static top-level class named Point . Recall that Kjell tells us that a point is simply a location in space. A point can be represented by a pair of coordinate values in a specific coordinateframe. A convenient way to handle the pair of coordinate values in a program is to store them in a column matrix. An object of the GM2D01.Point class is intended to represent a point in 2D space.

Instantiating a Point object

As you will see shortly, the constructor for the Point class requires a single incoming parameter, which is a reference to an object of the class ColMatrix .

Listing 10 , (which is a fragment from the PointLine02 program) , instantiates a new ColMatrix object and populates it with the values 3.4 and 9.7. Then it instantiates a new Point object, passing the aforementioned ColMatrix object's reference as a parameter to the Point constructor.

Listing 10 . Exercising the Point class.
//A fragment from the PointLine02 program System.out.println(/*blank line*/);System.out.println( "Instantiate and display the contents\n"+ "of a new Point object"); colMatrix = new GM2D01.ColMatrix(3.4,9.7);GM2D01.Point point = new GM2D01.Point(colMatrix); System.out.println(point);try{ System.out.println(point.getData(0));System.out.println(point.getData(1)); //This statement will throw an exception on purposeSystem.out.println(point.getData(-1)); }catch(Exception e){System.out.println("Bad index"); }//end catch

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask