CS324e - Assignment 7

Placed Online: Wednesday, November 14
Due: Thursday, November 29, no later than 11 pm
Points: 100, about 10% of the final grade
Program Shell: WatorMain.java, WatorFrame.java, GraphPanel.java, WatorPanel.java. You also need the code that handles the back end of the simulation. A7.jar

Pair Assignment. This is an pair assignment. You may complete the assignment with one other person from the class or you may complete it by yourself.  You may use the examples shown in class as a guide, but you may not get help from anyone besides your partner, the instructor, and the TA. Copying code or getting code from anyone besides the the instructor or TA will result in an F in the course. You can discuss approaches to the problem with others as long as you don't write code or look at code. You may get help on syntax errors from others.

If you are working with a partner turn in one version of the assignment to either partner's turnin account. If you work with a partner, both student's in a pair will get the same grade on the assignment.

The purpose of the assignment is to create a program with a 2D animation with multiple controls. You will be writing the code to display the present state of the world, show the animation of the world as the simulation runs, plot the number of sharks and fish over time, and create some of the controls for the GUI. The code that handles the simulation is complete.


 In this assignment you will use the same framework we have used in class and on assignments. I am providing a shell of the program (contained in multiple classes) with some code already completed.

Implement a program that displays the world from a Wa-tor World Simulation. The world is based on an article by A.K. Dewdney in Scientific American entitled Sharks and Fish Wage an Ecological War on the Toroidal Planet Wa-Tor. This is a biological simulation that takes place on a world covered with an ocean. The world is a torus (like a doughnut) not a sphere. The world is divided into cells or locations. Every location has four adjacent locations: above, below, left, and right.

The only two animals in the world are fish and sharks.

Fish eat microscopic kelp. The supply of kelp is never exhausted. Every turn of the simulation a fish will move to a random adjacent cell unless all four are occupied. If the fish has survived the number of turns necessary to breed it produces a new fish if there is an empty adjacent cell. (This is all handled by the simulation.)

Sharks eat fish. Each turn if there is a fish adjacent to a shark the shark eats it. If there are multiple adjacent fish the shark eats one at random. If there are no adjacent fish the shark moves in the same manner as fish. After eating or moving if the shark has survived the number of turns necessary to breed it produces a new shark if there is an empty adjacent cell. If a shark does not eat in a certain number of turns in starves.

You will write a program that display a graph of the fish and shark population over time, the current status of the world, and controls for the simulation. The program is animated to show the simulation in action.

The shell consists of 4 files / classes: WatorMain.java, WatorFrame.java, GraphPanel.java, WatorPanel.java

The code that handles the simulation itself is in a jar file. (Java Archive) named A7.jar. Download A7.jar to your computer. To add the jar to Eclipse follow the steps on this page.

Here is the documentation for the classes that handle the simulation. Your code shall only interact with the WatorWorld class (and possibly the Fish and the Shark classes if your added control alters some aspect of those classes such as breed and starve times)


There is a lot to do in this assignment. Add instance variables and /or  methods to the WatorFrame, WatorPanel, and GraphPanel classes as necessary. The WatorFrame holds the GraphPanel at the top, the WatorPanel in the middle, and the control panel at the bottom. The control panel is part of the WatorFrame, not a separate class.

Here is an overview of the items you must complete. This is a summary. Be sure to use good coding style, constants and helper methods to help solve the problem. Good style and constants make it easier to understand the program and easier to make changes to the program if desired.

1. Add the Start, Stop, and Reset buttons. Do this in the addControlPanel method or create a helper method which is called from addControlPanel. The purpose of the buttons are:

2. Finish the addTimer method in the frame class. When the timer goes off the WatorPanel and GraphPanel shall be updated. (There is a method named update in the GraphPanel and WatorPanel classes. There is a small bit of work to do in each of these methods. At the end of these methods call the repaint method, which will in turn cause a call to paintComponent.)

3. Next finish the WatorPanel class. You must finish the constructor. It is important to call the setPreferredSize method on the panel based on the world size and the number of pixels you want to each cell to take up. In the screenshots I have each cell set to 5 by 5, but you can use different values or vary it based on the size of the world. You can also change the parameters for the world. (Good place for constants.)

5. Finish the WatorPanel update and paintComponent methods. You must call the step method on theWorld variable to have the simulation perform one "turn" and then call repaint. paintComponent shall fill all the rectangles in the panel for every cell in theWorld. You should use a nested loop to generate the coordinates of all the rows and columns in theWorld and call the getColor method from theWorld for each of these. There are other options for drawing the world and you are free to take a different approach. You may use ellipses instead of rectangles if you wish.

6. The GraphPanel displays the fish and shark populations over time. There are methods in the WatorWorld class to get the current number of fish and sharks.

Time is the horizontal axis and number of fish and sharks is the vertical axis. The number of fish is plotted in green. The number of sharks is plotted in orange. For each step in the simulation redraw the graph of population. You may redraw the enitre graph each time or save a BufferedImage and add new data points and lines to it. Scale the values based on the total number of cells in the world. So if the world is all fish, then the green line would be at the very top and the orange line would be at the very bottom. The picture below shows what happens if all the sharks die out and the fish breed to fill the world.

There are many ways of plotting the population over time,. One would be to store the populations in two ArrayLists and call drawLine many between each point. When the size of the ArrayList is greater than the width of the GraphPanel clear the ArrayList and start adding numbers again. (or scroll the graphs a little ways from the end. Leave at least 10% empty space at the end if you take this approach.) Another option would be to create a generalPath object for each of the population lines and then just draw those in each call to render. do not just plot individual points. The points must be connected.

7. The last thing is to add one more control that changes some aspect of the simulation. Add a button or slider to the control panel to alter some aspect of the simulation. This could include changing the breed time for fish, the breed time for sharks, or the  starve time for sharks. Other options would be to make controls for the number of fish and / or sharks to start with when the simulation is reset. You may also add controls of your own design as long as they alter the parameters of the simulation in some way. You cannot alter the WatorWorld, Fish, or Shark classes. You must use the methods that are currently available in those classes.

The frame cannot be resized. However, it should be easy to change the size of the panel or the size of the world or both and rerun the program. (The size of the world would be another control you could add.)


When you complete the assignment fill in the header information at the top (Eclipse sometimes collapses this comment, so be sure to expand it.) and turn in your WatorMain.java with the header filled in and the completed WatorFrame.java, WatorPanel.java, and GraphPanel.java files using the turnin program. This page contains instructions for using turnin. Do not upload theA7.jar file. Be sure you upload your files to your CS324e folder.