Java Reference
In-Depth Information
Code 10.2
The Fox class
// other static fields omitted
// Individual characteristics (instance fields).
// The fox's age.
private int age;
// Whether the fox is alive or not.
private boolean alive;
// The fox's position.
private Location location;
// The field occupied.
private Field field;
// The fox's food level, which is increased by eating rabbits.
private int foodLevel;
* Create a fox. A fox can be created as a newborn (age zero
* and not hungry) or with a random age and food level.
* @param randomAge If true, the fox will have random age
* and hunger level.
* @param field The field currently occupied.
* @param location The location within the field.
public Fox( boolean randomAge, Field field, Location location)
// body of constructor omitted
* This is what the fox does most of the time: it hunts for
* rabbits. In the process, it might breed, die of hunger,
* or die of old age.
* @param field The field currently occupied.
* @param newFoxes A list to return newly born foxes.
public void hunt(List<Fox> newFoxes)
if (alive) {
// Move towards a source of food if found.
Location newLocation = findFood();
if (newLocation == null ) {
// No food found - try to move to a free location.
newLocation = field.freeAdjacentLocation(location);
Search WWH ::

Custom Search