-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.java
64 lines (54 loc) · 2.12 KB
/
Population.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Population.java
import java.util.Scanner;
/**
This program will calculate an organisms population based on a
given rate of increase over a given number of days from a given
population begining number.
*/
public class Population
{
public static void main(String[] args)
{
double startingOrganisims; // The given number of starting organisms
double percentPopIncrease; // The given percentage of population increase
double multiplyDays; // The given number of days to calculate for
double dailyPopulation; // The calculated population per day
// create a scanner object
Scanner keyboard = new Scanner(System.in);
// Get the starting number of organisms
System.out.println("Starting number of organisms: ");
startingOrganisims = keyboard.nextDouble();
// Input validation for organisms
while (startingOrganisims < 2)
{
System.out.println("You must enter 2 or more organisms: ");
startingOrganisims = keyboard.nextDouble();
}
// Get the percentage of population increase
System.out.println("Average daily population increase as a percentage: ");
percentPopIncrease = keyboard.nextDouble();
// Input validation for percentage
while (percentPopIncrease < 0)
{
System.out.println("You must enter a positive percentage: ");
percentPopIncrease = keyboard.nextDouble();
}
// Get the number of days
System.out.println("How many days should the experiment run? ");
multiplyDays = keyboard.nextDouble();
// Input validation for days
while (multiplyDays < 1)
{
System.out.println("You must enter 1 or more days: ");
multiplyDays = keyboard.nextDouble();
}
// Set intital value for dailyPopulation
dailyPopulation = startingOrganisims;
// Begin a loop to calculate and display the results
for (double days = 0; days <= multiplyDays; days++)
{
dailyPopulation = dailyPopulation + (dailyPopulation * (percentPopIncrease / 100));
System.out.printf("On day " + days + ", the population will be: " + dailyPopulation + "\n");
}
}
}