In: Computer Science
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: "
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();
}
}