AP® Computer Science Marine Biology Simulation

Interface Environment

All Known Implementing Classes:
SquareEnvironment

public interface Environment

AP® Computer Science Marine Biology Simulation:
Environment provides an interface for a two-dimensional, grid-like environment containing locatable objects. For example, it could be an environment of fish for a marine biology simulation.

The Environment interface is copyright© 2002 College Entrance Examination Board (www.collegeboard.com).

Version:
1 July 2002
See Also:
Direction, Locatable, Location

Method Summary
 void add(Locatable obj)
          Adds a new object to this environment at the location it specifies.
 Locatable[] allObjects()
          Returns all the objects in this environment.
 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.
 boolean isEmpty(Location loc)
          Determines whether a specific location in this environment is empty.
 boolean isValid(Location loc)
          Verifies whether a location is valid in this environment.
 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.
 int numCols()
          Returns number of columns in this environment.
 int numObjects()
          Returns the number of objects in this environment.
 int numRows()
          Returns number of rows in this environment.
 Locatable objectAt(Location loc)
          Returns the object at a specific location in this environment.
 Direction randomDirection()
          Generates a random direction.
 void recordMove(Locatable obj, Location oldLoc)
          Updates this environment to reflect the fact that an object moved.
 void remove(Locatable obj)
          Removes the object from this environment.
 

Method Detail

numRows

public int numRows()
Returns number of rows in this environment.
Returns:
the number of rows, or -1 if the environment is unbounded

numCols

public int numCols()
Returns number of columns in this environment.
Returns:
the number of columns, or -1 if the environment is unbounded

isValid

public boolean isValid(Location loc)
Verifies whether a location is valid in this environment.
Parameters:
loc - location to check
Returns:
true if loc is valid; false otherwise

numCellSides

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

numAdjacentNeighbors

public int numAdjacentNeighbors()
Returns the number of adjacent neighbors around each cell.
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.
Returns:
a direction

getDirection

public Direction getDirection(Location fromLoc,
                              Location toLoc)
Returns the direction from one location to another.
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.
Parameters:
fromLoc - starting location for search
compassDir - direction in which to look for adjacent neighbor
Returns:
neighbor of fromLoc in given direction

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.
Parameters:
ofLoc - location whose neighbors to get
Returns:
a list of locations that are neighbors of ofLoc

numObjects

public int numObjects()
Returns the number of objects in this environment.
Returns:
the number of objects

allObjects

public Locatable[] allObjects()
Returns all the objects in this environment.
Returns:
an array of all the environment objects

isEmpty

public boolean isEmpty(Location loc)
Determines whether a specific location in this environment is empty.
Parameters:
loc - the location to test
Returns:
true if loc is a valid location in the context of this environment and is empty; false otherwise

objectAt

public Locatable objectAt(Location loc)
Returns the object at a specific location in this environment.
Parameters:
loc - the location in which to look
Returns:
the object at location loc; null if loc is not in the environment or is empty

add

public void add(Locatable obj)
Adds a new object to this environment at the location it specifies. (Precondition: obj.location() is a valid empty location.)
Parameters:
obj - the new object to be added
Throws:
java.lang.IllegalArgumentException - if the precondition is not met

remove

public void remove(Locatable obj)
Removes the object from this environment. (Precondition: obj is in this environment.)
Parameters:
obj - the object to be removed
Throws:
java.lang.IllegalArgumentException - if the precondition is not met

recordMove

public void recordMove(Locatable obj,
                       Location oldLoc)
Updates this environment to reflect the fact that an object moved. (Precondition: obj.location() is a valid location and there is no other object there. Postcondition: obj is at the appropriate location (obj.location()), and either oldLoc is equal to obj.location() (there was no movement) or oldLoc is empty.)
Parameters:
obj - the object that moved
oldLoc - the previous location of obj
Throws:
java.lang.IllegalArgumentException - if the precondition is not met

AP® Computer Science Marine Biology Simulation

Copyright© 2002 College Entrance Examination Board