Objects First With Java - Solutions

Jun 16, 2006 ... Object animal = field.getObjectAt(where); if(animal instanceof Rabbit) { Rabbit
rabbit = (Rabbit) animal; if(rabbit.isAlive()) { rabbit.setEaten(); foodLevel =
RABBIT_FOOD_VALUE; } } } return where; }. It does not seem to have a big
impact. Exercise 10.15. To implement the suggested changes we need a field ...

Part of the document



|Exercise 10.1 |
|Yes, the number of foxes change. |
|Exercise 10.2 |
|Yes, it changes each time simulateOneStep is invoked. |
|Sometimes the number increase and other times it decreases. It |
|probably simulates the births and deaths of foxes. |
|Exercise 10.3 |
|No. |
|Exercise 10.4 |
|The numbers of foxes and rabbits goes up and down. |
|Exercise 10.5 |
|No, it is not an identical simulation. |
|Yes, it is similar patterns. |
|Exercise 10.6 |
|No, it doesn't look like the foxes or rabbits ever die out |
|completely. Why doesn't that happen then? Well, it seems like the |
|parameters of the simulation has been tuned to make a balance |
|between the number of foxes and rabbits. |
|Exercise 10.9 |
|Making the breeding probability of the rabbits much lower has the |
|result that the foxes die out quickly because they don't have |
|enough food. Increasing the breeding probability of the rabbits |
|make them spread faster. |
| |
|Exercise 10.11 |
|Increasing the maximum age for foxes doesn't seem to have a big |
|impact on the number of foxes. |
|Exercise 10.12 |
|Yes, in some configurations the foxes or the rabbits disappear |
|completely. |
|Exercise 10.13 |
|Yes the size does affect the likelihood of species surviving. With|
|a small field (10x10) this is easy to observe. Sometimes the foxes|
|die out, and at other times the rabbits die out. |
|Exercise 10.14 |
|The modified findFood() method: |
| |
|/** |
|* Tell the fox to look for rabbits adjacent to its current |
|location. |
|* @param field The field in which it must look. |
|* @param location Where in the field it is located. |
|* @return Where the last piece of food was found, or null if it |
|wasn't. |
|*/ |
|private Location findFood(Field field, Location location) |
|{ |
|Iterator adjacentLocations = |
|field.adjacentLocations(location); |
|Location where = null; |
|while(adjacentLocations.hasNext()) { |
|where = (Location) adjacentLocations.next(); |
|Object animal = field.getObjectAt(where); |
|if(animal instanceof Rabbit) { |
|Rabbit rabbit = (Rabbit) animal; |
|if(rabbit.isAlive()) { |
|rabbit.setEaten(); |
|foodLevel = RABBIT_FOOD_VALUE; |
| |
|} |
|} |
|} |
|return where; |
|} |
|It does not seem to have a big impact. |
|Exercise 10.15 |
|To implement the suggested changes we need a field with maximum |
|food value: |
|private static final int MAX_FOOD_VALUE = 20; |
|And the modified method: |
| |
|private Location findFood(Field field, Location location) |
|{ |
|Iterator adjacentLocations = |
|field.adjacentLocations(location); |
|Location where = null; |
|while(adjacentLocations.hasNext()) { |
|where = (Location) adjacentLocations.next(); |
|Object animal = field.getObjectAt(where); |
|if(animal instanceof Rabbit) { |
|Rabbit rabbit = (Rabbit) animal; |
|if(rabbit.isAlive()) { |
|rabbit.setEaten(); |
|foodLevel = foodLevel + RABBIT_FOOD_VALUE; |
|if(foodLevel > MAX_FOOD_VALUE) { |
|foodLevel = MAX_FOOD_VALUE; |
|} |
| |
|} |
|} |
|} |
|return where; |
|} |
|Depending on the maximum food value, the foxes seem to survive |
|longer. |
|Exercise 10.16 |
|No, it doesn't seem to have a catastrophic effect. |
|Exercise 10.17 |
|Yes, after a while it seems to behave like the original version. |
|The relative size of the initial populations doesn't have a big |
|impact on the outcome of the simulation. |
|Exercise 10.18 |
|The effect on the fox population is that they all die out after a |
|while when there is too many rabbits. |
|No, the constraint is not placed upon newly born rabbits. |
|Exercise 10.19 |
|No. |
|Exercise 10.20 |
|The similar class fields are: |
|private static final int BREEDING_AGE = 5; |
|private static final int MAX_AGE = 50;. |
|private static final double BREEDING_PROBABILITY = 0.15; |
|private static final int MAX_LITTER_SIZE = 5; |
|private static final Random rand = new Random(); |
|The fox has an additional class field: |
|private static final int RABBIT_FOOD_VALUE = 4; |
| |
|The similar instance fields are: |
|private int age; |
|private boolean alive; |
|private Location location; |
|The fox has an additional instance field: |
|private int foodLevel; |
|The constructors are similar, except that the Fox constructor also|
|initialises its foodLevel. |
|The similar methods are: |
|private void incrementAge() //except for the values of the static |
|fields |
|private int breed() //except for the values of the static fields |
|private boolean canBreed() //except for the values of the static |
|fields |
|public boolean isAlive() |
|public void setLocation(int row, int col) |
|public void setLocation(Location location) |
| |
| |
|The Rabbit class has these methods that the Fox class doesn't |
|have: |
|public void run(Field updatedField, List newRabbits) |
|public void setEaten() |
| |
|And the Fox class has these methods that the Rabbit class doesn't |
|have: |
|public void hunt(Field currentField, Field updatedField, List |
|newFoxes)