/** * A class representing a 2D point. Allows the measurement * of distances between points. * * This version includes a hashCode implementation. * * @author Sean Barker */ public class PointHash { /** * X coordinate of the point. * Instance variables should always be private! */ private double xCoord; /** * Y coordinate of the point. */ private double yCoord; /** * Construct a new point with the given X and Y values. * * @param xval The x coordinate. * @praam yval The y coordinate. */ public PointHash(double xval, double yval) { // save the argument values into the instance variables xCoord = xval; // equivalent to this.xCoord = xval; yCoord = yval; // equivalent to this.yCoord = yval; } /** * Compute the distance from this point to another point. * * @param otherPoint The other point to compare to * @return The distance from this point to otherPoint. */ public double distanceTo(PointHash otherPoint) { double x1 = this.xCoord; // define some temporary (local) variables double y1 = this.yCoord; double x2 = otherPoint.xCoord; double y2 = otherPoint.yCoord; // compute the distance return Math.sqrt(square(x1 - x2) + square(y1 - y2)); } /** * A helper (i.e., private) method to compute the square of a value. * * @param val The value to square. * @return The squared value. */ private double square(double val) { return val * val; } /** * Compute the distance from this point to the origin. * * @return The distance from this point to (0, 0). */ public double distanceToOrigin() { // construct a new object representing the origin PointHash origin = new PointHash(0, 0); // now use the existing distanceTo method to compute the distance // could also write origin.distanceTo(this); return distanceTo(origin); // equivalent to this.distanceTo(origin); } /** * Get the point's x value (a getter method). * * @return The x coordinate. */ public double getX() { return xCoord; } /** * Get the point's y value (a getter method). * * @return The y coordinate. */ public double getY() { return yCoord; } /** * Change the point's x value (a setter method). * * @param newX The new x coordinate. */ public void setX(double newX) { xCoord = newX; } /** * Change the point's y value (a setter method). * * @param newY The new y coordinate. */ public void setY(double newY) { yCoord = newY; } /** * Construct a string representation of the point object. * * @return A string representation showing the point coordinates. */ public String toString() { return "[" + xCoord + "," + yCoord + "]"; } /** * Check whether this Point is equivalent to another object - * a Point is equal to another Point if they have the same * X and Y coordinate values. * * @param obj The other object to compare against. * @return True if obj is equal to this point. */ @Override public boolean equals(Object obj) { if (obj instanceof PointHash) { PointHash otherPoint = (PointHash) obj; double otherX = otherPoint.xCoord; double otherY = otherPoint.yCoord; return this.xCoord == otherX && this.yCoord == otherY; } else { return false; } } /** * Compute a hash code for this Point. The general contract of the * hashCode method is that any two objects that are considered equal * (according to the equals() method) *must* give the same hash code. * Otherwise, we just want a reasonably uniform distribution of hash code * values (i.e., not overly biased towards any particular values, thus * minimizing collisions). * * @return The hash code of this Point. */ @Override public int hashCode() { // Note: a simpler implementation could be just xCoord + yCoord, but // this implementation results in better key distributions. // The specific value 31 is a small prime that is often used in hash functions // (e.g., also String.hashCode()) due to empirically good behavior, // but it isn't strictly required, merely helpful. return (int) (xCoord + 31 * yCoord); } /** * Main function demonstrating the use of the Point class. */ public static void main(String[] args) { PointHash p1 = new PointHash(3, 5); System.out.println("p1 hash code is " + p1.hashCode()); PointHash p2 = new PointHash(4, 4); System.out.println("p2 hash code is " + p2.hashCode()); } }