Source of Species.java


  1: 
  2: 
  3: import java.util.*;
  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(
 31:                "Enter growth rate (percent 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 projectedPopulation(int years)
 48:     {
 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:             return (int)populationAmount;
 59:         else
 60:             return 0;
 61:     }
 62: 
 63:     public void set(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: }