From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch11;
02
03 importimport means to make the classes and/or packages available in this program java.util.Scanner;
04
05 /**
06 * An Answer page in an {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. An Answer has no
07 * children, no following pages. Instead it just has a guess (the text
08 * on the page). The {open braces start code blocks and must be matched with a close brace@link #play()}close braces end code blocks and must match an earlier open brace method can construct a yes/no
09 * question out of the guess.
10 */
11 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 Answer
12 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
13 /**
14 * 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 Answer}close braces end code blocks and must match an earlier open brace object by reading fields from the
15 * given file.
16 *
17 * @paramthis is the Javadoc tag for documenting the purpose of parameters scanner an open {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 associated with an input
18 * file containing a saved {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
19 */
20 publicpublic is used to indicate unrestricted access (any other class can have access) Answer(AdventureBook book, Scanner scanner) {open braces start code blocks and must be matched with a close brace
21 thisthis means the current object (the implicit parameter)(book, scanner.nextLine(), scanner.nextInt());
22 scanner.skip(".*\n");// skip to the end of the line
23 }close braces end code blocks and must match an earlier open brace
24
25 /**
26 * Construct an answer with the given text
27 *
28 * @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
29 * belongs
30 * @paramthis is the Javadoc tag for documenting the purpose of parameters text the text of the question
31 * @paramthis is the Javadoc tag for documenting the purpose of parameters parentNdx the index number of the parent page (how we got
32 * here)
33 */
34 publicpublic is used to indicate unrestricted access (any other class can have access) Answer(AdventureBook book, String text, intint is the type for whole numbers and it is short for integer parentNdx) {open braces start code blocks and must be matched with a close brace
35 super(book, text, parentNdx);
36 }close braces end code blocks and must match an earlier open brace
37
38 /**
39 * Play thisthis means the current object (the implicit parameter) {open braces start code blocks and must be matched with a close brace@link Answer}close braces end code blocks and must match an earlier open brace: generate a guess question and ask the
40 * player; ifif executes the next statement only if the condition in parenthesis evaluates to true the guess is right, 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 (the game was won by the
41 * computer); otherwise get information from the player to improved
42 * the quality of 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) answer
43 * belongs and 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 (the game was lost by the computer).
44 *
45 * @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 machine guessed right; falsefalse is a value for the boolean type and means not true otherwise
46 */
47 @Override
48 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
49 String guess =this assignment operator makes the left side equal to the right side "Is it a(n) " +adds two numbers together or concatenates Strings together getText() +adds two numbers together or concatenates Strings together "?";
50 ifif executes the next statement only if the condition in parenthesis evaluates to true (TwentyQuestions.answersYes(guess)) {open braces start code blocks and must be matched with a close brace
51 System.out.println("I am so smart! I win!!!");
52 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;
53 }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
54 System.out.println("Darn, you stumped me.");
55 getParent().userProvidesNewAnswer(thisthis means the current object (the implicit parameter));
56 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;
57 }close braces end code blocks and must match an earlier open brace
58 }close braces end code blocks and must match an earlier open brace
59
60 /**
61 * Get a string representation of thisthis means the current object (the implicit parameter) answer. A: before the text
62 * content of the string
63 *
64 * @returnnull string representation of thisthis means the current object (the implicit parameter) Answer
65 */
66 @Override
67 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
68 returnreturn means to provide the result of the method and/or cease execution of the method immediately "A: " +adds two numbers together or concatenates Strings together getText();
69 }close braces end code blocks and must match an earlier open brace
70 }close braces end code blocks and must match an earlier open brace
71
72 //Uploaded on Mon Mar 29 21:42:01 EDT 2010
|
Download/View scg/ch11/Answer.java