CSE 142 Winter 2002Homework #4Virtual ForestFinal project due: Electronically, 11:00pm,
Tue., Feb. 19;
|
You will be given some code that partially implements a simulation of a growing forest, and your job is to complete it.
Download the following two files: Forest.java and Tree.java. Place them in a folder (none of the folders in the path name to these files should have a space or any special character (such as '#') in them) and create a BlueJ project from them. Here is a brief description of the two classes.
This class implements a simple graphical tree (similar to the one on a recent quiz). Besides the constructor and standard graphics package methods, there are methods to "grow" the tree (make it larger).
This class is intended to represent a forest (that is, a collection (hint) of trees). It will eventually implement a simulation of the forest according to the rules below.
The basic idea behind animation is as follows: 1) display an image, 2) pause, 3) display a slightly modified image, 4) repeat. If the images are close enough to each other, and the pause is short enough, the eyes are tricked into thinking the images are moving. We will be simulating a growing forest by repeatedly displaying the forest, pausing, growing the forest a small amount, and then redisplaying it again.
We will be simulating a graphical forest, which works according to the following rules:
As noted above, we've provided a Tree class, which implements a graphical tree. The constructor takes the coordinates and initial height of the tree, as well a growth rate. It has a method to grow (resize) the tree based on the growth rate, which works according to the rules above. However, there is no code to deal with reproduction, so you may need to introduce instance variables and methods in class Tree to be used by your Forest class.
The Forest class is basically empty, except for the declarations of the two functions you must implement (a no-argument constructor and the simulate()) method. The constructor must create the initial set of trees as described in the rules and display them in a GWindow. The simulate() method, when called, must run the simulation. That is, it must display the initial forest, then apply the rules for the first year, and redisplay the forest. It should do this repeatedly until the simulation ends as in the rules.
We have provided two utility functions for you to use. Unless you are truly inclined to, do not attempt to figure out what they do -- simply use them based on their comments. The method random() takes two integers and returns a random integer between the two, inclusive. The method pause() will pause the program for 1/4 second. You should call the pause() method between each year in your simulation.
Besides implementing the constructor and simulate() methods, you may (and probably must) define your own instance variables and methods for your own use. You are free to do what you wish in these methods.
The only real hint is develop this assignment incrementally. Add a bit of code and test it out before continuing. Here are suggestions of how you can procede.
First, just write a very simple constructor for Forest which displays a single tree on the screen. This will get you used to some of the functions we've provided.
Expand your constructor using a simple loop so that it produces the initial set of trees in the forest. Test your code.
Now try implementing a simple loop to call the growTree() methods on each tree in the forest. You must decide whether it's better to put this in simulate() or in a method of your own. You might try testing this by trying to grow the forest a few times with pauses in between.
Now try adding a loop in simulate to continually grow the trees, with pauses in between. Test your code.
Now try adding the death condition to the loop that grows the trees. Test your code.
Finally, add the reproduction condition to the growing loop, and implement the condition to end the simulation. Test your code.
When working with the simulation, it may be necessary to stop the program currently running. To do this, click the red candy-cane like button in BlueJ. This brings up the debugger. Click Stop, then Terminate, and close any open GWindows.
It might be useful to look at the image below. This is a screen capture of the sample solution in the middle of execution. Note the trees at differing heights and the dead trees.
For full credit you should use Iterators when you want to process the list of trees or any other list. That is, use an Iterator object and its hasNext() and next() methods to access the elements of a list; don't use get() to access the elements by index if you are processing the whole collection.
One complication: If you are using an Iterator to process a list, you may not alter the list until you are done processing the entire list. If, say, you try to add() new trees to the list of trees while you are in the middle of iterating through that list, you will get an error. That is a complication, since you eventually need to add new trees to the ArrayList inside class Forest to make trees reproduce.
A solution to this is to have a second list of "new" trees. Instead of adding new trees directly to the forest, add them to this second list. Then, when you've processed all of the existing trees, go through this second list and add as many of them as possible to the forest. (Remember that the forest can have at most 50 trees. Any additional trees that are created by reproduction are crowded out, fail to take root, and are not added to the forest.)
The turn-in page will accept a Forest.java file, and a Tree.java file.
You will be graded on two things: the correctness of your program (i.e., how well you followed directions, and how well your program does what we asked), and on the clarity of your program (i.e., how readable it is, and how well it communicates the intent of what you were trying to accomplish to the human reader). Clarity will benefit from good explanatory comments, good choice of variable names, good use of local names (if needed), and good use of indentation to group things.
Electronically turn in your Forest.java and Tree.java files. Turn-in form
In lecture on Wednesday, February 20, you must turn in a short (no more than one page) written report about your project. Include your name, section, homework number and date at the top. Briefly describe what code you wrote to complete the assignment and the problems you faced. Did your first idea work? If not, describe what did not work. Describe the most important things you learned from this project, which could be new ideas that you have never seen before, or misconceptions that you had that the project helped you straighten out.
End of Project