Source of DemoSpecies.java


  1: //DemoSpecies.java
  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
 22:     (
 23:         int intVariable
 24:     )
 25:     {
 26:         intVariable = this.population;
 27:     }
 28: 
 29:     /**
 30:      * Tries to make otherObject reference the calling object.
 31:      * But arguments of a class type cannot be replaced.
 32:     */
 33:     public void tryToReplace
 34:     (
 35:         DemoSpecies otherObject
 36:     )
 37:     {
 38:         otherObject = this;
 39:     }
 40: 
 41:     /**
 42:      * Changes the data in otherObject to the data in this object,
 43:          * which is unchanged.
 44:      */
 45:     public void change
 46:     (
 47:         DemoSpecies otherObject
 48:     )
 49:     {
 50:         otherObject.name = this.name;
 51:         otherObject.population = this.population;
 52:         otherObject.growthRate = this.growthRate;
 53:     }
 54: 
 55:     public void readInput()
 56:     {
 57:         Scanner keyboard = new Scanner(System.in);
 58:         System.out.println("What is the species’ name?");
 59:         name = keyboard.nextLine();
 60:         System.out.println("What is the population of the species?");
 61:         keyboard = new Scanner(System.in);
 62:         population = keyboard.nextInt();
 63:         while (population < 0)
 64:         {
 65:             System.out.println("Population cannot be negative.");
 66:             System.out.println("Reenter population:");
 67:             population = keyboard.nextInt();
 68:         }
 69:         System.out.println("Enter growth rate (percent increase per year):");
 70:         growthRate = keyboard.nextDouble();
 71:     }
 72: 
 73:     public void writeOutput()
 74:     {
 75:         System.out.println("Name = " + name);
 76:         System.out.println("Population = " + population);
 77:         System.out.println("Growth rate = " + growthRate + "%");
 78:     }
 79: 
 80:     /**
 81:      * Precondition: years is a nonnegative number.
 82:      * Returns the projected population of the calling object
 83:      * after the specified number of years.
 84:     */
 85:     public int predictPopulation
 86:     (
 87:         int years
 88:     )
 89:     {
 90:         int result = 0;
 91:         double populationAmount = population;
 92:         int count = years;
 93:         while ((count > 0) && (populationAmount > 0))
 94:         {
 95:             populationAmount =
 96:                 (populationAmount + (growthRate / 100) * populationAmount);
 97:             count--;
 98:         }
 99:         if (populationAmount > 0)
100:             result = (int)populationAmount;
101: 
102:         return result;
103:     }
104: 
105:     public void setSpecies
106:     (
107:         String newName,
108:         int newPopulation,
109:         double newGrowthRate
110:     )
111:     {
112:         name = newName;
113:         if (newPopulation >= 0)
114:             population = newPopulation;
115:         else
116:         {
117:             System.out.println("ERROR: using a negative population.");
118:             System.exit(0);
119:         }
120:         growthRate = newGrowthRate;
121:     }
122: 
123:     public String getName()
124:     {
125:         return name;
126:     }
127: 
128:     public int getPopulation()
129:     {
130:         return population;
131:     }
132: 
133:     public double getGrowthRate()
134:     {
135:         return growthRate;
136:     }
137: 
138:     public boolean equals(DemoSpecies otherObject)
139:     {
140:         return (name.equalsIgnoreCase(otherObject.name)) &&
141:                (population == otherObject.population)    &&
142:                (growthRate == otherObject.growthRate);
143:     }
144: }