WatorWorld
Class Location

java.lang.Object
  extended by WatorWorld.Location
All Implemented Interfaces:
Comparable<Location>

public class Location
extends Object
implements Comparable<Location>

A Location object represents the row and column of a location in a two-dimensional grid.
The API of this class is testable on the AP CS A and AB exams.


Field Summary
static int AHEAD
          The turn angle for making no turn.
static int EAST
          The compass direction for east.
static int FULL_CIRCLE
          The turn angle for turning a full circle.
static int HALF_CIRCLE
          The turn angle for turning a half circle.
static int HALF_LEFT
          The turn angle for turning 45 degrees to the left.
static int HALF_RIGHT
          The turn angle for turning 45 degrees to the right.
static int LEFT
          The turn angle for turning 90 degrees to the left.
static int NORTH
          The compass direction for north.
static int NORTHEAST
          The compass direction for northeast.
static int NORTHWEST
          The compass direction for northwest.
static int RIGHT
          The turn angle for turning 90 degrees to the right.
static int SOUTH
          The compass direction for south.
static int SOUTHEAST
          The compass direction for southeast.
static int SOUTHWEST
          The compass direction for southwest.
static int WEST
          The compass direction for west.
 
Constructor Summary
Location(int r, int c)
          Constructs a location with given row and column coordinates.
 
Method Summary
 int compareTo(Location other)
          Compares this location to other for ordering.
 boolean equals(Object other)
          Indicates whether some other Location object is "equal to" this one.
 Location getAdjacentLocation(int direction)
          Gets the adjacent location in any one of the eight compass directions.
 int getCol()
          Gets the column coordinate.
 int getDirectionToward(Location target)
          Returns the direction from this location toward another location.
 int getRow()
          Gets the row coordinate.
 int hashCode()
          Generates a hash code.
 String toString()
          Creates a string that describes this location.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

LEFT

public static final int LEFT
The turn angle for turning 90 degrees to the left.

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
The turn angle for turning 90 degrees to the right.

See Also:
Constant Field Values

HALF_LEFT

public static final int HALF_LEFT
The turn angle for turning 45 degrees to the left.

See Also:
Constant Field Values

HALF_RIGHT

public static final int HALF_RIGHT
The turn angle for turning 45 degrees to the right.

See Also:
Constant Field Values

FULL_CIRCLE

public static final int FULL_CIRCLE
The turn angle for turning a full circle.

See Also:
Constant Field Values

HALF_CIRCLE

public static final int HALF_CIRCLE
The turn angle for turning a half circle.

See Also:
Constant Field Values

AHEAD

public static final int AHEAD
The turn angle for making no turn.

See Also:
Constant Field Values

NORTH

public static final int NORTH
The compass direction for north.

See Also:
Constant Field Values

NORTHEAST

public static final int NORTHEAST
The compass direction for northeast.

See Also:
Constant Field Values

EAST

public static final int EAST
The compass direction for east.

See Also:
Constant Field Values

SOUTHEAST

public static final int SOUTHEAST
The compass direction for southeast.

See Also:
Constant Field Values

SOUTH

public static final int SOUTH
The compass direction for south.

See Also:
Constant Field Values

SOUTHWEST

public static final int SOUTHWEST
The compass direction for southwest.

See Also:
Constant Field Values

WEST

public static final int WEST
The compass direction for west.

See Also:
Constant Field Values

NORTHWEST

public static final int NORTHWEST
The compass direction for northwest.

See Also:
Constant Field Values
Constructor Detail

Location

public Location(int r,
                int c)
Constructs a location with given row and column coordinates.

Parameters:
r - the row
c - the column
Method Detail

getRow

public int getRow()
Gets the row coordinate.

Returns:
the row of this location

getCol

public int getCol()
Gets the column coordinate.

Returns:
the column of this location

getAdjacentLocation

public Location getAdjacentLocation(int direction)
Gets the adjacent location in any one of the eight compass directions.

Parameters:
direction - the direction in which to find a neighbor location
Returns:
the adjacent location in the direction that is closest to direction

getDirectionToward

public int getDirectionToward(Location target)
Returns the direction from this location toward another location. The direction is rounded to the nearest compass direction.

Parameters:
target - a location that is different from this location
Returns:
the closest compass direction from this location toward target

equals

public boolean equals(Object other)
Indicates whether some other Location object is "equal to" this one.

Overrides:
equals in class Object
Parameters:
other - the other location to test
Returns:
true if other is a Location with the same row and column as this location; false otherwise

hashCode

public int hashCode()
Generates a hash code.

Overrides:
hashCode in class Object
Returns:
a hash code for this location

compareTo

public int compareTo(Location other)
Compares this location to other for ordering. Returns a negative integer, zero, or a positive integer as this location is less than, equal to, or greater than other. Locations are ordered in row-major order.
(Precondition: other is a Location object.)

Specified by:
compareTo in interface Comparable<Location>
Parameters:
other - the other location to test
Returns:
a negative integer if this location is less than other, zero if the two locations are equal, or a positive integer if this location is greater than other

toString

public String toString()
Creates a string that describes this location.

Overrides:
toString in class Object
Returns:
a string with the row and column of this location, in the format (row, col)