

But in this case it would make more sense to keep them as separate methods, because it is most likely just a coincidence that the values are equal.Ī good way to test it would be to build a set of unit tests that tests the part of the program that is likely to be affected by the changes. In this case that would be the methods: canBreed() and breed(). If it can truly be considered a field which will always have the same value for both the Rabbit and the Fox, it would make sense to treat methods that use that field as identical. In general it depends on the actual field. Because it is most likely that we would want to change the value in the future to values different for the Fox and the Rabbit. The truly identical methods are: 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()Īnd the Fox class has these methods that the Rabbit class doesn't have: public void hunt(Field currentField, Field updatedField, List newFoxes) private void incrementHunger() private Location findFood(Field field, Location location) 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 constructors are similar, except that the Fox constructor also initialises its foodLevel. The fox has an additional instance field: private int foodLevel The similar instance fields are: private int age The fox has an additional class field: private static final int RABBIT_FOOD_VALUE = 4 private static final double BREEDING_PROBABILITY = 0.15 private static final int MAX_LITTER_SIZE = 5 private static final Random rand = new Random() The similar class fields are: private static final int BREEDING_AGE = 5 private static final int MAX_AGE = 50. No, the constraint is not placed upon newly born rabbits.

The effect on the fox population is that they all die out after a while when there is too many rabbits. The relative size of the initial populations doesn't have a big impact on the outcome of the simulation. Yes, after a while it seems to behave like the original version. No, it doesn't seem to have a catastrophic effect. To implement the suggested changes we need a field with maximum food value: private static final int MAX_FOOD_VALUE = 20 Īnd the modified method: private Location findFood(Field field, Location location)įoodLevel = foodLevel + RABBIT_FOOD_VALUE ĭepending on the maximum food value, the foxes seem to survive longer. Object animal = field.getObjectAt(where) Where = (Location) adjacentLocations.next() Private Location findFood(Field field, Location location) * Where the last piece of food was found, or null if it wasn't. * location Where in the field it is located. * Tell the fox to look for rabbits adjacent to its current location. Sometimes the foxes die out, and at other times the rabbits die out. With a small field (10x10) this is easy to observe. Yes the size does affect the likelihood of species surviving.

Yes, in some configurations the foxes or the rabbits disappear completely. Increasing the maximum age for foxes doesn't seem to have a big impact on the number of foxes.

Increasing the breeding probability of the rabbits make them spread faster. 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. 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. No, it doesn't look like the foxes or rabbits ever die out completely. The numbers of foxes and rabbits goes up and down. It probably simulates the births and deaths of foxes. Sometimes the number increase and other times it decreases. Yes, it changes each time simulateOneStep is invoked.
