Question

Write a program that will predict the size of a population of organisms. The program should...

Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply.
For example, a population might begin with two organisms, have an average daily increase of 50 percent, and will be allowed to multiply for seven days. The program should use a loop to display the size of the population for each day.
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.
 Your program must a recursive method instead of a loop to calculate the number of organisms.

Here's the code. The code runs however the daily increase is wrong. When i plug in the number of oganisms to be 15 , daily incease to be 30 and the days to be 7, it should print out 1    15.0, 2 19.5, 3 25.35, 4 32.955, 5 42.842, 6 55.694 and 7 72.402. Thanks for your help

import java.util.Scanner;
import java.text.DecimalFormat;
public class Population
{
   private double showPopulation(int dayNum, int days,double organisms,double dailyIncrease) //private function each day population display
   {
       if (dayNum == days)
        return organisms;
       else
       return (organisms*(dailyIncrease)/100); //caculate population %
   }
    
   public void displayPopulation(double startingOrgnisms,double increase,int days)
   {
       DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.000000000000");
       double dailyIncrease=startingOrgnisms;
       System.out.println("Days            Organisms");
       System.out.println("----------------------------------");
     
       for(int i=1;i <= days;i++) //repeating loop fo no.of days
       {
       dailyIncrease+=showPopulation(i,days,startingOrgnisms,increase); //calling private function to display each population
       System.out.println(i+"                 "+formatter.format(dailyIncrease)); //printing the output
   }
   }
   public static void main(String args[])
   {
       double startingOrgnisms,increase;
       int days;
       Population p1=new Population(); //create object of Population
    
       Scanner s=new Scanner(System.in); //create Scanner object for reading
       while(true) //repeat loop,if population size entered is <2
       {
       System.out.println("Enter the starting number of organisms: ");
       startingOrgnisms=s.nextDouble(); //input population
       if(startingOrgnisms>=2) break;//if valid input exit loop
       System.out.println("Invalid! enter more than 2 for population"); //otherwise error message and repeat loop
       }
    
       while(true) //repeat loop, if average increase <0
       {
       System.out.println("Enter the daily Increase : ");
       increase=s.nextDouble(); //input increase average value
       if(increase>0) break; //if input is valid then exit loop
       System.out.println("Invalid! enter greater than 0 for daily increase"); //otherwise error message and repeat loop
       }
    
       while(true) //loop for validating no.of days
       {
       System.out.println("Enter the number of days organisms will multiply : ");
       days=s.nextInt();//input days
       if(days>0) break; //if right input exit loop
       System.out.println("Please! enter greater than 0 for the number of days organisms will multiply ");
        //otherwise error and repeat the loop
       }
    
       p1.displayPopulation(startingOrgnisms,increase,days); //calling function to display all
    
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
import java.text.DecimalFormat;
public class Population
{
private double showPopulation(int dayNum, int days,double organisms,double dailyIncrease) //private function each day population display
{
if (dayNum == days)
return organisms;
else
return (organisms*(dailyIncrease)/100); //caculate population %
}
  
public void displayPopulation(double startingOrgnisms,double increase,int days)
{
DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.000000000000");
double dailyIncrease=startingOrgnisms;
System.out.println("Days Organisms");
System.out.println("----------------------------------");

for(int i=1;i <= days;i++) //repeating loop fo no.of days
{   
dailyIncrease+=showPopulation(i,days,startingOrgnisms,increase); //calling private function to display each population
startingOrganisms = dailyIncrease;
System.out.println(i+" "+formatter.format(dailyIncrease)); //printing the output
}
}
public static void main(String args[])
{
double startingOrgnisms,increase;
int days;
Population p1=new Population(); //create object of Population
  
Scanner s=new Scanner(System.in); //create Scanner object for reading
while(true) //repeat loop,if population size entered is <2
{
System.out.println("Enter the starting number of organisms: ");
startingOrgnisms=s.nextDouble(); //input population
if(startingOrgnisms>=2) break;//if valid input exit loop
System.out.println("Invalid! enter more than 2 for population"); //otherwise error message and repeat loop
}
  
while(true) //repeat loop, if average increase <0
{
System.out.println("Enter the daily Increase : ");
increase=s.nextDouble(); //input increase average value
if(increase>0) break; //if input is valid then exit loop
System.out.println("Invalid! enter greater than 0 for daily increase"); //otherwise error message and repeat loop
}
  
while(true) //loop for validating no.of days
{
System.out.println("Enter the number of days organisms will multiply : ");
days=s.nextInt();//input days
if(days>0) break; //if right input exit loop
System.out.println("Please! enter greater than 0 for the number of days organisms will multiply ");
//otherwise error and repeat the loop
}
  
p1.displayPopulation(startingOrgnisms,increase,days); //calling function to display all
  
}
}

Add a comment
Know the answer?
Add Answer to:
Write a program that will predict the size of a population of organisms. The program should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5.11: Population Write a program that will predict the size of a population of organisms. The...

    5.11: Population Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), and the number of days they will multiply. A loop should display the size of the population for each day. Input Validation.Do not accept a number less than...

  • Write a program that will predict the size of a population of organisms. The program should ask t...

    Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. A loop should display the size of the population for each day. Input Validation: Do not accept a number less than 2 for the starting size of the population. "The starting number of organisms must be at least 2."...

  • Starting out with Python 4th edition I need help with while loops I can't even get...

    Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY       APPROXIMATE POPULATION ------------------------------------------------- 1            2 2            2.600 3            3.380 4            4.394...

  • Java. Please write the output import java.util.Scanner; import java.text.Decimalformat: * This program demonstrates two-dimensional array. public...

    Java. Please write the output import java.util.Scanner; import java.text.Decimalformat: * This program demonstrates two-dimensional array. public class CorpSales public static void main(String[] args) Final int DIVS - 3; // Three divisions in the company final int QTRS = 4; // Four quarters double totalSales - e.e; / Accumulator // Create an array to hold the sales for each // division, for each quarter. double[][] sales - new double[DIVS][QTRS] // Create a Scanner object for keyboard input. Scanner keyboard = new...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value have been read, and computes the total and average of the input values (not counting zeros. Your program ends with the input 0. Display the average as a floating point number Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program. import java.util.Scanner; public class SalaryCalc { double Rpay = 0, Opay = 0; void...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

  • Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....

    Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. a. Modify the TaxTableTools class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT