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:
- Contains a method named
getPopulationthat returns anintrepresenting the number of rabbits alive at the end of the current year of the simulation. It takes no arguments. - Contains a method named
simulateYearthat simulates the passage of a year. This method updates the number of rabbits alive at the end of the year, and adjusts any additional state (instance variables) of the model. It takes no arguments. - Contains a constructor taking no arguments.
- Contains a method named
resetwhich initializes any instance variables to their values at the start of the simulation. It takes no arguments. - Is in the
lab2package.
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- The population is initially two,
- one new rabbit is born each year, and
- no rabbits ever die.
- Add a private
intinstance variable to yourRabbitModel. - In the constructor, initialize this variable to 2.
-
In the
simulateYearmethod, increment this variable by 1 to simulate the passing of one year. -
In the
resetmethod, set this variable back to the initial value 2. (Note that thisresetalways does the exact same work as the constructor, so in more complex models you can save yourself some effort by just callingresetin 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");
}
}