// Stuart Reges handout #25 // 11/28/05 // // This class is a minor variation of the chapter 8 Point class that keeps // track of how many times each Point has been translated. // // A Point object represents an ordered pair of 2D coordinates (x, y). public class Point { private int x; // x-coordinate private int y; // y-coordinate private int translateCount; // # of times this Point has been translated // Constructs a Point object at the origin, (0, 0). public Point() { this(0, 0); } // Constructs a Point object with the given x and y coordinates. public Point(int x, int y) { this.setLocation(x, y); this.translateCount = 0; } // Returns the x-coordinate of this Point. public int getX() { return this.x; } // Returns the y-coordinate of this Point. public int getY() { return this.y; } // Sets this Point's x/y coordinates to the given values. public void setLocation(int x, int y) { this.x = x; this.y = y; } // Returns a String representation of this Point object. public String toString() { return "(" + this.x + ", " + this.y + ")"; } // Shifts the position of this Point object by the given amount // in each direction. public void translate(int dx, int dy) { this.setLocation(this.x + dx, this.y + dy); this.translateCount++; } // Returns a count of the number of times translate has been called for // this Point. public int getTranslateCount() { return this.translateCount; } }
Stuart Reges
Last modified: Mon Nov 28 17:05:57 PST 2005