// Third version of Point class for storing an ordered pair (x, y). This // version is encapsulated and has fields, two constructors, additional methods // to set the location and compute the distance from another point, and a // toString method. It also uses the "this" notation to simplify parameter // names and to have one constructor call the other. public class Point { private int x; private int y; // constructs a point that corresponds to the origin (0, 0) public Point() { this(0, 0); } // constructs a point with given x and y coordinates public Point(int x, int y) { setLocation(x, y); } // translates this points's coordinates by the given delta-x and delta-y public void translate(int dx, int dy) { x = x + dx; y = y + dy; } // returns the distance from this point to the given point public double distance(Point other) { int dx = this.x - other.x; int dy = this.y - other.y; return Math.sqrt(dx * dx + dy * dy); } // returns this point's x coordinate public int getX() { return x; } // returns this point's y coordinate public int getY() { return y; } // sets this point's location to the given x and y coordinates public void setLocation(int x, int y) { this.x = x; this.y = y; } // returns a text representation of the point: (x, y) public String toString() { return "(" + x + ", " + y + ")"; } }