Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Write a recursive method to calculate the size of a population of organisms

ID: 3589618 • Letter: J

Question

Java

Write a recursive method to calculate the size of a population of organisms that increases at a specified rate each day.

The method header is: public int populationSize(int startingPopulation, double increaseRate, int numberOfDays)

Examples:starting population = 10, increase rate = 0.5, number of days = 5

day 1 population = 15

day 2 population = 22 (22.5 truncated down to 22)

day 3 population = 33

day 4 population = 49 (49.5 truncated down to 49)

day 5 population = 73 (73.5 truncated down to 73)

method returns 73

starting population = 2, increase rate = 1.0, number of days = 4

day 1 population = 4

day 2 population = 8

day 3 population = 16

day 4 population = 32

method returns 32

Explanation / Answer

PopulationSizeTest.java

public class PopulationSizeTest {

public static void main(String[] args) {

PopulationSizeTest p = new PopulationSizeTest();

System.out.println(p.populationSize(2, 1.0, 4));

System.out.println(p.populationSize(10, 0.5, 5));

}

public int populationSize(int startingPopulation, double increaseRate,

int numberOfDays) {

if (numberOfDays == 0) {

return startingPopulation;

} else {

startingPopulation = (int)(startingPopulation + startingPopulation * increaseRate);

return

populationSize(startingPopulation, increaseRate,

numberOfDays - 1);

}

}

}

Output:

32
73

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote