/* Point.java */ /** * Point : A Point object stores a 2-D grid coordinate. * Points are immutable (they can not be modified once created). */ public class Point { /** Create a new Point initialized to (r, c) */ public Point(int r, int c) { myRow = r; myCol = c; } /** Returns the row of this Point */ public int row() { return myRow; } /** Returns the column of this Point */ public int column() { return myCol; } /** Returns true <==> this Point's coordinates exactly match pt's */ public boolean equals( Object pt ) { Point p = (Point) pt; return (myRow == p.myRow) && (myCol == p.myCol); } /** Assumes: dir == one of {NORTH, SOUTH, EAST, WEST} * Returns a new Point object that is "this" Point's position * shifted by one in the direction dir. */ Point move( int dir ) { Point newPoint = new Point( myRow, myCol ); switch (dir) { case Direction.NORTH: newPoint.myRow--; break; case Direction.EAST: newPoint.myCol++; break; case Direction.SOUTH: newPoint.myRow++; break; case Direction.WEST: newPoint.myCol--; break; } return newPoint; } /** Returns a String representation of this Point */ public String toString() { return "(" + myCol + ", " + myRow + ")"; } // Data fields private int myRow; private int myCol; };