Marine Biology Simulation
Case Study

Supplemental Example:
Forward-Moving Fish


Problem Specification

Consider adding forward-moving fish (ForwardFish) to the Marine Biology Simulation program. A forward-moving fish can move forward or forward and slightly to the left or right. For example, a fish facing north could move north, northeast, or northwest. A fish facing east could move northeast, east, or southeast. If a fish moves directly forward, its direction should be unchanged. A fish that turns as it moves should change its direction 90 degrees to the right or left. For example, if a north-facing fish moves northeast, it should turn to face east. If an east-facing fish moves northeast, it should turn to face north. [Remember that locations in a bounded environment have only four adjacent neighbors, and that the getDirection method in such an environment will "round" directions to one of the four cardinal directions in a square environment.]

 

Variant: You can modify the BoundedEnv constructor to create a bounded environment in which a location has eight adjacent neighbors (four sides and four diagonals) rather than four.  BoundedEnv extends the SquareEnvironment class, which provides the implementations for methods such as getDirection, getNeighbor, and neighborsOf.  The BoundedEnv constructor calls super() to construct the SquareEnvironment aspects of the object using the default SquareEnvironment constructor.  It could use the other SquareEnvironment constructor, though, and create an environment that looks for neighbors on the diagonals.  Research the second SquareEnvironment constructor and then modify the BoundedEnv constructor to pass parameters to super.  This will lead to a different solution to the forward-moving fish problem.

 

Extension: Notice that forward-moving fish get stuck at the sides of the environment. Modify the ForwardFish class so that a fish that is stuck reverses direction.