New version page

UNI CS 4550 - More Implications of Inheritance & Ball World Example

This preview shows page 1-2-19-20 out of 20 pages.

View Full Document
View Full Document

End of preview. Want to read all 20 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

Session 7 More Implications of Inheritance & Chapter 5: Ball World ExampleImplications of Inheritance/PolymorphismAssignment of ObjectsSlide 4Try to fix the problem by creating a copy() method.Cloneable InterfaceCloneable Box ClassUsing the Cloneable Box ClassEquality Test3. What’s wrong with each of the following?equals() Exampleequals() of BallChapter 5: Ball World ExampleFiguring Out the BallWorldThe Role of Inheritance in Java GraphicsSlide 16Example: Frame manipulationExample: The show() and paint() MethodsExample: The show(), paint(), and repaint() Methods“Don’t call us. We’ll call you.”Session 7More Implications of Inheritance&Chapter 5: Ball World ExampleImplications of Inheritance/Polymorphism•At compile-time, the amount of memory for polymorphic variables cannot be determined, so all objects reside in the heap•Because values reside in the heap, reference semantics is used for assignment and parameter passing•Most natural interpretation of equality is identity. Since programmers often require a different meaning two operators are needed•Garbage collection needed since it is hard for a programmer to know if/when an object is no longer referencedAssignment of Objectspublic class Box { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value; }}pubic class BoxTest { public static void main (String [] args) { Box x = new Box(); x.setValue ( 7 ); Box y = x; y.setValue( 11 ); System.out.println( “x’s value = “ + x.getValue()); System.out.println(“y’s value = “ + y.getValue()); } // end main}What’s the output? Why?Assignment of Objects•semantics for assignment - simply copies the address of to the object Address Heap: single Box object: 4000Run-time Stack:Main’s Activation Record: x: 4000 y: 4000Try to fix the problem by creating a copy() method.Cloneable Interface•Java has no general mechanism to copy arbitrary objects•But, the base class Object provides a clone() method that creates a bitwise copy•The Cloneable interface represents objects that can be cloned•Several methods in Java’s library use clone()Cloneable Box Classpublic class Box implements Cloneable { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value; } public Object clone() { Box b = new Box(); b.setValue ( getValue() ); return b; } // end clone}Using the Cloneable Box Classpubic class BoxTest { public static void main (String [] args) { Box x = new Box(); x.setValue ( 7 ); Box y = (Box) x.clone(); // assigns y a copy of y y.setValue( 11 ); System.out.println( “x’s value = “ + x.getValue()); System.out.println(“y’s value = “ + y.getValue()); } // end main}Equality Test•== tests for pointer equality, so == really tests for object identity (i.e., is it the same object) and not equality.•equals() is a method inherited from class Object. The Java run-time system uses equals() in a number of places and expects to be able to test any Object for equality with any other Object.3. What’s wrong with each of the following?public class Ball { private Rectangle location; private Color color; public Color color() { return color; } public Rectangle region() { return location; }...a) public boolean equals( Ball b ) { return this == b;}b) public boolean equals( Ball b ) { return this.equals( b ); }c) public boolean equals( Ball b ) { return ( location == b.region() ) && ( color == b.color () ); }equals() Exampleclass Circle extends Shape { int radius;... int getRadius() { return radius; }... public boolean equals (Object arg) { if (arg instanceof Circle) { Circle argc = (Circle) arg; if (radius == argc.getRadius()) { return true; } // end if } // end if return false; }}equals() of Ball... public boolean equals (Object arg) { if (arg instanceof Ball) { Ball argBall = (Ball) arg; if (color.equals(argBall.color()) && location.equals(argBall.region()) { return true; } // end if } // end if return false; }}Chapter 5: Ball World ExampleFiguring Out the BallWorld •Why use a Rectangle instead of a Point to represent the Ball’s location? •Modify the color of the ball to be green. •Modify the ball to be much larger and to move much faster. •Modify the simulation so that it runs much longer. •Modify the shape of the window to be 100 X 800.•Modify the Ball so the it initial moves in a random direction and with random magnitude. – (Hint: Use the static double Math.random() in the Java package Math. It returns a random in the range [0.0 – 1.0).The Role of Inheritance in Java Graphics“Don’t call us. We’ll call you.”Without inheritance, we would have to understand many details of how windows work and how they interact with the operating system.•Some of those details are over our heads at this point. We don’t know enough OOP or Java yet.•Even if we could understand them (and we will eventually), we don’t care about them.The Role of Inheritance in Java GraphicsWith inheritance, a BallWorld can act as a “regular” Frame when we don’t care about the details and as a special kind of Frame when we do.But there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld!Example: Frame manipulationYour BallWorld should be able to handle:•Frame relocation•Frame resizing•Minimizing/Maximizing•Etc….But wait, _I_ didn’t write any code to handle this…Example: The show() and paint() MethodsBut there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld!Consider how the BallWorld program displays its output:// BallWorldpublic void paint( Graphics g ) {aBall.paint( g );aBall.move ();...counter = counter + 1;if ( counter < 2000 )repaint();elseSystem.exit(0);}Example: The show(), paint(), and repaint() Methods// In BallWorldpublic void paint( Graphics g )


View Full Document
Loading Unlocking...
Login

Join to view More Implications of Inheritance & Ball World Example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More Implications of Inheritance & Ball World Example and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?