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.

object-oriented programs: impedance & batons 91✞/ / Path . java : Parabolic Trajectory Classpublic class Path {public static final double g = 9.8; // Static , same a l l Pathspublic double v0x , v0y ; / / Non−static , unique each Pathpublic Path ( double v0 , double theta ) {v0x = v0 ∗ Math . cos ( theta∗Math . PI /180.) ;v0y = v0 ∗ Math . sin ( theta ∗ Math . PI / 1 8 0 . ) ; }public double getX ( double t) { return v0x ∗ t; }☎246810✝}public double getY( double t) { return v0y ∗ t − 0.5∗ g∗ t ∗ t; }12Listing 4.8 The class Path creating an object that represents the trajectory of the centerof mass.✞✝/ / Baton . java : Combines classes to form Batonimport ptolemy . plot .∗ ;public class Baton {public double L; // Nonstatic variables unique ea batonpublic double w; / / Omegapublic Path path ; / / Path objectpublic Ball ball ; // Ball objectBaton ( Path p, Ball b, double L1 , double w1) { path = p; ball = b; L = L1; w = w1;}public double getM ( ) { return 2∗ ball .getM ( ) ; }public double getI () { return 2∗ ball . getI () + 1./2.∗ ball .getM ( ) ∗L∗L; }public double getXa ( double t) {return path . getX ( t ) + L/2∗Math . cos (w∗ t);}public double getYa ( double t) {return path . getY( t ) + L/2∗Math . sin (w∗ t); }public double getXb ( double t) { return path . getX ( t ) − L/2∗ Math . cos (w∗ t); }public double getYb ( double t) { return path . getY( t ) − L/2∗Math . sin (w∗ t); }public static void main ( String args []) { / / Main methoddouble x, y;Plot myPlot = new Plot () ; // Create PlotBall myBall = new Ball (0.5 , 0.4) ; // Create BallPath myPath = new Path(15. , 34.) ; // Create PathBaton myBaton = new Baton (myPath , myBall , 2.5 , 15.) ; / / BatonmyPlot. setTitle ("y vs x" );myPlot. setXLabel ("x" );myPlot. setYLabel ("y" );for ( double t = 0 . ; myPath . getY ( t ) >= 0 . ; t += 0 . 0 2 ){ x = myBaton . getXa ( t ) ;y = myBaton . getYa ( t ) ;System . out . println ("t = " + t + " x = " + x + " y = " + y) ;myPlot . addPoint (0 , x , y, true ); }PlotApplication app = new PlotApplication (myPlot) ;} }☎2468101214161820222426283032343638Listing 4.9 Baton.java combines ball and path classes to form a baton.−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 91

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

Saved successfully!

Ooh no, something went wrong!