Source of DemoSpecies.java


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