I need to Write a class that will predict the size of a population of organisms.
ID: 3770796 • Letter: I
Question
I need to
Write a class that will predict the size of a population of organisms. The class should store the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. The class should have a method that uses a loop to display the size of the population for each day.
Test the class in a program that asks the user for the starting size of the population, their average daily increase, and the number of days they will multiply. The program should display the daily population.
Input Validation: Do not accept a number less than 2 for the starting size of the population. Do not accept a negative number for average daily population increase. Do not accept a number less than 1 for the number of days they will multiply.
THEN
Modify the class so it uses a recursive method instead of a loop to calculate the number of organisms.
This is what I have so far
I need to include a Demo/tester class
public class Population
{
private int popNum;
private double percentIncr;
private int numDays;
public Population(int intNum, double percent, int days)
{
popNum = intNum;
percentIncr = percent;
numDays = days;
}
public void displayIncrease( )
{
for(int day=1; day<= numDays; day++)
{
popNum += popNum * percentIncr;
System.out.println( "Days" + day + "Population: " + popNum);
Explanation / Answer
package assignment;
public class PopulationTest {
public static void main(String[] args) {
Population pop = new Population(10, 0.4, 5);
pop.displayIncrease();
pop.displayIncreaseRecursive();
}
}
class Population {
private int popNum;
private double percentIncr;
private int numDays;
public Population(int intNum, double percent, int days) {
popNum = intNum;
percentIncr = percent;
numDays = days;
}
public void displayIncreaseRecursive() {
displayIncrease(numDays, popNum);
}
private int displayIncrease(int days, int population) {
if(days > 0) {
population += population * percentIncr;
System.out.println("Days: " + (numDays-days+1) + " Population: " + population);
return displayIncrease(days-1,population);
}
return population;
}
public void displayIncrease() {
int population = popNum;
for (int day = 1; day <= numDays; day++) {
population += population * percentIncr;
System.out.println("Days" + day + "Population: " + population);
}
}
}
--output---
Days1Population: 14
Days2Population: 19
Days3Population: 26
Days4Population: 36
Days5Population: 50
Days: 1 Population: 14
Days: 2 Population: 19
Days: 3 Population: 26
Days: 4 Population: 36
Days: 5 Population: 50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.