Write a program that will predict the size of a population of organisms. The pro
ID: 3760225 • Letter: W
Question
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 populationincrease (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 dailyincrease : ", 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: ".
Explanation / Answer
If you have any further queries, just revert here.
import java.util.Scanner;
public class PopulationRecur {
public static void main(String[] args) {
int numDays;
int days;
int organism;
float increase;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of organisms: ");
organism = keyboard.nextInt();
while (organism < 2) {
System.out.print("The starting population must me 2 or higher: ");
organism = keyboard.nextInt();
}
System.out.print("Enter the daily increase: ");
increase = keyboard.nextInt();
while (increase < 0) {
System.out.print("The daily increase must be a positive fraction: ");
increase = keyboard.nextFloat();
}
System.out.print("Enter the number of days the organisms will multiply:");
days = keyboard.nextInt();
while (days < 1) {
System.out.print("The number of days must be 1 or greater: ");
days = keyboard.nextInt();
}
numDays = 1;
System.out.println("DayNum Population");
System.out.println(" -----------------------------");
showPopulation(numDays,days,organism,increase);
}
private static double showPopulation(int dayNum, int days, double organisms, double dailyIncrease) {
if (dayNum == days) {
return organisms;
} else {
}
System.out.println( (dayNum + 1) + " " + (organisms + organisms * dailyIncrease));
return showPopulation(dayNum + 1, days, organisms + organisms * dailyIncrease, dailyIncrease);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.