52.1k views
1 vote
6. Create a java application that will predict the size of a population of organisms. The user should be able to provide 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.

User Med Besbes
by
7.9k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

Here's a Java application that predicts the size of a population of organisms based on user inputs:

-------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class PopulationPredictor {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Get user inputs

int startingSize;

do {

System.out.print("Enter the starting size of the population (must be at least 2): ");

startingSize = scanner.nextInt();

} while (startingSize < 2);

double dailyIncrease;

do {

System.out.print("Enter the average daily population increase as a percentage (must be non-negative): ");

dailyIncrease = scanner.nextDouble();

} while (dailyIncrease < 0);

int numDays;

do {

System.out.print("Enter the number of days the population will multiply (must be at least 1): ");

numDays = scanner.nextInt();

} while (numDays < 1);

// Calculate and display population size for each day

double populationSize = startingSize;

System.out.println("\\Day 1: " + populationSize);

for (int day = 2; day <= numDays; day++) {

populationSize += (populationSize * dailyIncrease) / 100;

System.out.println("Day " + day + ": " + populationSize);

}

}

}

-------------------------------------------------------------------------------------------------------------

The program first prompts the user to input the starting size of the population, the average daily population increase (as a percentage), and the number of days the population will multiply. It then uses a series of do-while loops to validate the user inputs according to the requirements stated in the prompt.

Once the user inputs are validated, the program calculates the size of the population for each day using a for loop. The population size for each day is calculated by multiplying the previous day's population size by the daily increase percentage (converted to a decimal), and adding the result to the previous day's population size.

The program then displays the population size for each day using println statements.

User Cheries
by
7.8k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.