AP® Computer Science Marine Biology Simulation

Class SquareEnvironment

java.lang.Object
  |
  +--SquareEnvironment
All Implemented Interfaces:
Environment
Direct Known Subclasses:
BoundedEnv, UnboundedEnv

public abstract class SquareEnvironment
extends java.lang.Object
implements Environment

AP® Computer Science Marine Biology Simulation:
SquareEnvironment is an abstract class that implements only the navigational methods in the Environment interface. It considers the cells in the environment to be square, with sides to the north, south, east, and west, and navigates accordingly.

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

Version:
1 July 2002
See Also:
Direction, Location

Constructor Summary
SquareEnvironment()
          Constructs a SquareEnvironment object in which cells have four adjacent neighbors -- those with which they share sides.
SquareEnvironment(boolean includeDiagonalNeighbors)
          Constructs a SquareEnvironment object in which cells have four or eight adjacent neighbors, depending on the value of the includeDiagonalNeighbors parameter.
 
Method Summary
 Direction getDirection(Location fromLoc, Location toLoc)
          Returns the direction from one location to another.
 Location getNeighbor(Location fromLoc, Direction compassDir)
          Returns the adjacent neighbor (whether valid or invalid) of a location in the specified direction.
 java.util.ArrayList neighborsOf(Location ofLoc)
          Returns the adjacent neighbors of a specified location.
 int numAdjacentNeighbors()
          Returns the number of adjacent neighbors around each cell.
 int numCellSides()
          Returns the number of sides around each cell.
 Direction randomDirection()
          Generates a random direction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface Environment
add, allObjects, isEmpty, isValid, numCols, numObjects, numRows, objectAt, recordMove, remove
 

Constructor Detail

SquareEnvironment

public SquareEnvironment()
Constructs a SquareEnvironment object in which cells have four adjacent neighbors -- those with which they share sides. These neighbors are in the four cardinal directions.

SquareEnvironment

public SquareEnvironment(boolean includeDiagonalNeighbors)
Constructs a SquareEnvironment object in which cells have four or eight adjacent neighbors, depending on the value of the includeDiagonalNeighbors parameter. If includeDiagonalNeighbors is true, cells have eight adjacent neighbors -- the immediately adjacent neighbors on all four sides and the four neighbors on the diagonals. If includeDiagonalNeighbors is false, cells have only the four neighbors they would have in an environment created with the default SquareEnvironment constructor.
Parameters:
includedDiagonalNeighbors - whether to include the four diagonal locations as neighbors
Method Detail

numCellSides

public int numCellSides()
Returns the number of sides around each cell.
Specified by:
numCellSides in interface Environment
Returns:
the number of cell sides in this environment

numAdjacentNeighbors

public int numAdjacentNeighbors()
Returns the number of adjacent neighbors around each cell.
Specified by:
numAdjacentNeighbors in interface Environment
Returns:
the number of adjacent neighbors

randomDirection

public Direction randomDirection()
Generates a random direction. The direction returned by randomDirection reflects the direction from a cell in the environment to one of its adjacent neighbors.
Specified by:
randomDirection in interface Environment
Returns:
a direction

getDirection

public Direction getDirection(Location fromLoc,
                              Location toLoc)
Returns the direction from one location to another. If fromLoc and toLoc are the same, getDirection arbitrarily returns Direction.NORTH.
Specified by:
getDirection in interface Environment
Parameters:
fromLoc - starting location for search
toLoc - destination location
Returns:
direction from fromLoc to toLoc

getNeighbor

public Location getNeighbor(Location fromLoc,
                            Direction compassDir)
Returns the adjacent neighbor (whether valid or invalid) of a location in the specified direction.
Specified by:
getNeighbor in interface Environment
Parameters:
fromLoc - starting location for search
compassDir - direction in which to look for adjacent neighbor
Returns:
neighbor of fromLoc in given direction (whether valid or not)

neighborsOf

public java.util.ArrayList neighborsOf(Location ofLoc)
Returns the adjacent neighbors of a specified location. Only neighbors that are valid locations in the environment will be included.
Specified by:
neighborsOf in interface Environment
Parameters:
ofLoc - location whose neighbors to get
Returns:
a list of locations that are neighbors of ofLoc

AP® Computer Science Marine Biology Simulation

Copyright© 2002 College Entrance Examination Board