|CIT 591 Ninth Java Assignment: Wator
David Matuszek, Fall 2002
"Wator" (a misspelling of "water") is a classic predator-prey simulation. In this simulation, there are sharks and tuna. Sharks move, eat tuna, and reproduce; they might starve to death. Tuna move and reproduce, and they always have enough to eat. Neither tuna nor sharks ever die of old age.
In this simulation, you will display an N by N array representing the ocean, and markers of two different sizes representing the sharks and the tuna.
Details of program behavior:
Implement this program as an applet.
The ocean is an NxN array, where N is an input parameter from the HTML to the Applet. Each location in the array can be empty, or it can hold one tuna or one shark (but not both). At the beginning of the simulation, put the tuna and sharks in random locations in the ocean.
The following parameters should be set from the GUI:
The GUI should have these buttons
New, to create a new simulation based on GUI parameters. The simulation should not start running until the
Startbutton is pressed.
Start, to start the simulation running.
Stop, to pause the simulation. When it is stopped, pressing
Startwill cause the simulation to continue from the point at which it was stopped.
A scrollbar to control the speed of the simulation would be nice, but is optional.
The simulation progresses in a series of "steps." At each step, each tuna will:
At each step, each shark will
Details of program implementation:
The program should be written using the Model-View Controller design pattern. Put the view and the controller each in their own separate class; the model part will consist of more than one class.
The class you create that extends
Applet should be controller.
init method should create model and view objects,
set up and display the GUI, attach listeners to the buttons; once it has done
that, its job is done.
Newbutton should call an initialization method in the model.
Startbutton should call a method in the model that runs the simulation.
Stopbutton should call a setter method in the model to set a variable; the model should check this variable from time to time, to see whether it should pause the simulation (you can pause by returning from the method that is running the simulation).
Notice that tuna and sharks share a lot of the same behaviours. Define a class
Fish with subclasses
and implement the necessary methods for movement and reproduction in
Fishclass should include an abstract boolean method
okToMove(with whatever parameters you find convenient) which you implement in both
Shark(because a tuna can't move to a square containing another tuna, but a shark can).
Fishclass should implement a method
moveto move the actual fish.
movemethod should be inherited and used without change by the
Tunaclass to move the tuna.
movemethod should be overridden in the
Sharkclass to do the one extra thing that a shark might do (eat a tuna), but the
movemethod in the
Sharkclass should call the
movemethod with the same signature in the
Fishclass to do all the rest of the work involved in movement. (Hint: use
You can, of course, implement any other classes that you need. Note that at
least one of your classes--the one in charge of making sure every fish has a
chance to do something--should extend
Observable, and should notify
observers when it is time to redraw the ocean.
View class should extend
Canvas and implement
Observer, and should have a
This method draws the ocean (with grid lines or not, as you choose) with tuna
and sharks in it.
You are welcome to borrow any code you find helpful from my
program. However, you should be aware of one problem in that program: since
I did not properly extend the
Canvas class, there are times when
the screen does not redraw properly. You can avoid this particular problem my
making sure your
View class extends
Due date: Friday, December 6.