// CSE 331 // This file is posted as an example of Javadoc commenting. /** * A Point object represents a 2-D (x, y) pair in the Cartesian plane. * @author Marty Stepp * @version 1.0 */ public class Point implements Cloneable { private int x; // fields private int y; /** * Constructs a new point object at the origin, (0, 0). */ public Point() { this(0, 0); } /** * Constructs a new point with the given x/y coordinates. * @param x the x-coordinate to use * @param y the x-coordinate to use */ public Point(int x, int y) { // constructor this.x = x; this.y = y; } /** * Creates a copy of this Point object. * @return the copy created */ public Point clone() { try { // all of a Point's fields are primitive, so a shallow copy is fine Point copy = (Point) super.clone(); return copy; } catch (CloneNotSupportedException e) { // won't happen return null; } } /** * Returns the direct distance between this point and the given other point. * @param p the point to measure against * @return the direct distance as a real number * @throws NullPointerException if p is null */ public double distance(Point p) { int dx = x - p.x; int dy = y - p.y; return Math.sqrt(dx*dx + dy*dy); } /** * Returns whether o refers to a Point object with the same x/y * coordinates as this Point. * @param o the object to compare against * @return true if o refers to a Point object with the same x/y * coordinates as this Point; otherwise false */ public boolean equals(Object o) { if (o != null && getClass() == o.getClass()) { Point p = (Point) o; return p != null && x == p.x && y == p.y; } else { return false; } } /** * Returns the x-coordinate of this point. * @return the point's x-coordinate */ public int getX() { return x; } // accessor /** * Returns the y-coordinate of this point. * @return the point's y-coordinate */ public int getY() { return y; } /** * Shifts the x/y position of this point by the given offsets. * @param dx the x-offset by which to shift * @param dy the y-offset by which to shift */ public void translate(int dx, int dy) { x += dx; y += dy; } /** * Returns a string representation of this point, such as {@code "(3, -2)"}. */ public String toString() { return "(" + x + ", " + y + ")"; } }