// A very simple class that defines a cartesian point, and has a translate // method, a constructor, and a toString() method. Enforces that the // point is never negative. public class Point8 { private int x; // an "instance variable" defining the point's x value private int y; // an "instance variable" defining the point's y value // a constructor method for a Point object public Point8(int x, int y) { this.setLocation(x, y); } // returns X coordinate public int getX() { return this.x; } // returns Y coordinate public int getY() { return this.y; } // sets the location of the point to x, y public void setLocation(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException("components shouldn't be -ve"); } this.x = x; this.y = y; } // this method translates the point by (dx, dy) public void translate(int dx, int dy) { this.setLocation(this.x + dx, this.y + dy); } // a more useful toString() method to print the Point's value public String toString() { return "(" + this.x + ", " + this.y + ")"; } }