12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

92 chapter 4In a project such as this where there are many different types of objects, it is a goodidea to define each object inside its own .java file (and therefore its own .class file)and to place the main method in a file such as Main.java or ProjectName.java. Thisseparates individual objects from the helper codes that glue the objects together.And since reading a well-written main method should give you a fair idea of whatthe entire program does, you want to be able to find the main methods easily.4.9.2 Ball and Path Class ImplementationThe Ball class in Listing 4.7 creates an object representing a sphere of mass m, radiusr, and moment of inertia I. It is our basic building block. Scrutinize the length ofthe methods in Ball; most are short. Inasmuch as we will be using Ball as a buildingblock, it is a good idea to keep the methods simple and just add more methods tocreate more complicated objects. Take stock of how similar Ball is to the Complexclass in its employment of dynamic (nonstatic) variables. In the present case, m andr behave like the re and im dynamic variables in the Complex class in that theytoo act by being attached to the end of an object’s name. As an example, myBall.mextracts the mass of a ball object.In Ball.java we have defined three dynamic methods, getM, getR, and getI.When affixed to a particular ball object, these methods extract its mass, radius,and moment of inertia, respectively. Dynamic methods are like dynamic variablesin that they behave differently depending on the object they modify. To cite aninstance, ball1.getR() and ball2.getR() return different values if ball1 and ball2have different radii. The getI method computes and returns the moment of inertiaI = 2 5 mr2 of a sphere for an axis passing through its center. The methods getMand getR are template methods; that is, they do not compute anything now butare included to facilitate future extensions. To name an instance, if the Ball classbecomes more complex, you may need to sum the masses of its constituent parts inorder to return the ball’s total mass. With the template in place, you do that withouthaving to reacquaint yourself with the rest of the code first.Look back now and count all the methods in the Ball class. You should find four,none of which is a main method. This is fine because these methods are used byother classes, one of which will have a main method.Exercise: Compile the Ball class. If the Java compiler does not complain, youknow Ball.java contains valid code. Next try to run the byte code in Ball.class:> java Ball // Run Ball.classYou should get an error message of the type java.lang.NoSuchMethodError, withthe word main at the end. This is Java’s way of saying you need a main methodto execute a class file.Exercise: Be adventurous and make a main method for the Ball class. Becausewe have not yet included Path and Baton objects, you will not be able to do−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!