|
001 packagepackage is used to name the directory or folder a class is in scg.ch08;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004
005 importimport means to make the classes and/or packages available in this program fang2.core.Alarm;
006 importimport means to make the classes and/or packages available in this program fang2.core.Game;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
008
009 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects FluSimulator
010 extendsextends means to customize or extend the functionality of a class Game
011 implementsimplements means providing method bodies for the methods declared in the corresponding interface Alarm {open braces start code blocks and must be matched with a close brace
012 /** Number of neighbors exposed to a contagious villager per day */
013 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer EXPOSED_PER_DAY =this assignment operator makes the left side equal to the right side 3;
014
015 /** Maximum neighbor visiting distance */
016 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NEIGHBOR_DISTANCE =this assignment operator makes the left side equal to the right side 3;
017
018 /** Patients zero are initially ill patients. */
019 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NUMBER_OF_PATIENTS_ZERO =this assignment operator makes the left side equal to the right side 2;
020
021 /** The number of individuals in the village */
022 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NUMBER_OF_VILLAGERS =this assignment operator makes the left side equal to the right side 20;
023
024 /** Clock seconds per simulated day */
025 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions SECONDS_PER_DAY =this assignment operator makes the left side equal to the right side 1.0;
026
027 /** Number of days elapsed since the beginning of the simulation. */
028 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer dayCount;
029
030 /** String sprite assigned to display the day count. */
031 privateprivate is used to restrict access to the current class only StringSprite dayCountDisplay;
032
033 /** The collection of Person representing the village. */
034 privateprivate is used to restrict access to the current class only ArrayList<Person> village;
035
036 /**
037 * Perform a scheduled action: Advance simulation one day into the
038 * future. This method is required to implement the Action interfaceinterface is a named collection of declared methods.
039 */
040 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value act() {open braces start code blocks and must be matched with a close brace
041 spreadInfection();
042 advanceSimulatedTime();
043 // schedule the next update unless the simulation is quiescent
044 ifif executes the next statement only if the condition in parenthesis evaluates to true (simulationContinues()) {open braces start code blocks and must be matched with a close brace// schedule a new call in the future
045 scheduleRelative(thisthis means the current object (the implicit parameter), SECONDS_PER_DAY);
046 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
047 endgameStatistics();
048 }close braces end code blocks and must match an earlier open brace
049 }close braces end code blocks and must match an earlier open brace
050
051 /**
052 * Initialize the game: Construct the villagers and put them in the
053 * village. Add the day counting variables.
054 */
055 @Override
056 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
057 // how big is each villager (so they all fit in one line)
058 doubledouble is the type for numbers that can contain decimal fractions scale =this assignment operator makes the left side equal to the right side 1.0 / NUMBER_OF_VILLAGERS;
059
060 // set aside space for the collection (no Persons yet)
061 village =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Person>();
062
063 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i < NUMBER_OF_VILLAGERS; ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
064 // construct one person
065 Person nextVillager =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Person();
066 nextVillager.setScale(scale);
067 nextVillager.setLocation((i * scale) +adds two numbers together or concatenates Strings together (scale / 2), 0.5);
068 addSprite(nextVillager);
069
070 // add one person to village
071 village.add(nextVillager);
072 }close braces end code blocks and must match an earlier open brace
073
074 dayCount =this assignment operator makes the left side equal to the right side 1;
075 dayCountDisplay =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
076 dayCountDisplay.setScale(0.1);
077 dayCountDisplay.leftJustify();
078 dayCountDisplay.setLocation(0.2, 0.1);
079 dayCountDisplay.setText("Day #" +adds two numbers together or concatenates Strings together Integer.toString(dayCount));
080 addSprite(dayCountDisplay);
081
082 infectPatientsZero();
083
084 scheduleRelative(thisthis means the current object (the implicit parameter), SECONDS_PER_DAY);
085 }close braces end code blocks and must match an earlier open brace
086
087 /**
088 * Advance simulation to the next day. Tell each villager to advance
089 * their health and update the day count.
090 */
091 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value advanceSimulatedTime() {open braces start code blocks and must be matched with a close brace
092 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer currentVillagerNdx =this assignment operator makes the left side equal to the right side 0;
093 currentVillagerNdx !=this is the not equals operator which evaluates to true if both sides are different village.size(); ++this is the increment operator, which increases the variable by 1currentVillagerNdx) {open braces start code blocks and must be matched with a close brace
094 Person villager =this assignment operator makes the left side equal to the right side village.get(currentVillagerNdx);
095
096 villager.nextDay();
097 }close braces end code blocks and must match an earlier open brace
098 // increment the day count and display it
099 ++this is the increment operator, which increases the variable by 1dayCount;
100 dayCountDisplay.setText("Day #" +adds two numbers together or concatenates Strings together Integer.toString(dayCount));
101 }close braces end code blocks and must match an earlier open brace
102
103 /**
104 * Are any villagers neither healthy nor dead?
105 *
106 * @returnnull falsefalse is a value for the boolean type and means not true ifif executes the next statement only if the condition in parenthesis evaluates to true all villagers are healthy or dead; truetrue is the boolean value that is the opposite of false otherwise
107 */
108 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false anySick() {open braces start code blocks and must be matched with a close brace
109 booleanboolean is a type that is either true or false sawSick =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
110
111 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer currentVillagerNdx =this assignment operator makes the left side equal to the right side 0;
112 currentVillagerNdx !=this is the not equals operator which evaluates to true if both sides are different village.size(); ++this is the increment operator, which increases the variable by 1currentVillagerNdx) {open braces start code blocks and must be matched with a close brace
113 Person villager =this assignment operator makes the left side equal to the right side village.get(currentVillagerNdx);
114 sawSick =this assignment operator makes the left side equal to the right side sawSick ||this is boolean or, meaning if either or both are true then the result is true villager.isSick() ||this is boolean or, meaning if either or both are true then the result is true
115 villager.isContagious() ||this is boolean or, meaning if either or both are true then the result is true villager.isRecovering();
116 }close braces end code blocks and must match an earlier open brace
117
118 returnreturn means to provide the result of the method and/or cease execution of the method immediately sawSick;
119 }close braces end code blocks and must match an earlier open brace
120
121 /**
122 * How many villagers are currently dead? Iterate across the village,
123 * counting elements which meet some criteria (isDead() returns truetrue is the boolean value that is the opposite of false)
124 *
125 * @returnnull the number of dead villagers
126 */
127 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer countOfTheDead() {open braces start code blocks and must be matched with a close brace
128 intint is the type for whole numbers and it is short for integer numberOfDead =this assignment operator makes the left side equal to the right side 0;
129
130 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer currentVillagerNdx =this assignment operator makes the left side equal to the right side 0;
131 currentVillagerNdx !=this is the not equals operator which evaluates to true if both sides are different village.size(); ++this is the increment operator, which increases the variable by 1currentVillagerNdx) {open braces start code blocks and must be matched with a close brace
132 Person villager =this assignment operator makes the left side equal to the right side village.get(currentVillagerNdx);
133
134 ifif executes the next statement only if the condition in parenthesis evaluates to true (villager.isDead()) {open braces start code blocks and must be matched with a close brace
135 ++this is the increment operator, which increases the variable by 1numberOfDead;
136 }close braces end code blocks and must match an earlier open brace
137 }close braces end code blocks and must match an earlier open brace
138
139 returnreturn means to provide the result of the method and/or cease execution of the method immediately numberOfDead;
140 }close braces end code blocks and must match an earlier open brace
141
142 /**
143 * Display end of game statistics on the screen.
144 */
145 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value endgameStatistics() {open braces start code blocks and must be matched with a close brace
146 StringSprite announcement =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
147 announcement.setLocation(0.5, 0.7);
148 announcement.setColor(getColor("misty rose"));
149 announcement.setText("Dead: " +adds two numbers together or concatenates Strings together countOfTheDead() +adds two numbers together or concatenates Strings together " of " +adds two numbers together or concatenates Strings together
150 NUMBER_OF_VILLAGERS);
151 announcement.setWidth(0.9);
152 addSprite(announcement);
153 }close braces end code blocks and must match an earlier open brace
154
155 /**
156 * Expose a contagious villager, at contagiousVillagerNdx, to randomly
157 * chosen neighbors.
158 *
159 * @paramthis is the Javadoc tag for documenting the purpose of parameters contagiousVillagerNdx
160 */
161 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleContagiousVillager(intint is the type for whole numbers and it is short for integer contagiousVillagerNdx) {open braces start code blocks and must be matched with a close brace
162 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer numberOfExposed =this assignment operator makes the left side equal to the right side 0; numberOfExposed !=this is the not equals operator which evaluates to true if both sides are different EXPOSED_PER_DAY;
163 ++this is the increment operator, which increases the variable by 1numberOfExposed) {open braces start code blocks and must be matched with a close brace
164 intint is the type for whole numbers and it is short for integer exposedVillagerNdx =this assignment operator makes the left side equal to the right side selectNeighbor(contagiousVillagerNdx);
165
166 ifif executes the next statement only if the condition in parenthesis evaluates to true (isValidNdx(exposedVillagerNdx)) {open braces start code blocks and must be matched with a close brace
167 village.get(exposedVillagerNdx).expose();
168 }close braces end code blocks and must match an earlier open brace
169 }close braces end code blocks and must match an earlier open brace
170 }close braces end code blocks and must match an earlier open brace
171
172 /**
173 * Randomly select and infect initial patients. The same patient <i>
174 * could</i> be selected more than once so initial number of the sick
175 * may be < NUMBER_PATIENTS_ZERO.
176 */
177 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value infectPatientsZero() {open braces start code blocks and must be matched with a close brace
178 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer sickies =this assignment operator makes the left side equal to the right side 0; sickies !=this is the not equals operator which evaluates to true if both sides are different NUMBER_OF_PATIENTS_ZERO;
179 ++this is the increment operator, which increases the variable by 1sickies) {open braces start code blocks and must be matched with a close brace
180 village.get(randomInt(NUMBER_OF_VILLAGERS)).makeSick();
181 }close braces end code blocks and must match an earlier open brace
182 }close braces end code blocks and must match an earlier open brace
183
184 /**
185 * Is ndx a valid index into the village?
186 *
187 * @paramthis is the Javadoc tag for documenting the purpose of parameters ndx the index to check
188 *
189 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the index is valid index into village collection;
190 * falsefalse is a value for the boolean type and means not true otherwise.
191 */
192 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isValidNdx(intint is the type for whole numbers and it is short for integer ndx) {open braces start code blocks and must be matched with a close brace
193 returnreturn means to provide the result of the method and/or cease execution of the method immediately (0 <=this evaluates to true if the left side is not more than the right side ndx) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (ndx < village.size());
194 }close braces end code blocks and must match an earlier open brace
195
196 /**
197 * Select a neighbor of the villager at currNdx. Returned value will
198 * be within NEIGHBOR_DISTANCE of currNdx.
199 *
200 * @paramthis is the Javadoc tag for documenting the purpose of parameters currentVillagerNdx the index of the villager forfor is a looping structure for repeatedly executing a block of code whom we
201 * must select a neighbor
202 *
203 * @returnnull an "index" of a neighbor; index in quotes as it might be
204 * outside the legal range forfor is a looping structure for repeatedly executing a block of code village
205 */
206 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer selectNeighbor(intint is the type for whole numbers and it is short for integer currentVillagerNdx) {open braces start code blocks and must be matched with a close brace
207 intint is the type for whole numbers and it is short for integer neighborNdx;
208 neighborNdx =this assignment operator makes the left side equal to the right side currentVillagerNdx +adds two numbers together or concatenates Strings together
209 randomInt(-NEIGHBOR_DISTANCE, NEIGHBOR_DISTANCE);
210
211 returnreturn means to provide the result of the method and/or cease execution of the method immediately neighborNdx;
212 }close braces end code blocks and must match an earlier open brace
213
214 /**
215 * Does the simulation need to keep running?
216 *
217 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true someone's health will change spontaneously; falsefalse is a value for the boolean type and means not true
218 * otherwise
219 */
220 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false simulationContinues() {open braces start code blocks and must be matched with a close brace
221 returnreturn means to provide the result of the method and/or cease execution of the method immediately anySick();
222 }close braces end code blocks and must match an earlier open brace
223
224 /**
225 * Cycle across the village, exposing any contagious villagers to
226 * their neighbors.
227 */
228 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value spreadInfection() {open braces start code blocks and must be matched with a close brace
229 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer currentVillagerNdx =this assignment operator makes the left side equal to the right side 0;
230 currentVillagerNdx !=this is the not equals operator which evaluates to true if both sides are different village.size(); ++this is the increment operator, which increases the variable by 1currentVillagerNdx) {open braces start code blocks and must be matched with a close brace
231 Person villager =this assignment operator makes the left side equal to the right side village.get(currentVillagerNdx);
232 ifif executes the next statement only if the condition in parenthesis evaluates to true (villager.isContagious()) {open braces start code blocks and must be matched with a close brace
233 handleContagiousVillager(currentVillagerNdx);
234 }close braces end code blocks and must match an earlier open brace
235 }close braces end code blocks and must match an earlier open brace
236 }close braces end code blocks and must match an earlier open brace
237 }close braces end code blocks and must match an earlier open brace
238
239 //Uploaded on Mon Mar 29 21:39:14 EDT 2010
|