|
001 packagepackage is used to name the directory or folder a class is in scg.ch11;
002
003 importimport means to make the classes and/or packages available in this program java.io.PrintWriter;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005
006 /**
007 * A {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace is a type of {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace, a page with
008 * two different "following" pages: a yes page and a no page. Because it
009 * isa {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace, a {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace also has a parent (the
010 * page which led us here), text (the question), and a reference to the
011 * book in which thisthis means the current object (the implicit parameter) page appears.
012 */
013 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 Question
014 extendsextends means to customize or extend the functionality of a class AdventurePage {open braces start code blocks and must be matched with a close brace
015 /** reference to the no tree */
016 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer noNdx;
017
018 /** reference to the yes tree */
019 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer yesNdx;
020
021 /**
022 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace from values read from the file to
023 * which the given {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace is attached.
024 *
025 * @paramthis is the Javadoc tag for documenting the purpose of parameters book the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace to which thisthis means the current object (the implicit parameter) page
026 * belongs
027 * @paramthis is the Javadoc tag for documenting the purpose of parameters scanner input {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace attached to an {open braces start code blocks and must be matched with a close brace@link
028 * AdventureBook}close braces end code blocks and must match an earlier open brace file; must be positioned just after
029 * the type identification line in a {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace
030 * entry.
031 */
032 publicpublic is used to indicate unrestricted access (any other class can have access) Question(AdventureBook book, Scanner scanner) {open braces start code blocks and must be matched with a close brace
033 thisthis means the current object (the implicit parameter)(book, scanner.nextLine(), scanner.nextInt(), scanner.nextInt(),
034 scanner.nextInt());
035 scanner.skip(".*\n");// skip to the end of the line
036 }close braces end code blocks and must match an earlier open brace
037
038 /**
039 * The primary constructor forfor is a looping structure for repeatedly executing a block of code a {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace. Takes the book in
040 * which the question appears, the text of the question, and the
041 * indexes of the parent, yes, and no pages forfor is a looping structure for repeatedly executing a block of code thisthis means the current object (the implicit parameter) question.
042 *
043 * @paramthis is the Javadoc tag for documenting the purpose of parameters book the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace containing thisthis means the current object (the implicit parameter)
044 * question
045 * @paramthis is the Javadoc tag for documenting the purpose of parameters text the text content of thisthis means the current object (the implicit parameter) page
046 * @paramthis is the Javadoc tag for documenting the purpose of parameters parentNdx index (page number) of the page leading to thisthis means the current object (the implicit parameter)
047 * page; NO_SUCH_PAGE ifif executes the next statement only if the condition in parenthesis evaluates to true there is no parent or
048 * parent is unknown.
049 * @paramthis is the Javadoc tag for documenting the purpose of parameters yesNdx index (page number) of the page following on a
050 * yes answer. NO_SUCH_PAGE ifif executes the next statement only if the condition in parenthesis evaluates to true there is no yespage
051 * or yes page is unknown; should always be known!this is the not operator, which changes true to false and false to true
052 * @paramthis is the Javadoc tag for documenting the purpose of parameters noNdx index (page number) of the page following on a
053 * no answer. NO_SUCH_PAGE ifif executes the next statement only if the condition in parenthesis evaluates to true there is no no page
054 * or no page is unknown; should always be known!this is the not operator, which changes true to false and false to true
055 */
056 publicpublic is used to indicate unrestricted access (any other class can have access) Question(AdventureBook book, String text, intint is the type for whole numbers and it is short for integer parentNdx,
057 intint is the type for whole numbers and it is short for integer yesNdx, intint is the type for whole numbers and it is short for integer noNdx) {open braces start code blocks and must be matched with a close brace
058 super(book, text, parentNdx);
059 setYesNdx(yesNdx);
060 setNoNdx(noNdx);
061 }close braces end code blocks and must match an earlier open brace
062
063 /**
064 * Play the game by asking thisthis means the current object (the implicit parameter) question. Based on user's response to
065 * the question, continuecontinue terminates the current iteration of the loop and continues to the next iteration play by "turning" to the yes page or the no
066 * page.
067 */
068 @Override
069 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false play() {open braces start code blocks and must be matched with a close brace
070 intint is the type for whole numbers and it is short for integer nextPageNdx =this assignment operator makes the left side equal to the right side NO_SUCH_PAGE;
071 ifif executes the next statement only if the condition in parenthesis evaluates to true (TwentyQuestions.answersYes(getText())) {open braces start code blocks and must be matched with a close brace
072 nextPageNdx =this assignment operator makes the left side equal to the right side getYesNdx();
073 }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
074 nextPageNdx =this assignment operator makes the left side equal to the right side getNoNdx();
075 }close braces end code blocks and must match an earlier open brace
076 AdventurePage nextPage =this assignment operator makes the left side equal to the right side getBook().get(nextPageNdx);
077 returnreturn means to provide the result of the method and/or cease execution of the method immediately nextPage.play();
078 }close braces end code blocks and must match an earlier open brace
079
080 /**
081 * Convert thisthis means the current object (the implicit parameter) question to a string. The processing puts Q: in front
082 * of the question's text and follows it with the yes and no decision
083 * trees, each indented two spaces.
084 *
085 * @returnnull string representation (with indentation) of the
086 * DecisionTree
087 */
088 @Override
089 publicpublic is used to indicate unrestricted access (any other class can have access) String toString() {open braces start code blocks and must be matched with a close brace
090 String yesString =this assignment operator makes the left side equal to the right side getYes().toString();
091 String noString =this assignment operator makes the left side equal to the right side getNo().toString();
092 returnreturn means to provide the result of the method and/or cease execution of the method immediately "Q: " +adds two numbers together or concatenates Strings together getText() +adds two numbers together or concatenates Strings together "\n " +adds two numbers together or concatenates Strings together
093 yesString.replaceAll("\n", "\n ") +adds two numbers together or concatenates Strings together "\n " +adds two numbers together or concatenates Strings together
094 noString.replaceAll("\n", "\n ");
095 }close braces end code blocks and must match an earlier open brace
096
097 /**
098 * Extend the decision tree with a newnew is used to create objects by calling the constructor item. Given the wrong answer
099 * (already stored in the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace), prompt the user forfor is a looping structure for repeatedly executing a block of code
100 * the right answer and enough information to tell the right from the
101 * wrong in future games. Then build a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace and {open braces start code blocks and must be matched with a close brace@link
102 * Answer}close braces end code blocks and must match an earlier open brace structure (a question with the right and wrong answers as
103 * children) and replace the wrong answer in the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace
104 * with the newnew is used to create objects by calling the constructor question.<br />
105 *
106 * @paramthis is the Javadoc tag for documenting the purpose of parameters wrongAnswer reference to an Answer in the tree, the answer
107 * which was guessed incorrectly
108 */
109 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value userProvidesNewAnswer(Answer wrongAnswer) {open braces start code blocks and must be matched with a close brace
110 // Get the name of the correct object
111 String correctAnswerText =this assignment operator makes the left side equal to the right side TwentyQuestions.getLine(
112 "What were you thinking of?");
113
114 // And a question to differentiate right from wrong
115 String differentiatingQuestionText =this assignment operator makes the left side equal to the right side TwentyQuestions.getLine(
116 "What question could differentiate between a \"" +adds two numbers together or concatenates Strings together
117 wrongAnswer.getText() +adds two numbers together or concatenates Strings together "\" and a \"" +adds two numbers together or concatenates Strings together correctAnswerText +adds two numbers together or concatenates Strings together
118 "\"?");
119
120 // And whether "yes" means the new object or not
121 booleanboolean is a type that is either true or false newAnswerIsYes =this assignment operator makes the left side equal to the right side TwentyQuestions.answersYes(
122 "Which answer means \"" +adds two numbers together or concatenates Strings together correctAnswerText +adds two numbers together or concatenates Strings together
123 "\" is the right answer?");
124
125 // Make a new answer and a new question. Attach the two answers,
126 // right and wrong, to the yes/no or no/yes positions of the new
127 // question (according to what the player told us)
128 Answer correctAnswer =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Answer(getBook(), correctAnswerText,
129 NO_SUCH_PAGE);
130
131 Answer newYes =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
132 Answer newNo =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
133 ifif executes the next statement only if the condition in parenthesis evaluates to true (newAnswerIsYes) {open braces start code blocks and must be matched with a close brace
134 newYes =this assignment operator makes the left side equal to the right side correctAnswer;
135 newNo =this assignment operator makes the left side equal to the right side wrongAnswer;
136 }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
137 newYes =this assignment operator makes the left side equal to the right side wrongAnswer;
138 newNo =this assignment operator makes the left side equal to the right side correctAnswer;
139 }close braces end code blocks and must match an earlier open brace
140
141 Question differentiatingQuestion =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Question(getBook(),
142 differentiatingQuestionText, getNdx(), newYes.getNdx(),
143 newNo.getNdx());
144
145 // Fix up the last question so instead of guessing wrongAnswer, we
146 // ask differentiatingQuestion. Need to know whether the wrongAnswer
147 // came from the yes or the no branch of the last question
148 ifif executes the next statement only if the condition in parenthesis evaluates to true (isYesChild(wrongAnswer.getNdx())) {open braces start code blocks and must be matched with a close brace
149 setYesNdx(differentiatingQuestion.getNdx());
150 }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
151 setNoNdx(differentiatingQuestion.getNdx());
152 }close braces end code blocks and must match an earlier open brace
153 }close braces end code blocks and must match an earlier open brace
154
155 /**
156 * Get a reference to the no page.
157 *
158 * @returnnull the actual page following thisthis means the current object (the implicit parameter) one on answer of no.
159 */
160 privateprivate is used to restrict access to the current class only AdventurePage getNo() {open braces start code blocks and must be matched with a close brace
161 returnreturn means to provide the result of the method and/or cease execution of the method immediately getBook().get(getNoNdx());
162 }close braces end code blocks and must match an earlier open brace
163
164 /**
165 * Get the index (page number) of the page following thisthis means the current object (the implicit parameter) one on an
166 * answer of "no".
167 *
168 * @returnnull index of the page following thisthis means the current object (the implicit parameter) one (in the book) ifif executes the next statement only if the condition in parenthesis evaluates to true the
169 * answer is "no"
170 */
171 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer getNoNdx() {open braces start code blocks and must be matched with a close brace
172 returnreturn means to provide the result of the method and/or cease execution of the method immediately noNdx;
173 }close braces end code blocks and must match an earlier open brace
174
175 /**
176 * Get a reference to the yes page.
177 *
178 * @returnnull the actual page following thisthis means the current object (the implicit parameter) one on answer of yes.
179 */
180 privateprivate is used to restrict access to the current class only AdventurePage getYes() {open braces start code blocks and must be matched with a close brace
181 returnreturn means to provide the result of the method and/or cease execution of the method immediately getBook().get(getYesNdx());
182 }close braces end code blocks and must match an earlier open brace
183
184 /**
185 * Get the index (page number) of the page following thisthis means the current object (the implicit parameter) one on an
186 * answer of "yes".
187 *
188 * @returnnull index of the page following thisthis means the current object (the implicit parameter) one (in the book) ifif executes the next statement only if the condition in parenthesis evaluates to true the
189 * answer is "yes"
190 */
191 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer getYesNdx() {open braces start code blocks and must be matched with a close brace
192 returnreturn means to provide the result of the method and/or cease execution of the method immediately yesNdx;
193 }close braces end code blocks and must match an earlier open brace
194
195 /**
196 * Does queryYesNdx refer to (index) the same {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace as
197 * the yesNdx field of thisthis means the current object (the implicit parameter) {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace?
198 *
199 * @paramthis is the Javadoc tag for documenting the purpose of parameters queryYesNdx index of the page to check against yesNdx
200 *
201 * @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 yesNdx and queryYesNdx are identically the same;
202 * falsefalse is a value for the boolean type and means not true otherwise
203 */
204 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isYesChild(intint is the type for whole numbers and it is short for integer queryYesNdx) {open braces start code blocks and must be matched with a close brace
205 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getYesNdx() ==this is the comparison operator which evaluates to true if both sides are the same queryYesNdx);
206 }close braces end code blocks and must match an earlier open brace
207
208 /**
209 * Set the index of the no page to the given value. If the value is
210 * valid (already in the book), make sure to update the no page's
211 * parent to be the current page.
212 *
213 * @paramthis is the Javadoc tag for documenting the purpose of parameters noNdx index of page following thisthis means the current object (the implicit parameter) one on an answer of
214 * "no"
215 */
216 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setNoNdx(intint is the type for whole numbers and it is short for integer noNdx) {open braces start code blocks and must be matched with a close brace
217 thisthis means the current object (the implicit parameter).noNdx =this assignment operator makes the left side equal to the right side noNdx;
218 ifif executes the next statement only if the condition in parenthesis evaluates to true ((noNdx !=this is the not equals operator which evaluates to true if both sides are different NO_SUCH_PAGE) &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 &&) (noNdx < getBook().size())) {open braces start code blocks and must be matched with a close brace
219 getBook().get(noNdx).setParentNdx(getNdx());
220 }close braces end code blocks and must match an earlier open brace
221 }close braces end code blocks and must match an earlier open brace
222
223 /**
224 * Set the index of the yes page to the given value. If the value is
225 * valid (already in the book), make sure to update the yes page's
226 * parent to be the current page.
227 *
228 * @paramthis is the Javadoc tag for documenting the purpose of parameters yesNdx index of page following thisthis means the current object (the implicit parameter) one on an answer of
229 * "yes"
230 */
231 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setYesNdx(intint is the type for whole numbers and it is short for integer yesNdx) {open braces start code blocks and must be matched with a close brace
232 thisthis means the current object (the implicit parameter).yesNdx =this assignment operator makes the left side equal to the right side yesNdx;
233 ifif executes the next statement only if the condition in parenthesis evaluates to true ((yesNdx !=this is the not equals operator which evaluates to true if both sides are different NO_SUCH_PAGE) &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 &&) (yesNdx < getBook().size())) {open braces start code blocks and must be matched with a close brace
234 getBook().get(yesNdx).setParentNdx(getNdx());
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
238 /**
239 * Save thisthis means the current object (the implicit parameter) {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace to the given {open braces start code blocks and must be matched with a close brace@link PrintWriter}close braces end code blocks and must match an earlier open brace. Uses
240 * {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace save method forfor is a looping structure for repeatedly executing a block of code first part of the record;
241 * after that prints the indexes forfor is a looping structure for repeatedly executing a block of code yes and no pages on separate
242 * lines.
243 *
244 * @paramthis is the Javadoc tag for documenting the purpose of parameters out open text file with write pointer at the end, just
245 * where thisthis means the current object (the implicit parameter) question should be included.
246 */
247 @Override
248 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value save(PrintWriter out) {open braces start code blocks and must be matched with a close brace
249 super.save(out);
250 out.println(getYesNdx());
251 out.println(getNoNdx());
252 }close braces end code blocks and must match an earlier open brace
253 }close braces end code blocks and must match an earlier open brace
254
255 //Uploaded on Mon Mar 29 21:41:03 EDT 2010
|