Question: package lab2; /** * A RabbitModel is used to simulate the growth * of a population of rabbits. */ public class RabbitModel { // TODO

package lab2; /** * A RabbitModel is used to simulate the growth * of a population of rabbits. */ public class RabbitModel { // TODO - add instance variables as needed /** * Constructs a new RabbitModel. */ public RabbitModel() { // TODO } /** * Returns the current number of rabbits. * @return * current rabbit population */ public int getPopulation() { // TODO - returns a dummy value so code will compile return 0; } /** * Updates the population to simulate the * passing of one year. */ public void simulateYear() { // TODO } /** * Sets or resets the state of the model to the * initial conditions. */ public void reset() { // TODO } }

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

The class RabbitModel has the following specification:

  • Contains a method named getPopulation that returns an int representing the number of rabbits alive at the end of the current year of the simulation. It takes no arguments.
  • Contains a method named simulateYear that 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 reset which initializes any instance variables to their values at the start of the simulation. It takes no arguments.
  • Is in the lab2 package.

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!