Question
Create a Population class:
In the main method of the population class:
- From the keyboard, enter the starting number of organisms as 21; validate the input.
- Enter the daily increase from the keyboardas 2: validate the input.
- Enter the number of days the organisms will multiple as 10; validate the input.
- Calculate and display the daily population.
- In the Population class, create a displayPopulation method that displays the population table header and then calls the recursive showPopulation method to display the daily populations.
- In the population class, create the recursive showpopulation method that displays the daily populations for a group of organisms for a specified day, and then calls itself to display the data for the remainder of the days in a time period.
- Output the following:
Enter the starting number organisms: 21
Enter the daily increase: 2
Enter the number of days the organisms will multiply: 10
Day Organisms
----------------------------------
1 (These should be double values)
2
3
(Show this through Day 10)
Explanation / Answer
Based on the fact that the main method is in the Population class, I assume you are writing this in Java. Further, I assume that you already know how to do console input and output. If either of these cases is incorrect, let me know. The problem statement calls for three methods within the Population class: * main(), which is to handle input and input validation. * displayPopulation(), which is to handle the initial table display and then pass control to the recursive function. * showPopulation(), which is to be the recursive function that handles the calculation and display of the number of organisms for each day. Let's begin by examining what we need for showPopulation(). First, in order for this to work, we should have the parameters entered from the keyboard stored as static variables within the class. Let's call them startingPopulation, daylyIncrease, and numDays. They should also be marked private, since they don't need to be visible outside the scope of the class. The showPopulation() method itself will need to accept two parameters: the day number, and the current organism population. Here I need to make a couple of other assumptions, these about the multiplication algorithm itself. No description is given other than the data to be entered. I assume two things: the organism population increases by a simple additive process (numOrganisms[day] = numOrganisms[day -1] + dailyIncrease), and that numOrg[1] = startingPopulation. With these assumptions in place, let's look at what needs to happen in the recursive function. REMEMBER: A recursive function is one which calls itself. Thus, there needs to be an “escape clause”, otherwise you will wind up with a stack-eating monster that will crash with some sort of out of memory exception. The process will be something like this: public static void showPopulation (int day, double currentPopulation) { if (day