/home/wpollock1/public_html/AJava/Point.java

/** Class Point represents a point in a Cartesian plane.
* "x" is the distance from the left edge and "y" is the distance
* from the bottom edge.
* @author Wayne Pollock, Tampa Florida USA
*/

/* This trivial class demonstrates four common methods that
* "production quality" classes should implement; three override
* inherited methods from class Object.  Adding objects of this
* class to collections requires these methods.
*
* Take note of the generic Comparable interface used, and the
* fact that Point is immutable; there was no real need to add
* "getX" and "getY" methods, although you can if desired.  There
* is also no point in added a clone or copy constructor.
*/

public class Point implements Comparable<Point> {
    public final int x, y;

   public Point ( int x, int y ) {
      this.x = x;
      this.y = y;
   }

   /** Compare two points for equality: x == x && y == y. */
   @Override
   public boolean equals ( Object other ) {
      if ( this == other ) return true;
      if ( other == null )  return false;
      if ( ! (other instanceof Point) )  return false;
      Point otherPoint = (Point) other;
      return ( x == otherPoint.x && y == otherPoint.y );
   }

   //* Compute hash code using both x and y. */
   @Override
   public int hashCode () {
      int result = 17;
      result = 37 * result + x;
      result = 37 * result + y;
      return result;
   }

   /** Displays a Point as "(x,y)". */
   @Override
   public String toString () {
      return "(" + x + "," + y + ")";
   }

   /** Compares two Points.
    *  Define the order as X, then Y (so "(1,5) < (2,2)"):
    */
   public int compareTo ( Point other ) {
      if ( other == null )
         throw new IllegalArgumentException( "Argument must not be null." );

      if ( x == other.x )
         return y - other.y;
      else
         return x - other.x;
   }

   /** Determines the relative horizontal position of two Points. */
   public boolean isLeftOf ( Point other ) {
      if ( other == null )
         throw new IllegalArgumentException( "Argument must not be null." );
      return x < other.x;
   }

   /** Determines the relative vertical position of two Points. */
   public boolean isAbove ( Point other ) {
      if ( other == null )
         throw new IllegalArgumentException( "Argument must not be null." );
      return y > other.y;
   }

   // Demo using these methods:
   public static void main ( String [] args ) {
      Point p1 = new Point(1,2);
      Point p2 = new Point(2,1);

      System.out.println( "p1: " + p1 );
      System.out.println( "p2: " + p2 );
      System.out.println( p1 + (p1.isLeftOf(p2) ? " is left" : " is right")
         + " of " + p2 );
      System.out.println( p1 + (p1.isAbove(p2) ? " is above " : " is below ")
         + p2 );
      System.out.println( "p1.equals(p2) = " + p1.equals(p2) );
      System.out.println( "p1.compareTo(p2) = " + p1.compareTo(p2) );
      System.out.println( "p1.hashCode() = " + p1.hashCode() );
      System.out.println( "p2.hashCode() = " + p2.hashCode() );
   }
}