Question: A) In the class PopulationModel add the following fields: numA and numB which are both int arrays. Make these arrays public for testing purposes. Also
A)
In the class PopulationModel add the following fields:
numA and numB which are both int arrays.
Make these arrays public for testing purposes.
Also add the following private constants:
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.
B)
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.
C)
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)
D)
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.
E)
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.
F)
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.
G)
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.
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 PopulationModelobject, and then printBarChart on the same object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
