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...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • 1.Given an int variable n that has been initialized to a positive value and, in addition,...

    1.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++) 2.Given an int variable...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • 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...

  • PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to...

    PLEASE LET ME KNOW WHERE I AM GOING WRONG IN THIS CODE. Allow a user to enter any number of double values up to 20. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. My code is below: import java.util.Scanner; public class DistanceFromAverage { public static void main(String[] args) { // declaration of values as double type...

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