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.

object-oriented programs: impedance & batons 93much more than test that you have created the Ball object, but that is at least astep in the right direction:✞public static void main ( String [] args ) {Ball myBall = new Ball (3.2 , 0.8) ;System . out . println ("M: " + myBall . getM ( ) ) ;System . out . println ("R: " + myBall . getR () ) ;✝System . out . println ("I : " + myBall . getI () ) ; }This testing code creates a Ball object and prints out its properties by affixingget methods to the object. Compile and run the modified Ball.java and therebyensure that the Ball class still works properly.The class Path in Listing 4.8 creates an object that represents the trajectory[(x(t),y(t)] of the center of mass. The class Path is another building block thatwe will use to construct the baton’s trajectory. It computes the initial velocity componentsV 0x and V 0y and stores them as the dynamic class variables v0x and v0y.These variables need to be dynamic because each new path will have its own initialvelocity. The acceleration resulting from gravity is the constant g, which being thesame for all objects can be safely declared as a static variable independent of classinstance. Survey how Path.java stores g not only as a static variable but also asa class variable so that its value is available to all methods in the class. The constructormethod Path( ) of the class Path takes the polar coordinates (V 0 ,θ) asarguments and computes the components of the initial velocity, (v0x, v0y). Thistoo is a building-block class, so it does not need a main method.☎Exercise: Use the main method below to test the class Path. Make a Path objectand find its properties at several different times. Remember, since this a testcode, it does not need to do anything much. Just making an object and checkingthat it has the expected properties are enough.✞✝public static void main ( String [] args ) {Path myPath = new Path(3.0 , 45.0) ;for ( double t = 0.0; t

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

Saved successfully!

Ooh no, something went wrong!