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