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

Java HW Write a recursive method to calculate the size of a population of organi

ID: 3698275 • Letter: J

Question

Java HW

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

Thank you

Explanation / Answer

/**
* The java program that test the recursive method populationSize
* that prints day and population and finally prints the total population.
* */
//Recursion.java
public class Recursion
{
   public static void main(String[] args) {

       //set vlaues
       int startingPopulation = 10;
       double increaseRate = 0.5;
       int numberOfDays = 5;  
       //call populationSize(startingPopulation,increaseRate,numberOfDays)
       int totalPopulation=populationSize(startingPopulation,increaseRate,numberOfDays);
       //print totalPopulation
       System.out.println(totalPopulation);
   }
   /**
   * The static method populationSize that takes an integer, double and integer values
   * base conditon:
   * If numberOfDays<1
   *    startingPopulation
   * Recursive call:
   * startingPopulation=populationSize(startingPopulation, increaseRate, numberOfDays-1);          
       startingPopulation=(int) (startingPopulation+startingPopulation*increaseRate);
       System.out.println("Day "+numberOfDays+"population = "+startingPopulation);
   * **/
   public static int populationSize(int startingPopulation, double increaseRate, int numberOfDays)
   {
       //Base conditon
       if(numberOfDays<1)
       {
           return startingPopulation;
       }
       else
       {  
           //Set populationSize return value to startingPopulation
           startingPopulation=populationSize(startingPopulation, increaseRate, numberOfDays-1);
           //find the increase int startingPopulation and store startingPopulation
           startingPopulation=(int) (startingPopulation+startingPopulation*increaseRate);
           //print Day and population
           System.out.println("Day "+numberOfDays+"population = "+startingPopulation);
          
                                  
       }
       //return startingPopulation
       return startingPopulation;
   }
}

--------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

Day 1population = 15
Day 2population = 22
Day 3population = 33
Day 4population = 49
Day 5population = 73
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