public class Species
1: //Species.java
2:
3: import java.util.Scanner;
4:
5: /**
6: * Class for data on endangered species.
7: */
8: public class Species
9: {
10: private String name;
11: private int population;
12: private double growthRate;
13:
14: public void readInput()
15: {
16: Scanner keyboard = new Scanner(System.in);
17: System.out.println("What is the species' name?");
18: name = keyboard.nextLine();
19:
20: System.out.println(
21: "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: }