// Ayaz Latif // TA: Grace Hopper // CSE142 // This class represents a point in the Cartesian plane (with an x- and y-coordinate). public class Point { private int x; private int y; // Constructs a new point at the origin (0, 0) public Point() { this(0, 0); } // Constructs a new point at the given x- and y-coordinates // int x: x-coordinate of newly-constructed Point // int y: y-coordinate of newly-constructed Point public Point(int initialX, int initialY) { setLocation(initialX, initialY); } // Returns the x-coordinate of this point public int getX() { return x; } // Returns the y-coordinate of this point public int getY() { return y; } // Sets the point to the given location, specified by the given x- and // y-coordinates // int x: new x-coordinate of the point // int y: new y-coordinate of the point public void setLocation(int x, int y) { this.x = x; this.y = y; } // Translates the point by the given dx and dy. // int dx: translation of x-coordinate // int dy: translation of y-coordinate public void translate(int dx, int dy) { x = x + dx; y = y + dy; } // returns the (double) distance from this point to the origin (0, 0) public double distanceFromOrigin() { double distance = Math.sqrt(x * x + y * y); return distance; } // returns the (double) distance from this point to a second point p2 // Point p2: second point to calculate distance to public double distanceTo(Point p2) { double distance = Math.sqrt(Math.pow(x - p2.x, 2) + Math.pow(y - p2.y, 2)); return distance; } // Returns a String representation of this point public String toString() { return "(" + x + ", " + y + ")"; } }