|
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.awt.Color;
004
005 importimport means to make the classes and/or packages available in this program fang2.core.Game;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.CompositeSprite;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
008 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
009
010 /**
011 * A {open braces start code blocks and must be matched with a close brace@code Person}close braces end code blocks and must match an earlier open brace forfor is a looping structure for repeatedly executing a block of code use in a flu simulator; a person is just their
012 * current state of health and the number of days they have been that
013 * way (dead and healthy people don't track how longlong is the type for whole numbers (they have a larger range than int) they have had their
014 * condition)
015 */
016 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 Person
017 extendsextends means to customize or extend the functionality of a class CompositeSprite {open braces start code blocks and must be matched with a close brace
018 /** Number of days spent in each of the states of health */
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 DAYS_SICK =this assignment operator makes the left side equal to the right side 1;
020 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 DAYS_CONTAGIOUS =this assignment operator makes the left side equal to the right side 3;
021 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 DAYS_RECOVERING =this assignment operator makes the left side equal to the right side 3;
022
023 /** Chance of infection and death */
024 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 INFECTION_CHANCE =this assignment operator makes the left side equal to the right side 0.50;
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 MORTALITY_RATE =this assignment operator makes the left side equal to the right side 0.20;
026
027 /** states of health. */
028 privateprivate is used to restrict access to the current class only 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 DEAD =this assignment operator makes the left side equal to the right side -1;
029 privateprivate is used to restrict access to the current class only 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 HEALTHY =this assignment operator makes the left side equal to the right side 0;
030 privateprivate is used to restrict access to the current class only 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 SICK =this assignment operator makes the left side equal to the right side 1;
031 privateprivate is used to restrict access to the current class only 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 CONTAGIOUS =this assignment operator makes the left side equal to the right side 2;
032 privateprivate is used to restrict access to the current class only 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 RECOVERING =this assignment operator makes the left side equal to the right side 3;
033
034 /** Color constants forfor is a looping structure for repeatedly executing a block of code each of the states of health */
035 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color COLOR_DEAD =this assignment operator makes the left side equal to the right side Game.getColor("dark gray");
036 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color COLOR_HEALTHY =this assignment operator makes the left side equal to the right side Game.getColor("green");
037 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color COLOR_SICK =this assignment operator makes the left side equal to the right side Game.getColor("green yellow");
038 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color COLOR_CONTAGIOUS =this assignment operator makes the left side equal to the right side
039 Game.getColor("goldenrod");
040 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color COLOR_RECOVERING =this assignment operator makes the left side equal to the right side
041 Game.getColor("yellow green");
042
043 /** visible body parts; colored to indicate health state */
044 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite body;
045 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) OvalSprite head;
046
047 /** current state of health; drawn from states above */
048 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer health;
049
050 /** days a person has been sick (or greater) */
051 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer sickDay;
052
053 /**
054 * Initialize a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@code Person}close braces end code blocks and must match an earlier open brace. Creates visible shapes and uses
055 * makeHealty to initialize fields and colors.
056 */
057 publicpublic is used to indicate unrestricted access (any other class can have access) Person() {open braces start code blocks and must be matched with a close brace
058 body =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(0.5, 0.5);
059 body.setLocation(0.0, 0.25);
060 head =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.5, 0.5);
061 head.setLocation(0.0, -0.25);
062 addSprite(head);
063 addSprite(body);
064 makeHealthy();
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * This person has been exposed to the disease. Either they get it or
069 * they don't.
070 */
071 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value expose() {open braces start code blocks and must be matched with a close brace
072 ifif executes the next statement only if the condition in parenthesis evaluates to true (isHealthy() &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 &&)
073 (Game.getCurrentGame().randomDouble() < INFECTION_CHANCE)) {open braces start code blocks and must be matched with a close brace
074 makeSick();
075 }close braces end code blocks and must match an earlier open brace
076 }close braces end code blocks and must match an earlier open brace
077
078 /**
079 * Randomly determine ifif executes the next statement only if the condition in parenthesis evaluates to true killed by disease. Transition to healthy or
080 * dead depending on mortality.
081 */
082 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value finishRecovery() {open braces start code blocks and must be matched with a close brace
083 ifif executes the next statement only if the condition in parenthesis evaluates to true (Game.getCurrentGame().randomDouble() < MORTALITY_RATE) {open braces start code blocks and must be matched with a close brace
084 makeDead();
085 }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
086 makeHealthy();
087 }close braces end code blocks and must match an earlier open brace
088 }close braces end code blocks and must match an earlier open brace
089
090 /**
091 * Is thisthis means the current object (the implicit parameter) person CONTAGIOUS?
092 *
093 * @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 health is CONTAGIOUS; falsefalse is a value for the boolean type and means not true otherwise
094 */
095 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isContagious() {open braces start code blocks and must be matched with a close brace
096 returnreturn means to provide the result of the method and/or cease execution of the method immediately health ==this is the comparison operator which evaluates to true if both sides are the same CONTAGIOUS;
097 }close braces end code blocks and must match an earlier open brace
098
099 /**
100 * Is thisthis means the current object (the implicit parameter) person DEAD?
101 *
102 * @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 health is DEAD; falsefalse is a value for the boolean type and means not true otherwise
103 */
104 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isDead() {open braces start code blocks and must be matched with a close brace
105 returnreturn means to provide the result of the method and/or cease execution of the method immediately health ==this is the comparison operator which evaluates to true if both sides are the same DEAD;
106 }close braces end code blocks and must match an earlier open brace
107
108 /**
109 * Is thisthis means the current object (the implicit parameter) person HEALTHY?
110 *
111 * @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 health is HEALTHY; falsefalse is a value for the boolean type and means not true otherwise
112 */
113 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isHealthy() {open braces start code blocks and must be matched with a close brace
114 returnreturn means to provide the result of the method and/or cease execution of the method immediately health ==this is the comparison operator which evaluates to true if both sides are the same HEALTHY;
115 }close braces end code blocks and must match an earlier open brace
116
117 /**
118 * Is thisthis means the current object (the implicit parameter) person RECOVERING?
119 *
120 * @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 health is RECOVERING; falsefalse is a value for the boolean type and means not true otherwise
121 */
122 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isRecovering() {open braces start code blocks and must be matched with a close brace
123 returnreturn means to provide the result of the method and/or cease execution of the method immediately health ==this is the comparison operator which evaluates to true if both sides are the same RECOVERING;
124 }close braces end code blocks and must match an earlier open brace
125
126 /**
127 * Is thisthis means the current object (the implicit parameter) person SICK?
128 *
129 * @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 health is SICK; falsefalse is a value for the boolean type and means not true otherwise
130 */
131 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isSick() {open braces start code blocks and must be matched with a close brace
132 returnreturn means to provide the result of the method and/or cease execution of the method immediately health ==this is the comparison operator which evaluates to true if both sides are the same SICK;
133 }close braces end code blocks and must match an earlier open brace
134
135 /**
136 * Set the person's state to CONTAGIOUS. Update health, sickDay, and
137 * color.
138 */
139 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeContagious() {open braces start code blocks and must be matched with a close brace
140 sickDay =this assignment operator makes the left side equal to the right side 0;
141 health =this assignment operator makes the left side equal to the right side CONTAGIOUS;
142 setColor(COLOR_CONTAGIOUS);
143 }close braces end code blocks and must match an earlier open brace
144
145 /**
146 * Set thisthis means the current object (the implicit parameter) person's state to DEAD. Update health, sickDay, and color.
147 */
148 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeDead() {open braces start code blocks and must be matched with a close brace
149 sickDay =this assignment operator makes the left side equal to the right side 0;
150 health =this assignment operator makes the left side equal to the right side DEAD;
151 setColor(COLOR_DEAD);
152 }close braces end code blocks and must match an earlier open brace
153
154 /**
155 * Set thisthis means the current object (the implicit parameter) person's state to HEALTHY. Update health, sickDay, and
156 * color.
157 */
158 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeHealthy() {open braces start code blocks and must be matched with a close brace
159 sickDay =this assignment operator makes the left side equal to the right side 0;
160 health =this assignment operator makes the left side equal to the right side HEALTHY;
161 setColor(COLOR_HEALTHY);
162 }close braces end code blocks and must match an earlier open brace
163
164 /**
165 * Set the state to RECOVERING. Update health, sickDay, and color.
166 */
167 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeRecovering() {open braces start code blocks and must be matched with a close brace
168 sickDay =this assignment operator makes the left side equal to the right side 0;
169 health =this assignment operator makes the left side equal to the right side RECOVERING;
170 setColor(COLOR_RECOVERING);
171 }close braces end code blocks and must match an earlier open brace
172
173 /**
174 * Set the person's state to SICK. Update health, sickDay, and color.
175 */
176 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeSick() {open braces start code blocks and must be matched with a close brace
177 sickDay =this assignment operator makes the left side equal to the right side 0;
178 health =this assignment operator makes the left side equal to the right side SICK;
179 setColor(COLOR_SICK);
180 }close braces end code blocks and must match an earlier open brace
181
182 /**
183 * Move forward one simulated day: ifif executes the next statement only if the condition in parenthesis evaluates to true (healthy or dead) dodo is part of the do-while looping structure (post condition loop) nothing;
184 * elseelse is what happens when the if condition is false add one to sickDay and advance illness accordingly.
185 */
186 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value nextDay() {open braces start code blocks and must be matched with a close brace
187 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueisHealthy() &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 &&) !this is the not operator, which changes true to false and false to trueisDead()) {open braces start code blocks and must be matched with a close brace
188 ++this is the increment operator, which increases the variable by 1sickDay;
189
190 ifif executes the next statement only if the condition in parenthesis evaluates to true (isSick() &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 &&) (sickDay >=this evaluates to true if the left side is not less than the right side DAYS_SICK)) {open braces start code blocks and must be matched with a close brace
191 makeContagious();
192 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (isContagious() &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 &&) (sickDay >=this evaluates to true if the left side is not less than the right side DAYS_CONTAGIOUS)) {open braces start code blocks and must be matched with a close brace
193 makeRecovering();
194 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (isRecovering() &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 &&) (sickDay >=this evaluates to true if the left side is not less than the right side DAYS_RECOVERING)) {open braces start code blocks and must be matched with a close brace
195 finishRecovery();
196 }close braces end code blocks and must match an earlier open brace
197 }close braces end code blocks and must match an earlier open brace
198 }close braces end code blocks and must match an earlier open brace
199
200 /**
201 * Override the parent {open braces start code blocks and must be matched with a close brace@code setColor}close braces end code blocks and must match an earlier open brace method so that color of the
202 * component parts is set.
203 *
204 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the {open braces start code blocks and must be matched with a close brace@link java.awt.Color Color}close braces end code blocks and must match an earlier open brace to which the
205 * components should be set
206 */
207 @Override
208 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setColor(Color color) {open braces start code blocks and must be matched with a close brace
209 body.setColor(color);
210 head.setColor(color);
211 }close braces end code blocks and must match an earlier open brace
212 }close braces end code blocks and must match an earlier open brace
213
214 //Uploaded on Mon Mar 29 21:42:32 EDT 2010
|