public class Species
1: //Species.java
2:
3: import java.util.Scanner;
4:
5: /**
6: * Class for data on endangered species.
7: * Includes a main method.
8: */
9: public class Species
10: {
11: private String name;
12: private int population;
13: private double growthRate;
14:
15: public void readInput()
16: {
17: Scanner keyboard = new Scanner(System.in);
18: System.out.println("What is the species' name?");
19: name = keyboard.nextLine();
20:
21: System.out.println("What is the population of the species?");
22: population = keyboard.nextInt();
23: while (population < 0)
24: {
25: System.out.println("Population cannot be negative.");
26: System.out.println("Reenter population:");
27: population = keyboard.nextInt();
28: }
29:
30: System.out.println("Enter growth rate (% increase per year):");
31: growthRate = keyboard.nextDouble();
32: }
33:
34: public void writeOutput()
35: {
36: System.out.println("Name = " + name);
37: System.out.println("Population = " + population);
38: System.out.println("Growth rate = " + growthRate + "%");
39: }
40:
41: /**
42: Precondition: years is a nonnegative number.
43: Returns the projected population of the calling object
44: after the specified number of years.
45: */
46: public int predictPopulation
47: (
48: int years
49: )
50: {
51: int result = 0;
52: double populationAmount = population;
53: int count = years;
54: while ((count > 0) && (populationAmount > 0))
55: {
56: populationAmount = (populationAmount +
57: (growthRate / 100) * populationAmount);
58: count--;
59: }
60: if (populationAmount > 0)
61: result = (int)populationAmount;
62:
63: return result;
64: }
65:
66: public void setSpecies
67: (
68: String newName,
69: int newPopulation,
70: double newGrowthRate
71: )
72: {
73: name = newName;
74: if (newPopulation >= 0)
75: population = newPopulation;
76: else
77: {
78: System.out.println("ERROR: using a negative population.");
79: System.exit(0);
80: }
81: growthRate = newGrowthRate;
82: }
83:
84: public String getName()
85: {
86: return name;
87: }
88:
89: public int getPopulation()
90: {
91: return population;
92: }
93:
94: public double getGrowthRate()
95: {
96: return growthRate;
97: }
98:
99: public boolean equals
100: (
101: Species otherObject
102: )
103: {
104: return (name.equalsIgnoreCase(otherObject.name)) &&
105: (population == otherObject.population) &&
106: (growthRate == otherObject.growthRate);
107: }
108:
109: public static void main(String[] args)
110: {
111: Species speciesToday = new Species();
112: System.out.println("Enter data on today's species:");
113: speciesToday.readInput();
114: speciesToday.writeOutput();
115: System.out.println("Enter number of years to project:");
116: Scanner keyboard = new Scanner(System.in);
117: int numberOfYears = keyboard.nextInt();
118:
119: int futurePopulation = speciesToday.predictPopulation(numberOfYears);
120: System.out.println("In " + numberOfYears
121: + " years the population will be " + futurePopulation);
122: speciesToday.setSpecies("Klingon ox", 10, 15);
123: System.out.println("The new species is:");
124: speciesToday.writeOutput();
125: }
126: }