public class SpeciesFourthTry
1:
2: import java.util.Scanner;
3:
4: public class SpeciesFourthTry
5: {
6: private String name;
7: private int population;
8: private double growthRate;
9:
10: public void readInput( )
11: {
12: Scanner keyboard = new Scanner(System.in);
13: System.out.println("What is the species' name?");
14: name = keyboard.nextLine( );
15:
16: System.out.println("What is the population of the species?");
17: keyboard = new Scanner(System.in);
18: population = keyboard.nextInt( );
19:
20: System.out.println("Enter growth rate (% increase per year):");
21: growthRate = keyboard.nextDouble( );
22: }
23:
24: public void writeOutput( )
25: {
26: System.out.println("Name = " + name);
27: System.out.println("Population = " + population);
28: System.out.println("Growth rate = " + growthRate + "%");
29: }
30:
31: /**
32: Precondition: years is a nonnegative number.
33: Returns the projected population of the calling object
34: after the specified number of years.
35: */
36: public int predictPopulation(int years)
37: {
38: int result = 0;
39: double populationAmount = population;
40: int count = years;
41: while ((count > 0) && (populationAmount > 0))
42: {
43: populationAmount = (populationAmount +
44: (growthRate / 100) * populationAmount);
45: count--;
46: }
47: if (populationAmount > 0)
48: result = (int)populationAmount;
49:
50: return result;
51: }
52:
53: public void setSpecies(String newName,
54: int newPopulation, double newGrowthRate)
55: {
56: name = newName;
57: if (newPopulation >= 0)
58: population = newPopulation;
59: else
60: {
61: System.out.println(
62: "ERROR: using a negative population.");
63: System.exit(0);
64: }
65: growthRate = newGrowthRate;
66: }
67:
68: public String getName( )
69: {
70: return name;
71: }
72:
73: public int getPopulation( )
74: {
75: return population;
76: }
77:
78: public double getGrowthRate( )
79: {
80: return growthRate;
81: }
82: }