Point.java

Download Point.java

 1: /** Class Point represents a point in a Cartesian plane.
 2: * "x" is the distance from the left edge and "y" is the distance
 3: * from the bottom edge.
 4: * @author Wayne Pollock, Tampa Florida USA
 5: */
 6: 
 7: /* This trivial class demonstrates four common methods that
 8: * "production quality" classes should implement; three override
 9: * inherited methods from class Object.  Adding objects of this
10: * class to collections requires these methods.
11: *
12: * Take note of the generic Comparable interface used, and the
13: * fact that Point is immutable; there was no real need to add
14: * "getX" and "getY" methods, although you can if desired.  There
15: * is also no point in added a clone or copy constructor.
16: */
17: 
18: public class Point implements Comparable<Point> {
19:     public final int x, y;
20: 
21:    public Point ( int x, int y ) {
22:       this.x = x;
23:       this.y = y;
24:    }
25: 
26:    /** Compare two points for equality: x == x && y == y. */
27:    @Override
28:    public boolean equals ( Object other ) {
29:       if ( this == other ) return true;
30:       if ( other == null )  return false;
31:       if ( ! (other instanceof Point) )  return false;
32:       Point otherPoint = (Point) other;
33:       return ( x == otherPoint.x && y == otherPoint.y );
34:    }
35: 
36:    //* Compute hash code using both x and y. */
37:    @Override
38:    public int hashCode () {
39:       int result = 17;
40:       result = 37 * result + x;
41:       result = 37 * result + y;
42:       return result;
43:    }
44: 
45:    /** Displays a Point as "(x,y)". */
46:    @Override
47:    public String toString () {
48:       return "(" + x + "," + y + ")";
49:    }
50: 
51:    /** Compares two Points.
52:     *  Define the order as X, then Y (so "(1,5) < (2,2)"):
53:     */
54:    public int compareTo ( Point other ) {
55:       if ( other == null )
56:          throw new IllegalArgumentException( "Argument must not be null." );
57: 
58:       if ( x == other.x )
59:          return y - other.y;
60:       else
61:          return x - other.x;
62:    }
63: 
64:    /** Determines the relative horizontal position of two Points. */
65:    public boolean isLeftOf ( Point other ) {
66:       if ( other == null )
67:          throw new IllegalArgumentException( "Argument must not be null." );
68:       return x < other.x;
69:    }
70: 
71:    /** Determines the relative vertical position of two Points. */
72:    public boolean isAbove ( Point other ) {
73:       if ( other == null )
74:          throw new IllegalArgumentException( "Argument must not be null." );
75:       return y > other.y;
76:    }
77: 
78:    // Demo using these methods:
79:    public static void main ( String [] args ) {
80:       Point p1 = new Point(1,2);
81:       Point p2 = new Point(2,1);
82: 
83:       System.out.println( "p1: " + p1 );
84:       System.out.println( "p2: " + p2 );
85:       System.out.println( p1 + (p1.isLeftOf(p2) ? " is left" : " is right")
86:          + " of " + p2 );
87:       System.out.println( p1 + (p1.isAbove(p2) ? " is above " : " is below ")
88:          + p2 );
89:       System.out.println( "p1.equals(p2) = " + p1.equals(p2) );
90:       System.out.println( "p1.compareTo(p2) = " + p1.compareTo(p2) );
91:       System.out.println( "p1.hashCode() = " + p1.hashCode() );
92:       System.out.println( "p2.hashCode() = " + p2.hashCode() );
93:    }
94: }