// Tyler Rigsby, CSE 142 // Defines a point class with an x and a y. Points can be translated and can // tell you their distance from the origin. public class Point { private int x; private int y; // 0-argument constructor initializes x and y to 0 public Point() { } // initializes the x and y of the Point to the specified values public Point(int x, int y) { setLocation(x, y); } // setter (mutator) // sets the location to the specified x, y public void setLocation(int x, int y) { this.x = x; this.y = y; } // getters (accessors) // returns the x value of the point public int getX() { return x; } // returns the y value of the point public int getY() { return y; } // Translates the point's x by dx and y by dy public void translate(int dx, int dy) { x = x + dx; y = y + dy; } // returns the distance between this point and the point passed as the parameter public double getDistanceFrom(Point other) { int dx = this.x - other.x; int dy = this.y - other.y; return Math.sqrt(dx * dx + dy * dy); } // Returns the point's distance from the origin. public double getDistanceFromOrigin() { return getDistanceFrom(new Point()); } public String toString() { return "(" + x + ", " + y + ")"; } }