/* Craig Persiko - CS 111A
PP4.java - Solution to Practice Problem 4
Jackalope population program
This program calculates jackalope populations after
a specified number of generations pass.
*/
import java.util.Scanner;
class PP4
{
public static void main(String args[])
{
Scanner keyIn = new Scanner(System.in);
int starting_population, pop, ending_population, num_generations;
String calc_again;
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;
for(int g=0; g < num_generations; g++)
{
// pop always equals the current population,
// which is updated each generation
pop += (int)(pop * 0.03);
// make sure we don't subtract a fraction of an animal:
pop -= (int)(pop * 0.01);
}
ending_population = pop;
System.out.println("If you start with " + starting_population
+ " jackalopes and wait " + num_generations
+ " generations,\nyou'll end up with a total of "
+ ending_population + " 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