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 101then write the appropriate methods for accessing the relevant variables. This OOPprocess of hiding the object’s variables is called data hiding.4.9.10 Encapsulation Exercise1. Place the private key word before all the class variables in Ball.java. Thisaccomplishes the first step in encapsulating an object. Print out myBall.m andmyBall.r from the main method. The Java compiler should complain becausethe variables are now private (visible to Ball class members only) and themain method is outside the Ball class.2. Create methods that allow you to manipulate the object in an abstract way;for example, to modify the mass of a Ball object and assign it to the privateclass variable m, include the command myBall.setM(5.0). This is the secondstep in encapsulation. We already have the methods getM, getR, andgetI, and the object constructor Ball, but they do not assign a mass to theball. Insofar as we have used a method to change the private variable m,we have kept our code as general as possible and still have our objectsencapsulated.3. When we write getM(), we are saying that M is the property to be retrieved froma Ball object. Inversely, the method setM sets the property M of an object equalto the argument that is given. This is part of encapsulation because with bothget and set methods on hand, you do not need to access the class variablesfrom outside the class. The use of get and set methods is standard practice inJava. You do not have to write get and set methods for every class you create,but you should create these methods for any class you want encapsulated. Ifyou look back at Chapter 3, “Visualization Tools,” you will see that the classesin the PtPlot library have many get and set methods, for example, getTitle,setTitle, getXLabel, and setXLabel.4. Java’s interface key word allows us to specify an interface. Here BallInterfacedefines an interface for Ball-like objects:✞☎public interface BallInterface {public double getM ( ) ;public double getR () ;public double getI () ;}✝This interface does not do anything by itself, but if you modify Ball.java sothat public class Ball is replaced by public class Ball implements BallInterface,then the Java compiler will check that the Ball class has all the methodsspecified in the interface. The Java commands interface and implements areuseful in having the compiler check that your classes have all the requiredmethods.5. Add an arbitrary new method to the interface and compile Ball. If the methodis found in Ball.java, then the Ball class will compile without error.−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 101

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

Saved successfully!

Ooh no, something went wrong!