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