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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

84 chapter 42. Define a daughter class OOPMoon containing:radiuswmoon(xm, ym)trajectory()Radius of moon’s orbitFrequency of moon in orbitMoon’s coordinatesMethod for moon’s orbit relative to sun3. The main program must contain one instance of the class planet andanother instance of the class Moon, that is, one planet object and one Moonobject.4. Have each instance call its own trajectory method to plot the appropriateorbit. For the planet this should be a circle, while for the moon it shouldbe a circle with retrogrades (Figure 4.5).One solution, which produces the same results as the previous program, is theprogram OOPlanet.java in Listing 4.6. As with OOPbeats.java, the main programfor OOPlanet.java is at the end. It is short because all it does is create an OOPMoonobject with the appropriate parameters and then have the moon’s orbit plotted byapplying the trajectory method to the object.✞/ / OOPPlanet . java : Planet orbiting Sunimport ptolemy . plot .∗ ;public class OOPPlanet {double Radius , wplanet , xp , yp; // Orbit r , omega , Planet coordinatespublic OOPPlanet ( ) { Radius = 0 . ; wplanet = 0 . ; } // Default constructorpublic OOPPlanet ( double Rad , double pomg) { Radius = Rad; wplanet = pomg ; }public double getX ( double time ) // Get x of planet at time t{ return Radius∗Math . cos ( wplanet∗time ) ; }public double getY( double time ) // Get y of the planet at time t{ return Radius∗Math . sin ( wplanet∗time ) ; }public void trajectory () { // Trajectory of the planetdouble time ;Plot myPlot = new Plot () ;myPlot. setTitle ( "Motion of a planet around the Sun" );myPlot. setXLabel (" x");myPlot. setYLabel (" y");for ( time = 0.; time

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

Saved successfully!

Ooh no, something went wrong!