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