Source of Species.java


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