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

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

ID: 3776336 • Letter: I

Question

In 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

package org.students;

import java.util.Scanner;

public class PopulationOfOrganisms {
   //Declaring static variables
static int count=0;
static int population=0;

   public static void main(String[] args) {
      
       //Declaring variables
       int startPop,no_of_days;
       double incRate;
      
       //Scanner class object is sued to read the inputs entered by the user
Scanner sc=new Scanner(System.in);

//Getting the starting population entered by the user
System.out.print("Starting Population :");
startPop= sc.nextInt();
  
//Getting the increase rate entered by the user
System.out.print("Increase Rate :");
incRate=sc.nextDouble();
  
//Getting the no of days entered by the user
System.out.print("No of Days :");
no_of_days=sc.nextInt();

//Calling the method by passing the inputs
int finalPop=populationSize(startPop,incRate,no_of_days);


System.out.print("method returns "+finalPop);

   }

   /* This method method recursively calculates
   * and displays the population of Organisms each day
   */
   private static int populationSize(int startPop,double incRate, int no_of_days) {
       if(no_of_days==0)
           return startPop;
       else
       {
           population=(int)(startPop*incRate)+startPop;
           startPop=population;
       System.out.println("day "+(++count)+" population="+population);  
       return populationSize(startPop,incRate,no_of_days-1);
       }
   }

}

____________________

Output:

Starting Population :10
Increase Rate :0.5
No of Days :5
day 1 population=15
day 2 population=22
day 3 population=33
day 4 population=49
day 5 population=73
method returns 73

_____________

Output2:

Starting Population :2
Increase Rate :1.0
No of Days :4
day 1 population=4
day 2 population=8
day 3 population=16
day 4 population=32
method returns 32

_________Thank You

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