Java Reference
In-Depth Information
The Rabbit class contains a number of class variables that define configuration settings that are
common to all rabbits. These include values for the maximum age to which a rabbit can live (defined
as a number of simulation steps) and the maximum number of offspring it can produce at any one
step. Centralized control of random aspects of the simulation is provided through a single, shared
Random object supplied by the Randomizer class. This is what makes possible the repeatability
seen in Exercise 10.8. In addition, each individual rabbit has four instance variables that describe its
state: its age as a number of steps, whether it is still alive, and its location in a particular field.
Exercise 10.10 Do you feel that omitting gender as an attribute in the Rabbit class is likely
to lead to an inaccurate simulation? Write down the arguments for and against including it.
Exercise 10.11 Are there other simplifications that you feel are present in our implementa-
tion of the Rabbit class, compared with real life? Discuss whether these could have a signifi-
cant impact on the accuracy of the simulation.
Exercise 10.12 Experiment with the effects of altering some or all of the values of the class
variables in the Rabbit class. For instance, what effect does it have on the populations if the
breeding probability of rabbits is much higher or much lower than it currently is?
A rabbit's behavior is defined in its run method, which in turn uses the giveBirth and
incrementAge methods and implements the rabbit's movement. At each simulation step, the
run method will be called and a rabbit will increase its age; if old enough, it might also breed,
and it will then try to move. Both the movement and the breeding behaviors have random
components. The direction in which the rabbit moves is randomly chosen, and breeding occurs
randomly, controlled by the class variable BREEDING_PROBABILITY .
You can already see some of the simplifications that we have made in our model of rabbits:
there is no attempt to distinguish males from females, for instance, and a rabbit could poten-
tially give birth to a new litter at every simulation step once it is old enough.
10.2.3 The Fox class
There is a lot of similarity between the Fox and the Rabbit classes, so only the distinctive ele-
ments of Fox are shown in Code 10.2.
Code 10.2
The Fox class
// import statements and class comment omitted
public class Fox
{
// Characteristics shared by all foxes (class variables).
// The food value of a single rabbit. In effect, this is the
// number of steps a fox can go before it has to eat again.
private static final int RABBIT_FOOD_VALUE = 9;
 
Search WWH ::




Custom Search