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 107The member functions includex: position after time t,archive: creator of a file of position versus time.For our projectile motion, encapsulation is a combination of the initial conditions(x 0 ,v x0 ) with the member functions used to compute x(t). Our member functionsare the creator of the class of uniform 1-D motion Um1D and the creator x(t) ofa file of x as a function of time t. Inheritance is the child class Um2D for uniformmotion in both the x and y directions, it being created from the parent class Um1Dof 1-D uniform motion. Abstraction is present (although not used powerfully) bythe simple addition of motion in the x and y directions. Polymorphism is present byhaving the member function that creates the output file different for 1-D and 2-Dmotions. In this implementation of OOP, the class Accm2D for accelerated motion intwo dimensions inherits uniform motion in two dimensions (which in turn inheritsuniform 1-D motion) and adds to it the attribute of acceleration.4.14 Java Implementation✞/ / Accm2D . java OOP accelerated motion in 2Dimport java . io .∗ ;class Um1D { / / Um1D class createdprotected double delt ; // So children may access dataprotected int steps ; / / Time steps for file outputprivate double x00 , vx , time ;Um1D( double x0 , double dt , double vx0 , double tott ) {x00 = 0; // Constructor Um1D, initializesdelt = dt;vx = vx0 ;time = tott ;steps = ( int )( tott/delt ) ;}protected double x(double tt ) // Creates x = xo + v∗dt{ return ( x00 + tt∗vx ) ; } / / Method xpublic void archive () throws IOException , FileNotFoundException { // Method archivePrintWriter w = new PrintWriter (new FileOutputStream ("unimot1D . dat") , true );int i;double xx , t t ;tt = 0. ;for ( i = 1 ; i

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

Saved successfully!

Ooh no, something went wrong!