



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Department of Computer Science Wellesley College
Friday, November 16, 2007 Objects From Scratch 19- 2
Suppose we want to describe Point objects with two instance variables named x and y. Execution Land ObjectLand p Point x y 3 4 Point p1 = new Point(3,4); System.out.println( โp1 = โ + p1.toString()); Point p2 = new Point(p1.y, 2*p1.x); Console System.out.println(โp2 = โ + p2); p1.x = p2.x + 5; p2.y++; System.out.println(โp1 = โ + p1); x y 4 6 Point p p1 = (3,4) p2 = (4,6) p1 = (9,4) Point (unlike Location) components can change over time. p2 = (4,7) System.out.println(โp2 = โ + p2); 9 7
Objects From Scratch 19- 3 Declaring the Point Class public class Point { // by default, extends the Object class // Instance Variables public int x; // x coordinate public int y; // y coordinate // Constructor Method public Point (int initx, int inity) { this.x = initx; // set x coordinate to initx this.y = inity; // set y coordinate to inity // the โthis.โ is optional above and elsewhere } // Instance Method public String toString() { return โ(โ + this.x + โ,โ + this.y + โ)โ); } } Objects From Scratch 19- 4 Constructor Methods in a JEM p ExecutionLand ObjectLand Point p1 = new Point(3,4); current frame
PT Point x y 0 0 PT this PT Steps in the invocation of a constructor method:
Objects From Scratch 19- 7 Some Buggle Instance Methods // Some instance methods in the Buggle class public Location getPosition () { } public Location setPosition (Location loc) { } // getHeading(), setHeading(), getColor(), setColor() // brushUp(), and brushDown() are similar. Objects From Scratch 19- 8 More Buggle Instance Methods // More instance methods in the Buggle class // Some methods from the Direction contract are helpful for these! public Location left () { } public Location forward () { // Donโt worry about walls } public Location forward (int n) { // Donโt worry about walls }
Objects From Scratch 19- 9 Extending Buggles with New State Letโs define a CountingBuggle class with an instance variable named count used in the following instance method: public int distanceFromWall() { this.count = 0; while (!this.isFacingWall()) { // Assume we can handle walls this.count++; this.forward(); } this.backward(this.count); return this.count; } For example: CountingBuggle counter = new CountingBuggle(); counter.brushUp(); System.out.println(counter.distanceFromWall()); Objects From Scratch 19- 10 Defining the CountingBuggle Class public class CountingBuggle extends Buggle { // New instance variable (in addition to those of Buggles) // Constructor method public CountingBuggle() { // Implicitly executes the body of the Buggle() constructor // and then executes the body below: } // Instance method public int distanceFromWall() {โฆ as defined on previous slide โฆ} }
Objects From Scratch 19- 13 Summary of Class Declarations There are five kinds of declarations in a Java class: o three kinds of method declarations: