// Stuart Reges, Zorah Fung, Benson Limketkai CSE 142 // Point objects have x and y fields that represent // positions in 2D space. // This version has encapsulated fields, a toString method and several // instance methods. public class Point { private int x; private int y; // Create a Point a the given position. public Point(int x, int y) { this.x = x; // use of the this keyword avoids shadowing this.y = y; } // Create a Point representing the origin. public Point() { this(0, 0); } // Returns this point's x coordinate. public int getX() { return x; } // Returns this point's y coordinate. public int getY() { return y; } // Moves x and y coordinates of this point by specified amounts public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } // Returns the distance of this Point from the origin. public double distanceFromOrigin() { Point origin = new Point(); return distance(origin); } // Returns the distance between this Point and another Point. public double distance(Point other) { int dx = x - other.x; int dy = y - other.y; return Math.sqrt(dx * dx + dy * dy); } // Changes the location of this Point. public void setLocation(int newX, int newY) { x = newX; y = newY; } // Returns this Point's coordinates surrounded by parentheses // and separated by a comma. public String toString() { return "(" + x + ", " + y + ")"; } }