8) (a) This question refers to the subroutine InputCoordinate in the Simulation class.
The warren and fox inspection options in the Skeleton Program do not currently check if the coordinates entered by the user are on the landscape. This behaviour needs to be improved so that an error message is displayed if the user inputs coordinates for a location that is not on the landscape.
If the user runs a simulation with default settings then the landscape size is 15, so valid locations have an x coordinate between 0 and 14, inclusive.
What you need to do
Modify the InputCoordinate subroutine in the Simulation class so that, if a coordinate outside the range defined by the landscape size is input, the error message “Coordinate is outside of landscape, please try again.” is displayed and the user is forced to re-input the coordinate.
To achieve full marks for this question, the InputCoordinate subroutine should work correctly for any landscape size, not just the default size of 15.
Test
Test your changes work by running the Skeleton Program and selecting the following options:
-
- “1. Run simulation with default settings”
- “3. Inspect fox”
Then input these three x coordinates for the location of the fox to inspect:
Evidence that you need to provide
(i) Your PROGRAM SOURCE CODE for the amended subroutine InputCoordinate.
(ii) SCREEN CAPTURE(S) for the described test.
Ensure that in your SCREEN CAPTURE(S) it can be seen that the x coordinates −1 and 15 are rejected and that the x coordinate 0 is accepted, and that after the 0 is input the Skeleton Program advances to ask the user to input the y coordinate.
(b) The simulation is to be made more realistic by increasing the probability that a rabbit will die as a result of other causes, such as disease or injury, as the rabbit ages.
The default probability of death by another cause for a rabbit is 0.05.
-
- The probability of a male rabbit dying by another cause should increase by a factor of 50% after every time period.
- The probability of a female rabbit dying by another cause should remain constant until the rabbit reaches the age of 2. At the age of 2, and after every time period beyond this, the probability of a female rabbit dying by another cause should increase by 0.05.
Table 1 below summarises the probability of death by other causes for a rabbit of each gender, up to the age of 5. The probabilities will continue to increase beyond this age.

What you need to do
Create a new subroutine, CalculateNewAge, in the Rabbit class, that overrides the CalculateNewAge subroutine in the Animal class.
The new CalculateNewAge subroutine in the Rabbit class should recalculate the probability of death for a rabbit as the rabbit ages. The subroutine should also call the subroutine that it has overridden in the Animal class to ensure that the standard ageing process for a rabbit continues to be carried out as well.
Test
Check that the changes you have made work by conducting the following test:
-
- Select option “1. Run simulation with default settings” from the main menu.
- Then select option “2. Advance to next time period hiding detail” twice, to advance the simulation to time period 2.
- Then select option “4. Inspect warren” and enter the x coordinate 1 and the y coordinate 1.
- When asked “View individual rabbits (y/n)?” enter y.
Evidence that you need to provide
(i) Your PROGRAM SOURCE CODE for the new subroutine CalculateNewAge from the Rabbit class.
(ii) SCREEN CAPTURE(S) for the described test.
Your SCREEN CAPTURE(S) must clearly show the probability of death by other causes of both a male and a female rabbit of age 2. SCREEN CAPTURE(S) do not need to show the options that you have selected or the probability of death by other causes for rabbits of other ages.
(c) The simulation is to be extended to represent the landscape that the animals live in. Most of the landscape will be land, but two rivers will run through it. The locations of the rivers are shaded in Figure 3.

Each of the individual locations, eg (12, 7), within the landscape will be assigned to be an area of either land or river.
What you need to do
Task 1
Modify the Location class so that it can store a representation of the type of terrain at the location. This representation should be as a character, with “L” representing land and “R” representing river.
Task 2
Modify the constructor subroutine of the Location class so that when a location is created, the constructor is passed the type of terrain that the location will be and this is stored appropriately.
Task 3
Modify the CreateLandscapeAndAnimals subroutine in the Simulation class so that when the landscape is created the appropriate type of terrain, as shown in Figure 3, is stored in each location. The terrain should be represented as a character, with “L” representing land and “R” representing river.
Task 4
Modify the DrawLandscape subroutine in the Simulation class so that the correct type of terrain at each location is displayed when the landscape is drawn.
Figure 4 shows one example of how the landscape could be drawn, with a letter “L” indicating that a location contains land, and a letter “R” indicating that a location contains part of a river. However, you are free to indicate the type of terrain at a location in any way that you choose, so long as this is clear to the user.

Task 5
Modify the CreateNewWarren and CreateNewFox subroutines in the Simulation class so that warrens and foxes cannot be created in locations that are part of a river.
Test
Check that the changes you have made in Tasks 1 to 4 (not Task 5) work by conducting the following test:
-
- Select option “1. Run simulation with default settings” from the main menu.
Evidence that you need to provide
(i) Your PROGRAM SOURCE CODE for the whole of the Location class, including the constructor subroutine.
(ii) Your PROGRAM SOURCE CODE for the amended CreateLandscapeAndAnimals subroutine from the Simulation class.
(iii) Your PROGRAM SOURCE CODE for the amended DrawLandscape subroutine from the Simulation class.
(iv) Your PROGRAM SOURCE CODE for the amended CreateNewWarren and CreateNewFox subroutines from the Simulation class.
(v) SCREEN CAPTURE(S) for the described test, showing the correct type of territory in each location on the landscape.
(d) The landscape affects the foxes’ ability to eat the rabbits. Foxes do not like to swim, so will not cross the rivers on the landscape to eat. If a river lies between a fox and a warren, the fox will not eat any rabbits in the warren, even if it is near enough for it to do so.
As the rivers only run horizontally and vertically, and extend from one side of the landscape to the other, a simple way to check if reaching a warren would require a fox to cross a river is to:
-
- Calculate the coordinates of all of the locations between the fox and the warren in a horizontal line, level with the fox.
- Calculate the coordinates of all of the locations between the fox and the warren in a vertical line, level with the fox.
- If any of the locations horizontally or vertically between the fox and the warren contain a river, then the fox will not eat any of the rabbits in the warren as the fox’s path to the warren crosses a river.
Figure 5 shows the locations that would need to be checked to see if fox F could eat any rabbits in warren W. The locations that need to be checked are shown in black and the rivers are shown in grey. As location (5, 7) contains part of a river, the fox would not eat any rabbits in this warren.

If you have not been able to fully complete part (c), you will still be able to get most of the marks for this question if you can correctly compute the coordinates of the locations that would need to be checked to see if a river was present.
To get full marks for this question, your solution must work regardless of whether a warren is above, below, to the left or to the right of a fox.
What you need to do
Task 1
Create a new subroutine CheckIfPathCrossesRiver, in the Simulation class, that takes the coordinates of two locations in the landscape and checks if there is a river between them.
Task 2
Modify the FoxesEatRabbitsInWarren subroutine in the Simulation class so that it calls the CheckIfPathCrossesRiver subroutine, and ensures that if there is a river between a fox and a warren then the fox will not eat any rabbits from the warren.
Test
Check that the changes you have made work by conducting the following test:
-
- Select option “1. Run simulation with default settings” from the main menu.
- Then select option “1. Advance to next time period showing detail”.
When the test is conducted, no rabbits in the warren at (1, 1) should be eaten as it is bounded by rivers on all sides.
Evidence that you need to provide
(i) Your PROGRAM SOURCE CODE for the new subroutine CheckIfPathCrossesRiver from the Simulation class.
(ii) Your PROGRAM SOURCE CODE for the amended subroutine FoxesEatRabbitsInWarren from the Simulation class.
(ii) SCREEN CAPTURE(S) for the described test.
Your SCREEN CAPTURE(S) only needs to show what happens in the warren at location (1, 1) when the simulation advances to the next time period. It should contain similar information to Figure 6 below, but the exact number of rabbits killed, dying of old age and other details may differ owing to the random nature of parts of the simulation.
