Source of Species.java


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