DarterFish

A darter fish darts two cells forward if both the first and second cells in front of it are empty. If the first cell is empty but the second cell is not, then the darter fish moves forward only one space. If the first cell is not empty, then the darter reverses its direction but does not change its location.

Design Issues

Although normal fish and darters exhibit different behavior when moving, they also share many similarities. For example, the id, color, location, and direction accessor methods have nothing to do with the particular type of fish being modeled. Rather than repeat the code for all the methods with the same behavior, I wanted my DarterFish class to extend Fish. My DarterFish objects would inherit certain data and behavior defined in the Fish superclass, but redefine other behavior by providing new implementations in the subclass.

I decided that my first step would be to modify the nextLocation method, which defines how the fish chooses where to move.

Programming Exercise: DarterFish, Step 1

Changing Direction

So far, my fish were correctly determining where to move when they could do so, and the move method inherited from the Fish class was correctly moving them there. What it wasn't doing, though, was changing direction when the fish could not move.

When darters can't move forward, they should reverse direction without moving. Darter fish could also be called "pacer fish" because they pace back and forth.

I looked again at the move method in the Fish class, and confirmed that this was the method I needed to redefine to correctly implement changing direction.

Programming Exercise: DarterFish, Step 2

Analysis Questions: Testing DarterFish

  1. Each of the five rows of east-facing and west-facing fish in testDarters.dat could be considered a different test case, and each south-facing fish could also be considered a different test case, for a total of 9 test cases. What conditions do each of these test cases test? What is the difference between the test of the 3 east/west-facing fish in row 7 and the two east/west-facing fish in row 9? (Remember that rows start at 0, so row 7 is the eighth row.)


Alyce Brady, Kalamazoo College