Two colonies of animals predate on each other. On average, each week, each member of a colony
Question:
Two colonies of animals predate on each other. On average, each week, each member of a colony A kills 0.2 members of colony B, and each member of colony B kills 0.1 members of colony A. You can assume that the population of each colony does not depend on any other factor.
Here is an illustration of the mathematics (don't worry, you don't have to understand it in any detail).
If in week 0, the size of colony A is 50 and the size of colony B is 60 then
In week 1, the populations are:
A: 50 - 0.1(60) = 44
B: 60 - 0.2(50) = 50
In week 2, the populations are:
A = 44 - 0.1(50) = 39
B = 50 - 0.2(44) = 41.2 which is rounded down to 41.
Eventually one of the colonies will have a population that is zero indicating that it has been wiped out.
In this question you will explore this model for a set number of weeks.
a.Launch BlueJ and create a new project TMA02Q3SolXX, where XX should be replaced by your own initials, in the TMA02Download folder you unzipped earlier.
Begin by adding a new class called PopulationModel. Retain the class comment but edit to it include your own name and the date and a description of the class. Remove the sample code.
b.In the class PopulationModel add the following fields:
- numA and numB which are both int arrays.
- a class constant, INITIALA, of type int, initialised to the value 50
- a class constant, INITIALB, of type int, initialised to the value 60
- a class constant, KILL_RATEA, of type double, initialised to 0.2
- a class constant, KILL_RATEB, of type double, initialised to 0.1
- a class constant NUM_WEEKS which is initialised to 10.
c.Add a public constructor that initialises numA and numB to be int arrays of length NUM_WEEKS.
The elements at index 0 of numA and numB should then be initialised to INITIALA and INITIALB, respectively.
d.Write a method with the header
public int newNumA(int currentA, int currentB)
The method calculates the number of animals left in colony A in the given week, based on the numbers in the two colonies in the previous week. This number is given by the expression:
currentA - KILL_RATEB * currentB
Finally, the integer number of animals left in colony A should be returned if it is greater than 0, otherwise 0 should be returned. (The number of animals in the colony cannot go below zero.)
Hint: The result of the above calculation is a double, but you want to return an int. The following expression rounds a double value aDouble down to the nearest double number and casts it to an int:
(int) Math.floor(aDouble)
e.Write a method with the header
public int newNumB(int currentA, int currentB)
The method calculates the number of animals left in colony B in the given week, based on the numbers in the two colonies in the previous week. This is given by the expression:
currentB - KILL_RATEA * currentA
Remember to convert your result from the calculation to an int as in the previous part. This converted value should be returned if it is greater than 0, otherwise 0 should be returned, so that the number of animals in the colony does not go below 0.
f.Write a method with the header
public void storeNewPopulations(int thisWeek)
that uses the newNumA and newNumB methods to find the populations of colony A and colony B for next week based on the index, thisWeek. The new populations are stored at the appropriate indexes of the numA and numB arrays.
g.Write a method with the header
public void createData()
This method uses a loop to call the storeNewPopulations method with each of the possible array indexes in turn as a parameter, except for the last array index.
After calling this method the numA and numB arrays should contain model data for how the populations are changing in the two colonies. When testing, you should check that this method has populated both arrays with data.
h.Write a public method printBarChart that has no parameters and does not return a value, which prints a bar chart of the populations of colony A and colony B.
Note: An example of bar chart for a different set of parameters is illustrated in the file TMA02Q3SampleGraph.png, included in the TMA download folder.
To print the bar chart, your method must retrieve each value from the numA array and numB array using an outer loop and print that number of '*' characters, for each week, using an inner loop. Notice that the week number (starting from week 0) and A or B must be printed, as appropriate, before each row of asterisks. At the end of each sequence of stars, the population of colony A and colony B is printed in parentheses, as illustrated in the sample bar chart.
To test this method in the Code Pad, first call createData on a PopulationModel object, and then printBarChart on the same object.
In your Solution Document, in addition to your code, include the output from your Terminal window (either copy and paste, or as a screenshot) showing the result of testing the printBarChart method. Your constants should have the values as specified in part (b) of the question. Arrange it so that the output is shown on one page of your Solution Document.
i.Go back and check that you have provided Javadoc comments for your class, constructors and methods, including the @author, @version, @param and @return annotations where appropriate. Be sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.
Also check that you have used a consistent style across the whole class. See the M250 Code Conventions document for more advice on coding style and check that you reused methods in this question where possible rather than duplicating code.
j.Follow the link below to test your code. Take a screenshot of the output from the online testing system and paste it into your Solution Document.
You should still include the screenshot even if your code did not compile, or if it compiled and ran but did not produce correct results.
If you had errors in your code and were unable to fix them, explain briefly to your tutor how you tried to solve the problem. This will help them provide the most appropriate feedback.
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill