| CIT 591 Ninth Java Assignment: Wator David Matuszek, Fall 2002 |
Purposes:
The idea:
"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 Start button
is pressed.
Start, to start the simulation running.
Stop, to pause the simulation. When it is stopped, pressing
Start will 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 controller
The class you create that extends Applet should be controller.
Your applet's 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.
New button should call an initialization
method in the model.Start button should call a method in the
model that runs the simulation.Stop button 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).The model
Notice that tuna and sharks share a lot of the same behaviours. Define a class
Fish with subclasses Tuna and Shark,
and implement the necessary methods for movement and reproduction in Fish.
Fish class should include an abstract boolean method
okToMove (with whatever parameters you find convenient) which
you implement in both Tuna and Shark (because a
tuna can't move to a square containing another tuna, but a shark can).Fish class should implement a method move
to move the actual fish.
move method should be inherited and used without change
by the Tuna class to move the tuna.move method should be overridden in the Shark
class to do the one extra thing that a shark might do (eat a tuna), but
the move method in the Shark class should call
the move method with the same signature in the Fish
class to do all the rest of the work involved in movement. (Hint: use
super). 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.
The view
The View class should extend Canvas and implement
Observer, and should have a paint(Graphics) method.
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 RabbitHunt
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 Canvas.
Due date: Friday, December 6.