From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.gamestuff;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005
006 importimport means to make the classes and/or packages available in this program scg.ch14.core.Game;
007
008 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 Critter
009 extendsextends means to customize or extend the functionality of a class GameObject {open braces start code blocks and must be matched with a close brace
010 /**
011 * Read one critter from the given scanner. The {open braces start code blocks and must be matched with a close brace@link Critter}close braces end code blocks and must match an earlier open brace
012 * factory.
013 *
014 * @paramthis is the Javadoc tag for documenting the purpose of parameters gameObjectScanner scanner open forfor is a looping structure for repeatedly executing a block of code input
015 *
016 * @returnnull the newnew is used to create objects by calling the constructor Critter or nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true there is a problem
017 */
018 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) Critter readObjectFromFile(Scanner gameObjectScanner) {open braces start code blocks and must be matched with a close brace
019 String classID =this assignment operator makes the left side equal to the right side gameObjectScanner.next();
020 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 trueclassID.equalsIgnoreCase("Critter")) {open braces start code blocks and must be matched with a close brace
021 returnreturn means to provide the result of the method and/or cease execution of the method immediately nullnull is the value used to refer to a non-existant object;// not the expected type of object; punt
022 }close braces end code blocks and must match an earlier open brace
023
024 String endClassID =this assignment operator makes the left side equal to the right side "/" +adds two numbers together or concatenates Strings together classID;
025
026 Critter cr =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Critter();
027 GameObject.processAttributes(gameObjectScanner, endClassID, cr);
028 returnreturn means to provide the result of the method and/or cease execution of the method immediately cr;
029 }close braces end code blocks and must match an earlier open brace
030
031 /** Critter dies at 0 health */
032 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;
033
034 /** the list of UUIDs of things that reduce critter's health */
035 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<String> hurtBy;
036
037 /** where is thisthis means the current object (the implicit parameter) critter? */
038 privateprivate is used to restrict access to the current class only Location location;
039 privateprivate is used to restrict access to the current class only String locationID;
040
041 /** list of all items thisthis means the current object (the implicit parameter) critter will trade forfor is a looping structure for repeatedly executing a block of code */
042 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<TradesFor> tradesFor;
043
044 /**
045 * Construct a newnew is used to create objects by calling the constructor, empty critter.
046 */
047 publicpublic is used to indicate unrestricted access (any other class can have access) Critter() {open braces start code blocks and must be matched with a close brace
048 health =this assignment operator makes the left side equal to the right side 0;
049 locationID =this assignment operator makes the left side equal to the right side Location.NO_SUCH_LOCATION;
050 location =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
051 hurtBy =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
052 tradesFor =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<TradesFor>();
053 }close braces end code blocks and must match an earlier open brace
054
055 /**
056 * Add the given item to thisthis means the current object (the implicit parameter) critter's inventory.
057 */
058 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value add(Item item) {open braces start code blocks and must be matched with a close brace
059 super.add(item);
060 ifif executes the next statement only if the condition in parenthesis evaluates to true (hurtBy.indexOf(item.getUUID()) >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
061 health -=this decreases the variable on the left by the value on the right item.getAttackStrength();
062 Game.getCurrentGame().addMessage(getName() +adds two numbers together or concatenates Strings together " hurt by " +adds two numbers together or concatenates Strings together
063 item.getName() +adds two numbers together or concatenates Strings together " for " +adds two numbers together or concatenates Strings together item.getAttackStrength());
064 }close braces end code blocks and must match an earlier open brace
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * Get the critter's current health.
069 * @returnnull the health
070 */
071 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer getHealth() {open braces start code blocks and must be matched with a close brace
072 returnreturn means to provide the result of the method and/or cease execution of the method immediately health;
073 }close braces end code blocks and must match an earlier open brace
074
075 /**
076 * Add a newnew is used to create objects by calling the constructor uuid to the list of things that can hurt thisthis means the current object (the implicit parameter) critter
077 * @paramthis is the Javadoc tag for documenting the purpose of parameters uuid the uuid of item
078 */
079 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value addHurtBy(String uuid) {open braces start code blocks and must be matched with a close brace
080 ifif executes the next statement only if the condition in parenthesis evaluates to true (hurtBy.indexOf(uuid) < 0) {open braces start code blocks and must be matched with a close brace
081 hurtBy.add(uuid);
082 }close braces end code blocks and must match an earlier open brace
083 }close braces end code blocks and must match an earlier open brace
084
085 /**
086 * Add the given TradesFor pair of items to the critter's list of such
087 * pairs.
088 * @paramthis is the Javadoc tag for documenting the purpose of parameters tf the item to add.
089 */
090 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value addTradesFor(TradesFor tf) {open braces start code blocks and must be matched with a close brace
091 ifif executes the next statement only if the condition in parenthesis evaluates to true (tradesFor.indexOf(tf) < 0) {open braces start code blocks and must be matched with a close brace
092 tradesFor.add(tf);
093 }close braces end code blocks and must match an earlier open brace
094 }close braces end code blocks and must match an earlier open brace
095
096 /**
097 * Get thisthis means the current object (the implicit parameter) critter's current location
098 * @returnnull the location
099 */
100 publicpublic is used to indicate unrestricted access (any other class can have access) Location getLocation() {open braces start code blocks and must be matched with a close brace
101 returnreturn means to provide the result of the method and/or cease execution of the method immediately location;
102 }close braces end code blocks and must match an earlier open brace
103
104 /**
105 * Get the UUID of the current owner of thisthis means the current object (the implicit parameter) item.
106 *
107 * @returnnull the UUID of the owner
108 */
109 publicpublic is used to indicate unrestricted access (any other class can have access) String getLocationID() {open braces start code blocks and must be matched with a close brace
110 ifif executes the next statement only if the condition in parenthesis evaluates to true (location !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
111 returnreturn means to provide the result of the method and/or cease execution of the method immediately location.getUUID();
112 }close braces end code blocks and must match an earlier open brace
113 returnreturn means to provide the result of the method and/or cease execution of the method immediately locationID;
114 }close braces end code blocks and must match an earlier open brace
115
116 /**
117 * Check ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter) critter is hurt by the element "named" by the uuid.
118 * @paramthis is the Javadoc tag for documenting the purpose of parameters uuid the item which might hurt us
119 * @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 it can hurt us, falsefalse is a value for the boolean type and means not true otherwise
120 */
121 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isHurtBy(String uuid) {open braces start code blocks and must be matched with a close brace
122 returnreturn means to provide the result of the method and/or cease execution of the method immediately (hurtBy.indexOf(uuid) >=this evaluates to true if the left side is not less than the right side 0);
123 }close braces end code blocks and must match an earlier open brace
124
125 /**
126 * @paramthis is the Javadoc tag for documenting the purpose of parameters location the location to set
127 */
128 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setLocation(Location location) {open braces start code blocks and must be matched with a close brace
129 thisthis means the current object (the implicit parameter).location =this assignment operator makes the left side equal to the right side location;
130 }close braces end code blocks and must match an earlier open brace
131
132 /**
133 * Determine what, ifif executes the next statement only if the condition in parenthesis evaluates to true anything, thisthis means the current object (the implicit parameter) critter will trade forfor is a looping structure for repeatedly executing a block of code the given
134 * item. This NOT modify the critter's inventory.
135 * @paramthis is the Javadoc tag for documenting the purpose of parameters getUUID the offered uuid
136 * @returnnull nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true nothing offered, the offered trade item otherwise
137 */
138 publicpublic is used to indicate unrestricted access (any other class can have access) String willTradeFor(String getUUID) {open braces start code blocks and must be matched with a close brace
139 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 t =this assignment operator makes the left side equal to the right side 0; t !=this is the not equals operator which evaluates to true if both sides are different tradesFor.size(); ++this is the increment operator, which increases the variable by 1t) {open braces start code blocks and must be matched with a close brace
140 TradesFor curr =this assignment operator makes the left side equal to the right side tradesFor.get(t);
141 ifif executes the next statement only if the condition in parenthesis evaluates to true (curr.getGetsUUID().equalsIgnoreCase(getUUID)) {open braces start code blocks and must be matched with a close brace
142 returnreturn means to provide the result of the method and/or cease execution of the method immediately curr.getGivesUUID();
143 }close braces end code blocks and must match an earlier open brace
144 }close braces end code blocks and must match an earlier open brace
145 returnreturn means to provide the result of the method and/or cease execution of the method immediately nullnull is the value used to refer to a non-existant object;
146 }close braces end code blocks and must match an earlier open brace
147
148 /**
149 * @paramthis is the Javadoc tag for documenting the purpose of parameters health the health to set
150 */
151 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setHealth(intint is the type for whole numbers and it is short for integer health) {open braces start code blocks and must be matched with a close brace
152 thisthis means the current object (the implicit parameter).health =this assignment operator makes the left side equal to the right side health;
153 }close braces end code blocks and must match an earlier open brace
154
155 /**
156 * Handle, ifif executes the next statement only if the condition in parenthesis evaluates to true possible, the given name/value pair
157 *
158 * @paramthis is the Javadoc tag for documenting the purpose of parameters attribute name of the attribute
159 * @paramthis is the Javadoc tag for documenting the purpose of parameters value the value of the attribute
160 *
161 * @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 attribute was handled at thisthis means the current object (the implicit parameter) level; falsefalse is a value for the boolean type and means not true
162 * otherwise.
163 */
164 @Override
165 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false handleAttributeValuePair(String attribute,
166 String value) {open braces start code blocks and must be matched with a close brace
167 ifif executes the next statement only if the condition in parenthesis evaluates to true (super.handleAttributeValuePair(attribute, value)) {open braces start code blocks and must be matched with a close brace
168 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
169 }close braces end code blocks and must match an earlier open brace
170
171 ifif executes the next statement only if the condition in parenthesis evaluates to true (attribute.equalsIgnoreCase("location")) {open braces start code blocks and must be matched with a close brace
172 locationID =this assignment operator makes the left side equal to the right side value.toLowerCase();
173 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
174 }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 (attribute.equalsIgnoreCase("health")) {open braces start code blocks and must be matched with a close brace
175 setHealth(Integer.parseInt(value));
176 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
177 }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 (attribute.equalsIgnoreCase("hurtBy")) {open braces start code blocks and must be matched with a close brace
178 addHurtBy(value.toLowerCase());
179 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
180 }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 (attribute.equalsIgnoreCase("tradesFor")) {open braces start code blocks and must be matched with a close brace
181 String gets =this assignment operator makes the left side equal to the right side value.substring(0, value.indexOf(':')).trim().toLowerCase();
182 String gives =this assignment operator makes the left side equal to the right side value.substring(value.indexOf(':')+adds two numbers together or concatenates Strings together1).trim().toLowerCase();
183
184 addTradesFor(newnew is used to create objects by calling the constructor TradesFor(gets, gives));
185 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
186 }close braces end code blocks and must match an earlier open brace
187 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
188 }close braces end code blocks and must match an earlier open brace
189
190 /**
191 * Internal toString helper method.
192 *
193 * @returnnull A string representation of the fields.
194 */
195 @Override
196 protectedprotected is used to restrict access to the current class and subclasses only String toStringGuts() {open braces start code blocks and must be matched with a close brace
197 returnreturn means to provide the result of the method and/or cease execution of the method immediately super.toStringGuts() +adds two numbers together or concatenates Strings together "\n" +adds two numbers together or concatenates Strings together "locationID = " +adds two numbers together or concatenates Strings together
198 getLocationID() +adds two numbers together or concatenates Strings together "\n" +adds two numbers together or concatenates Strings together "health = " +adds two numbers together or concatenates Strings together health;
199 }close braces end code blocks and must match an earlier open brace
200 }close braces end code blocks and must match an earlier open brace
201
202 //Uploaded on Mon Mar 29 21:38:44 EDT 2010
|
Download/View scg/ch14/gamestuff/Critter.java