The Minnow program simulates three small fish, the minnows, swimming in a small, bounded environment such as a pond or aquarium. The small, bounded environment is represented as a rectangular grid of cells. Through a graphical user interface, the user can control how the program is run, stepping through it one step at a time or running it for many steps until the Stop button is pressed. In each step, the three minnows move one cell.
The exercises in this project are designed to introduce you to the
Minnow class, which represents the small, simple fish swimming in
the environment. You will be modifying the Minnow class to
improve the way minnows move. To do this you will use methods from three
other classes: Environment, Location, and
Direction. An Environment object represents the
environment in which minnows swim, a Location object represents the
row and column of a specific cell in the environment, and a
Direction object represents a direction, such as north, south,
east, or west. Each minnow in the environment is at a particular location
and facing a certain direction.
Environment provides two kinds of methods. One set deals
with the objects in the environment, such as numObjects (how many
objects are there?), allObjects (get me all the objects),
isEmpty (is there an object at a particular location?), and
objectAt (get me the object at a particular location).
Environment also provides methods for navigating around the
environment; for example, a minnow at a particular location can ask the
environment what location is to its north or south (getNeighbor),
or what direction it needs to go to get to another location
(getDirection). The exercises in this project will focus on
using the getDirection, getNeighbor, and
isEmpty methods.
Some of the classes in the Minnow project, including
Environment, Location, and Direction,
come directly from the AP® Marine Biology Simulation case
study. Others, such as Minnow, are based on classes in the
case study.†
[Educational prerequisites for this project: Students should be familiar with reading class documentation, constructing objects and invoking methods, the format of a class implementation (instance variables and methods), the basic flow control constructs (conditions and loops), and reading class documentation. Students should also be familiar with the
add,get,remove, andsizemethods of theArrayListclass.]
Exercise 1 — Running the program:
|
A Minnow object has instance variables to keep track of the
environment in which it lives, its location and direction in that environment,
and its color. Its constructor initializes those variables. It also
provides accessor methods that allow client code to find out those values — what
environment the minnow lives in, what its location or direction is, or what
color the minnow is. Its most important method, though, is the
act method, because that is the method that gets called in each
timestep. When the user clicks on the step or run buttons, the program
asks each object in the environment to "act." For a minnow, acting means
to move, so the act method calls an internal move
method.
The move method calls another internal method,
nextLocation, to determine the next location to which the minnow
should move. If the minnow is blocked, nextLocation returns
the minnow's current location, indicating that it can't move. If the
next location is not equal to the current location, the minnow moves
there (calling changeLocation), possibly changing direction in the
process. The minnow's new direction is determined by asking the
environment what direction the minnow had to swim to get from its old location
to its next location. (The separate changeLocation method handles
updating the minnow's location and notifying the environment of the move.)
Exercise 2 — Understanding the code:
|
In the next exercise you will modify the Minnow class so that a minnow moves forward if possible; otherwise it turns around and moves in that direction.
Exercise 3 — Turning around:
|
In the next exercise you will modify the Minnow class so that a
minnow moves forward, right, left, or backward, if possible. You may
search the locations in the four directions in any order to find one that is
empty.
Exercise 4 — Rotating:
|
In the next exercise you will modify the Minnow class to make
use of the emptyNeighbors helper method. A minnow will still
move forward, right, left, or backward, if possible, but now you should use a
location returned by emptyNeighbors.
Exercise 5 — Using
|
In the next exercise you will modify the Minnow class to
randomly choose an empty location to which to move.
Exercise 6 — Random behavior
|
In the next exercise you will modify the Minnow class to prevent
minnows from moving backward or turning completely around in one step.
Exercise 7 — No turning back
|
After completing these exercises, you are ready to explore the
Fish class in the Marine Biology Simulation (MBS) case study.* When
moving on to the MBS case study, notice the following differences between the
Minnow and Fish classes:
Fish class has more instance variables and more
constructors. (See pp. 27 - 30 of the MBS case study narrative.)
Fish check that they are still in the environment before
acting. (p. 33)
Fish methods use the changeDirection helper
method and the environment, location, and
direction accessor methods. (p. 39)
Fish helper methods are protected, not private.
(pp. 30, 65)