Java Reference
In-Depth Information
Exercise 10.9
Check to see that setting the
useShared
field in
Randomizer
to
false
breaks the repeatability of the simulations seen in Exercise 10.8. Be sure to restore it to
true
afterwards, because repeatability will be an important element in later testing.
Now that we have a broad, external understanding of what this project does, we will look in
detail at the implementation of the
Rabbit
,
Fox
, and
Simulator
classes.
10.2.2 The
Rabbit
class
The source code of the
Rabbit
class is shown in Code 10.1.
Code 10.1
The
Rabbit
class
//
import statements and class comment omitted
public
class
Rabbit
{
// Characteristics shared by all rabbits (class variables).
// The age at which a rabbit can start to breed.
private static final
int
BREEDING_AGE = 5;
// The age to which a rabbit can live.
private static final
int
MAX_AGE = 40;
// The likelihood of a rabbit breeding.
private static final
double
BREEDING_PROBABILITY = 0.12;
// The maximum number of births.
private static final
int
MAX_LITTER_SIZE = 4;
// A shared random number generator to control breeding.
private static final
Random rand = Randomizer.getRandom();
// Individual characteristics (instance fields).
// The rabbit's age.
private
int
age;
// Whether the rabbit is alive or not.
private
boolean
alive;
// The rabbit's position.
private
Location location;
// The field occupied.
private
Field field;
/**
* Create a new rabbit. A rabbit may be created with age
* zero (a newborn) or with a random age.
*
Search WWH ::
Custom Search