The Rabbit Problem

You are working with a team of scientists studying the rabbit population on Iowa State campus. Of utmost concern is the number of rabbits taxing the campus vegetation, and you have been asked to develop a Java class that can model the population in a simple simulation.

At its simplest, a model will have an instance variable representing the size of the population, and the population can be observed by calling a method getPopulation. The passage of time is indicated by calling a method simulateYear. Each year, some rabbits are born and others die, resulting in a new population at the end of the year.

As you will see in the next step, a simple plotting tool has already been developed. It will make a chart of the population over the simulated years using your model.

Specification for RabbitModel

The class RabbitModel has the following specification: Based on just the description above, we can create a minimal skeleton of a RabbitModel with the methods and constructor stubbed in and documented. Take a look at RabbitModel.java and add it to your lab2 package.

Our first model

For the first example, we will create a very simple model in which To implement this simple model, your class really only needs one instance variable, representing the current population of rabbits.
  1. Add a private int instance variable to your RabbitModel.
  2. In the constructor, initialize this variable to 2.
  3. In the simulateYear method, increment this variable by 1 to simulate the passing of one year.
  4. In the reset method, set this variable back to the initial value 2. (Note that this reset always does the exact same work as the constructor, so in more complex models you can save yourself some effort by just calling reset in your constructor.)

More generally, the simulateYear method could alter the population according to weather, pesticide usage, traffic, predators, butterflies in Brazil, etc. For the checkpoints, you will revise this code to create more complex models.

Unit testing the model

Before continuing, see if the model you created seems to be working correctly all by itself. This is much easier than trying to fix errors when you're trying to integrate your code with an existing library or framework.

For example, you could try something like this:

public class TestModel
{
  public static void main(String[] args)
  {
    RabbitModel model = new RabbitModel();

    // Check that the initial population is 2
    System.out.println(model.getPopulation());
    System.out.println("Expected 2");

    // A year goes by...
    model.simulateYear();
    System.out.println(model.getPopulation());
    System.out.println("Expected 3");

    // Start over
    model.reset();
    System.out.println(model.getPopulation());
    System.out.println("Expected 2");
  }
}