Source of Species.java


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