/** * A point has an x- and y-coordinate. * * @author Franck van Breugel */ public class Point implements Comparable { private int x; private int y; /** * Initializes this point with the given x- and y-coordinate. * * @param x the x-coordinate of this point. * @param y the y-coordinate of this point. */ public Point(int x, int y) { this.setX(x); this.setY(y); } /** * Returns the x-coordinate of this point. * * @return the x-coordinate of this point. */ public int getX() { return this.x; } /** * Sets the x-coordinate of this point to the given x-coordinate. * * @param x the new x-coordinate of this point. */ private void setX(int x) { this.x = x; } /** * Returns the y-coordinate of this point. * * @return the y-coordinate of this point. */ public int getY() { return this.y; } /** * Sets the y-coordinate of this point to the given y-coordinate. * * @param y the new y-coordinate of this point. */ private void setY(int y) { this.y = y; } /** * Returns the hash code of this point. * * @return the hash code of this point. */ public int hashCode() { return this.getX() * this.getY(); } /** * Tests whether this point is the same as the given object. * Two points are the same if they have the same x- and * y-coordinates. * * @param object an object * @return true if this point is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Point other = (Point) object; equal = this.getX() == other.getX() && this.getY() == other.getY(); } else { equal = false; } return equal; } /** * Compares this point to the other given point. * Returns a negative integer/zero/positive integer * if this point is smaller than/the same/greater than * the other point in the lexicographic order. * * @param other another point. * @return a negative integer/zero/positive integer * if this point is smaller than/the same/greater than * the other point in the lexicographic order. */ public int compareTo(Point other) { int difference; if (this.getX() == other.getX()) { difference = this.getY() - other.getY(); } else { difference = this.getX() - other.getX(); } return difference; } /** * Returns a string representation of this point. * The representation is of the form (x, y), where * x and y are the x- and y-coordinates of this point. * * @return a string representation of this point. */ public String toString() { return String.format("(%d, %d)", this.getX(), this.getY()); } }