Interface Environment

All Known Implementing Classes:
SquareEnvironment

public interface Environment

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.

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 nearest neighbor of a location in the specified direction.
 boolean isEmpty(Location loc)
          Determines whether a specific location in this environment is empty.
 boolean isValid(Direction dir)
          Verifies whether a direction is valid in this environment.
 boolean isValid(Location loc)
          Verifies whether a location is valid in this environment.
 java.util.ArrayList neighborsOf(Location ofLoc)
          Returns the immediately adjacent neighbors of a specified location.
 int numCellSides()
          Returns the number of sides around each cell (will not be tested on the Advanced Placement exam).
 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 valid 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.
 int turningAngle()
          Returns the turning angle for turning in this environment, (for example, 45, 60, 90, 120, 180) (will not be tested on the Advanced Placement exam).
 

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

numCellSides

public int numCellSides()
Returns the number of sides around each cell (will not be tested on the Advanced Placement exam).
Returns:
the number of cell sides in this environment

turningAngle

public int turningAngle()
Returns the turning angle for turning in this environment, (for example, 45, 60, 90, 120, 180) (will not be tested on the Advanced Placement exam). The turning angle is related to the number of sides to each cell; namely, turningAngle() * numCellSides() == 360.
Returns:
the turning angle in degrees

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

isValid

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

randomDirection

public Direction randomDirection()
Generates a valid random direction.
Returns:
a valid direction

getNeighbor

public Location getNeighbor(Location fromLoc,
                            Direction compassDir)
Returns the nearest neighbor of a location in the specified direction. (Precondition: isValid(compassDir).)
Parameters:
fromLoc - starting location for search
compassDir - direction in which to look for nearest neighbor
Returns:
neighbor of fromLoc in given direction

getDirection

public Direction getDirection(Location fromLoc,
                              Location toLoc)
Returns the direction from one location to another. If toLoc is not a "straight shot" from fromLoc, then the direction will be rounded to a valid direction in this environment.
Parameters:
fromLoc - starting location for search
toLoc - destination location
Returns:
direction from fromLoc to toLoc, rounded if necessary

neighborsOf

public java.util.ArrayList neighborsOf(Location ofLoc)
Returns the immediately 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