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.

82 chapter 4no arguments and returns no value; the waves are summed on line 19, and thegraph plotted all within the method.✞/ / OOPBeats . java : OOP Superposition 2 Sine wavesimport ptolemy . plot .∗ ;☎2public class OOPBeats {public double A, k1 , k2 ;public OOPBeats ( double Ampl , double freq1 , double freq2 ){ A = Ampl; k1 = freq1 ; k2 = freq2 ; }// Class Constructor468✝}public void sumwaves ( ) { / / Sums 2 wavesint i;double y1 , y2 , y3 , x = 0;Plot myPlot = new Plot () ;myPlot. setTitle ("Superposition of two Sines") ;myPlot. setXLabel (" x");for ( i = 1; i < 501; i++ ) {y1 = A∗Math . sin ( k1∗x) ; / / 1st Siney2 = A∗Math . sin ( k2∗x) ; / / 2nd Siney3 = y1 + y2; // SuperpositonmyPlot . addPoint (0 , x , y3 , true );x = x + 0.01; / / Increment x}PlotApplication app = new PlotApplication (myPlot) ;}public static void main ( String [] argv ) { // Class instanceOOPBeats sumsines = new OOPBeats ( 1 . , 30. , 33.) ; // Instancesumsines .sumwaves ( ) ; // Call sumsins ’ method}1012141618202224262830Listing 4.4 OOPBeats.java plots beats using OOP. Contrast this with the procedural programBeats.java in Listing 4.3.4.6.2 OOP PlanetIn our second example we add together periodic functions representing positionsversus time. One set describes the position of the moon as it revolves around aplanet, and the other set describes the position of the planet as it revolves about thesun. Specifically, the planet orbits the sun at a radius R =4units with an angularfrequency ω p =1rad/s, while the moon orbits Earth at a radius r =1unit from theplanet and an angular velocity ω s =14rad/s. The position of the planet at time trelative to the sun is described byx p = R cos(ω p t), y p = R sin(ω p t). (4.19)The position of the satellite, relative to the sun, is given by the sum of its positionrelative to the planet and the position of the planet relative to the sun:x s = x p + r cos(ω s t)=R cos(ω p t)+r cos(ω s t),y s = y p + r sin(ω s t)=R sin(ω p t)+r sin(ω s t).−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 82

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

Saved successfully!

Ooh no, something went wrong!