Java Reference
In-Depth Information
Code 10.1
continued
The Rabbit class
* @param randomAge If true, the rabbit will have a random age.
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Rabbit( boolean randomAge, Field field, Location location)
{
// body of constructor omitted
}
/**
* This is what the rabbit does most of the time: it runs
* around. Sometimes it will breed or die of old age.
* @param newRabbits A list to return newly born rabbits.
*/
public void run(List<Rabbit> newRabbits)
{
incrementAge();
if (alive) {
giveBirth(newRabbits);
// Try to move into a free location.
Location newLocation =
field.freeAdjacentLocation(location);
if (newLocation ! = null ) {
setLocation(newLocation);
}
else {
// Overcrowding.
setDead();
}
}
}
/**
* Indicate that the rabbit is no longer alive.
* It is removed from the field.
*/
public void setDead()
{
alive = false ;
if (location != null ) {
field.clear(location);
location = null ;
field = null ;
}
}
Search WWH ::




Custom Search