/* Craig Persiko - CS 111A
PP4.java - Solution to Homework 6
Jackalope population program
This program calculates jackalope populations after
a specified number of generations pass.
*/
import java.util.Scanner;
public class PP4
{
public static void main(String args[])
{
Scanner keyIn = new Scanner(System.in);
int starting_population, pop, num_generations;
String calc_again;
final double BIRTH_RATE = 0.03, DEATH_RATE = 0.01;
do
{
System.out.print("How many jackalopes do you have? ");
starting_population = keyIn.nextInt();
System.out.print("How many generations do you want to wait? ");
num_generations = keyIn.nextInt();
keyIn.nextLine(); // dispose of the newline
pop = starting_population;
// pop always equals the current population, which is updated each generation
for(int g=0; g < num_generations; g++)
{
pop += (int)(pop * BIRTH_RATE);
pop -= (int)(pop * DEATH_RATE); // make sure we don't subtract a fraction of an animal
}
System.out.println("If you start with " + starting_population
+ " jackalopes and wait " + num_generations
+ " generations,\nyou'll end up with a total of "
+ pop + " of them.");
System.out.print("Do you want to calculate another population? ");
calc_again = keyIn.nextLine();
}while(Character.toLowerCase(calc_again.charAt(0)) == 'y');
}
}
/* Sample Output:
How many jackalopes do you have? 200
How many generations do you want to wait? 1
If you start with 200 jackalopes and wait 1 generations,
you'll end up with a total of 204 of them.
Do you want to calculate another population? y
How many jackalopes do you have? 132
How many generations do you want to wait? 2
If you start with 132 jackalopes and wait 2 generations,
you'll end up with a total of 137 of them.
Do you want to calculate another population? y
How many jackalopes do you have? 40
How many generations do you want to wait? 100
If you start with 40 jackalopes and wait 100 generations,
you'll end up with a total of 291 of them.
Do you want to calculate another population? n
*/
syntax highlighted by Code2HTML, v. 0.9