Question

In: Computer Science

4.9: Population Write a program that will predict the size of a population of organisms. The...

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

Prompts, Output Labels and Messages.The three input data should be prompted for with the following prompts: "Enter the starting number organisms: ", "Enter the daily increase: ", and "Enter the number of days the organisms will multiply: " respectively. After the input has been read in successfully, a table is produced, for example:

      -----------------------------
      2            300.0
      4            675.0
Under the heading is a line of 29 dashes followed by one line for each day, showing the day number and the population at the beginning of that day.

Input Validation.Do not accept a number less than 2 for the starting size of the population. If the user fails to satisfy this print a line with this message "Invalid. Must be at least 2. Re-enter: " and try to read the value. Similarly, do not accept a negative number for average daily population increase, using the message "Invalid. Enter a non-negative number: " and retrying. Finally, do not accept a number less than 1 for the number of days they will multiply and use the message "Invalid. Enter 1 or more: "

Solutions

Expert Solution

Program Screenshots:

Sample Output:

Code to Copy:

import java.util.Scanner;

// Declare a class.

public class PopulationGrowth {

   // start the main method.
   public static void main(String[] args) {

       // Declare the object of scanner class.
       Scanner scan = new Scanner(System.in);

       // Declare the variables.
       double pop_count, D_growth;

       int N_Days;

       // Prompt the user to enter the strting number of organism.
       System.out.print("Enter the starting number organisms: ");

       // input value.
       pop_count = scan.nextDouble();

       // Start the while loop
       while (pop_count < 2) {

           // Display the result on console.
           System.out.print("Invalid. Must be at least 2. Re-enter:");

           // Input value.
           pop_count = scan.nextDouble();

       }

       // Prompt the user to enter daily increase
       System.out.print("Enter the daily increase: ");

       // Get the value.
       D_growth = scan.nextDouble();

       // Start the while loop
       while (D_growth < 0)

       {
           // Prompt the user to enter non-negative number.
           System.out.print("Invalid. Enter a non-negative number: ");

           // Get the value.
           D_growth = scan.nextDouble();

       }

       // prompt the user to enter value.
       System.out.print("Enter the number of days the organisms will multiply: ");

       N_Days = scan.nextInt();

       // Start the while loop
       while (N_Days < 1)

       {
           // Prompt the user to enter value.
           System.out.print("Invalid. Enter 1 or more: ");

           // get the number.
           N_Days = scan.nextInt();

       }

       // Display the header.
       System.out.println("Day" + "\t\t" + "pop_count");

       // start the for loop
       for (int i = 0; i < 29; i++)

           System.out.print("-");

       int i = 1;

       System.out.println();

       // Start the for loop
       while (i <= N_Days)

       {

           // Display the value.
           System.out.println(i + "\t\t" + Math.round(pop_count));

           // Calculate the value.
           pop_count += pop_count * D_growth;
           i++;
       }
       scan.close();
   }

}


Related Solutions

predict the impact of small population size upon population genetics. also, explain the advantages of using...
predict the impact of small population size upon population genetics. also, explain the advantages of using large population
6. Assessing the population size of organisms is important in determining whether a species requires special...
6. Assessing the population size of organisms is important in determining whether a species requires special conservation effort and in helping to manage sustainable harvesting of food. a) Why is it important to determine the uncertainty surrounding an estimate of population size? (1 mark) b) Using an example species and method of population size estimation, explain how you would assess the quality of your population size estimate. c) Estimating the size of fish species’ populations is very difficult. Imagine that...
A local biologist needs a program to predict population growth. The inputs would be: The initial...
A local biologist needs a program to predict population growth. The inputs would be: The initial number of organisms, as an int The rate of growth (a real number greater than 1), as a float The number of hours it takes to achieve this rate, as an int A number of hours during which the population grows, as an int For example, one might start with a population of 500 organisms, a growth rate of 2, and a growth period...
language: python A local biologist needs a program to predict population growth. The inputs would be:...
language: python A local biologist needs a program to predict population growth. The inputs would be: The initial number of organisms, as an int The rate of growth (a real number greater than 1), as a float The number of hours it takes to achieve this rate, as an int A number of hours during which the population grows, as an int For example, one might start with a population of 500 organisms, a growth rate of 2, and a...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
Write a program that does the following:  Assume the canvas size of 500X500.  The...
Write a program that does the following:  Assume the canvas size of 500X500.  The program asks the user to enter a 3 digit number.  The program then checks the value of the first and last digit of the number.  If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer.  If the two digits are odd, it makes the background red and displays...
Write a program that does the following:  Assume the canvas size of 500X500.  The...
Write a program that does the following:  Assume the canvas size of 500X500.  The program asks the user to enter a 3 digit number.  The program then checks the value of the first and last digit of the number.  If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer.  If the two digits are odd, it makes the background red and displays...
Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly...
Write a C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly...
Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT