|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||
java.lang.Object | +--SquareEnvironment
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.
Direction,
Location| Constructor Summary | |
SquareEnvironment()
|
|
| Method Summary | |
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 |
isValid(Direction dir)
Verifies whether a direction 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). |
Direction |
randomDirection()
Generates a valid random direction. |
int |
turningAngle()
Returns the turning angle for turning in this environment (e.g., 45, 60, 90, 120, 180) (will not be tested on the Advanced Placement exam). |
| 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 |
public SquareEnvironment()
| Method Detail |
public int numCellSides()
numCellSides in interface Environmentpublic int turningAngle()
turningAngle in interface Environmentpublic boolean isValid(Direction dir)
isValid in interface Environmentdir - direction to checktrue if dir is valid;
false otherwisepublic Direction randomDirection()
randomDirection in interface Environment
public Location getNeighbor(Location fromLoc,
Direction compassDir)
isValid(compassDir).)getNeighbor in interface EnvironmentfromLoc - starting location for searchcompassDir - direction in which to look for nearest neighborfromLoc in given direction
public Direction getDirection(Location fromLoc,
Location toLoc)
toLoc is not a "straight shot" from
fromLoc, then the direction will be rounded to a valid
direction in this environment. If fromLoc and
toLoc are the same, getDirection arbitrarily
returns Direction.NORTH. The table below shows examples.
fromLoc toLoc value returned by getDirection(fromLoc, toLoc)
------- ----- ----------------------------------------------
(1, 1) (0, 1) Direction.NORTH
(1, 1) (1, 2) Direction.EAST
(1, 1) (2, 6) Direction.EAST (slightly south, mostly east)
(0, 0) (1, 1) Direction.SOUTH (southeast, but rounds to south)
(1, 1) (1, 1) Direction.NORTH
One can use directions returned by getDirection
to move from one location to another in an environment, even
if the direction is not a "straight shot." For example,
getDirection from (0, 0) to (1, 1) returns
Direction.SOUTH. Moving one cell south means going to
location (1, 0). A second call to getDirection,
from (1, 0) to (1, 1) returns Direction.EAST. Moving
one cell east leads to the destination, (1, 1). Similarly, the
path from (1, 1) to (2, 6), following repeated calls to
getDirection from the intermediate cells, is
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 5), (2, 6)].
getDirection in interface EnvironmentfromLoc - starting location for searchtoLoc - destination locationfromLoc to toLoc,
rounded if necessarypublic java.util.ArrayList neighborsOf(Location ofLoc)
neighborsOf in interface EnvironmentofLoc - location whose neighbors to getofLoc
|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||