CSE 142 Winter 2002

Homework #4

Virtual Forest

Final project due: Electronically, 11:00pm, Tue., Feb. 19;
written report due in lecture, Wed., Feb. 20.


Purpose

The purpose of this assignment is to

You will be given some code that partially implements a simulation of a growing forest, and your job is to complete it.

Getting Started

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.

Tree

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).

Forest

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.

A Note on Animation

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.

The Simulation

We will be simulating a graphical forest, which works according to the following rules:

  1. Each pixel on the screen represents one foot.
  2. The forest begins with a random number of trees between 4 and 10, inclusive. All trees are 50 feet high to begin with, and 10 feet wide throughout their lives.
  3. When they are created, each tree is given a random growth rate between 2 and 10 feet, inclusive.
  4. Each "year" (step in the animation), every tree in the forest grows by the number of feet in its growth rate.
  5. Once a tree reaches 200 feet, it can reproduce to produce a new tree. See the reproduction conditions below.
  6. Trees grow across the screen from left to right, one right next to each other.
  7. Once the screen has filled with trees (at most 50 trees), no new trees can be created ever again.
  8. Once a tree reaches 300 feet, it dies and its trunk and crown turn to black.
  9. The simulation ends when there are no more live trees.
Trees reproduce as follows:
  1. Each tree starts out with two "spores".
  2. In any simulation year, any trees that have grown taller than 200 feet with spores left reproduce, which produces one new tree.
  3. When a tree reproduces, it loses one spore.
  4. Each tree may reproduce at most once in a given year, and at most twice in its lifetime.  (Trees reproduce twice in their lifetime unless the simulation ends before they get a chance to do so or if there is no space left in the forest.)

What We've Given You, and What You Need to Implement

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.

Hints and Suggestions

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.

Use Iterators

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.)

Turning in the Complete Project

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

Written Report

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