com.collegeboard.gridworld.grid
Class Location

java.lang.Object
  extended by com.collegeboard.gridworld.grid.Location
All Implemented Interfaces:
Comparable

public class Location
extends Object
implements Comparable

AP® Computer Science Marine Biology Simulation:
A Location object represents the row and column of a location in a two-dimensional grid.

The Location class is copyright© 2002 College Entrance Examination Board (www.collegeboard.com).

Version:
1 July 2002
Author:
Alyce Brady, Chris Nevison, APCS Development Committee

Field Summary
static int EAST
           
static int FULL_CIRCLE
           
static int HALF_CIRCLE
           
static int HALF_LEFT
           
static int HALF_RIGHT
           
static int LEFT
           
static int NORTH
           
static int NORTHEAST
           
static int NORTHWEST
           
static int RIGHT
           
static int SOUTH
           
static int SOUTHEAST
           
static int SOUTHWEST
           
static int WEST
           
 
Constructor Summary
Location(int row, int col)
          Constructs a Location object.
 
Method Summary
 int col()
          Returns the column coordinate of this location.
 int compareTo(Object other)
          Compares this location to other for ordering.
 int directionToward(Location target)
          Returns the direction from this location toward another location
 boolean equals(Object other)
          Indicates whether some other Location object is "equal to" this one.
 Location getNeighborLocation(int direction)
          Gets the neighbor location in the given direction
 int hashCode()
          Generates a hash code for this location (will not be tested on the Advanced Placement exam).
 int row()
          Returns the row coordinate of this location.
 String toString()
          Represents this location as a string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

FULL_CIRCLE

public static final int FULL_CIRCLE
See Also:
Constant Field Values

HALF_CIRCLE

public static final int HALF_CIRCLE
See Also:
Constant Field Values

HALF_LEFT

public static final int HALF_LEFT
See Also:
Constant Field Values

LEFT

public static final int LEFT
See Also:
Constant Field Values

HALF_RIGHT

public static final int HALF_RIGHT
See Also:
Constant Field Values

RIGHT

public static final int RIGHT
See Also:
Constant Field Values

NORTH

public static final int NORTH
See Also:
Constant Field Values

NORTHEAST

public static final int NORTHEAST
See Also:
Constant Field Values

EAST

public static final int EAST
See Also:
Constant Field Values

SOUTHEAST

public static final int SOUTHEAST
See Also:
Constant Field Values

SOUTH

public static final int SOUTH
See Also:
Constant Field Values

SOUTHWEST

public static final int SOUTHWEST
See Also:
Constant Field Values

WEST

public static final int WEST
See Also:
Constant Field Values

NORTHWEST

public static final int NORTHWEST
See Also:
Constant Field Values
Constructor Detail

Location

public Location(int row,
                int col)
Constructs a Location object.

Parameters:
row - location's row
col - location's column
Method Detail

row

public int row()
Returns the row coordinate of this location.

Returns:
row of this location

col

public int col()
Returns the column coordinate of this location.

Returns:
column of this location

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 at the same row and column as the current location; false otherwise

hashCode

public int hashCode()
Generates a hash code for this location (will not be tested on the Advanced Placement exam).

Overrides:
hashCode in class Object
Returns:
a hash code for a Location object

compareTo

public int compareTo(Object 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
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()
Represents this location as a string.

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

getNeighborLocation

public Location getNeighborLocation(int direction)
Gets the neighbor location in the given direction

Parameters:
direction - an angle (0 degrees = Location.NORTH, 45 degrees = Location.NORTHWEST, etc.)
Returns:
the neighbor location in the direction that is obtained by rounding angle to the nearest multiple of 45 degrees

directionToward

public int directionToward(Location target)
Returns the direction from this location toward another location

Parameters:
target - another location
Returns:
the closest compass direction from this location toward target