Source of Species.java


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