|
001 packagepackage is used to name the directory or folder a class is in scg.ch12;
002
003 importimport means to make the classes and/or packages available in this program java.util.Scanner;
004
005 /**
006 * The TwentyQuestions game. Run with a command-line argument of the
007 * data file with the questions and answers. This classclass is a group of fields and methods used for making objects provides two
008 * 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) input methods, {open braces start code blocks and must be matched with a close brace@link #answersYes(String)}close braces end code blocks and must match an earlier open brace which gets a
009 * yes or no answer (returning truetrue is the boolean value that is the opposite of false or falsefalse is a value for the boolean type and means not true) and {open braces start code blocks and must be matched with a close brace@link
010 * #getLine(String)}close braces end code blocks and must match an earlier open brace which gets a line from standard input. These two
011 * input methods should be used by other classes to prompt the user forfor is a looping structure for repeatedly executing a block of code
012 * the input.
013 */
014 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 TwentyQuestions {open braces start code blocks and must be matched with a close brace
015 /** scanner wrapped around standard input */
016 staticstatic means that an instance is not required for access (class level access) Scanner keyboard =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(System.in);
017
018 /** root of the DecisionTree representing the smarts */
019 privateprivate is used to restrict access to the current class only AdventureBook book;
020
021 /**
022 * Print the prompt on the standard output until the user answers
023 * either 'y', 'n', 'yes', or 'no'.
024 *
025 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt to show the user
026 *
027 * @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 user answered 'y' or 'yes'; falsefalse is a value for the boolean type and means not true otherwise
028 */
029 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) booleanboolean is a type that is either true or false answersYes(String prompt) {open braces start code blocks and must be matched with a close brace
030 String userAnswer =this assignment operator makes the left side equal to the right side "";
031 // sentinel: userAnswer is a valid answer
032 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("y") &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 &&)
033 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("n") &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 &&)
034 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("yes") &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 &&)
035 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("no")) {open braces start code blocks and must be matched with a close brace
036 System.out.print(prompt);
037 System.out.print(" ");
038 userAnswer =this assignment operator makes the left side equal to the right side keyboard.nextLine();
039 }close braces end code blocks and must match an earlier open brace
040 // if we get here with "yes" or "y" we return true. Just check first
041 // character (ignore case)
042 returnreturn means to provide the result of the method and/or cease execution of the method immediately userAnswer.substring(0, 1).equalsIgnoreCase("y");
043 }close braces end code blocks and must match an earlier open brace
044
045 /**
046 * Print the prompt on standard output, read the next line from the
047 * standard input and returnreturn means to provide the result of the method and/or cease execution of the method immediately that value.
048 *
049 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt/question to show the user
050 *
051 * @returnnull the {open braces start code blocks and must be matched with a close brace@link String}close braces end code blocks and must match an earlier open brace typed in by the user; it could be empty
052 */
053 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) String getLine(String prompt) {open braces start code blocks and must be matched with a close brace
054 System.out.print(prompt);
055 System.out.print(" ");
056 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.nextLine();
057 }close braces end code blocks and must match an earlier open brace
058
059 /**
060 * The mainThe main method is the place where applications begin executing. program. At thisthis means the current object (the implicit parameter) level: make sure there is a file name on
061 * the command-line, 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 TwentyQuestions}close braces end code blocks and must match an earlier open brace object,
062 * load and play the game.
063 *
064 * @paramthis is the Javadoc tag for documenting the purpose of parameters args
065 */
066 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) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
067 String fname =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
068 ifif executes the next statement only if the condition in parenthesis evaluates to true (args.length ==this is the comparison operator which evaluates to true if both sides are the same 1) {open braces start code blocks and must be matched with a close brace
069 fname =this assignment operator makes the left side equal to the right side args[brackets are typically used to declare, initialize and index (indicate which element of) arrays0]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
070 }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
071 System.out.println("Usage: java TwentyQuestions <fname>");
072 System.out.println(" where <fname> is the data file name");
073 System.out.println();
074 System.out.println(
075 "Alternatively, what file would you like to load?");
076 Scanner scanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(System.in);
077 fname =this assignment operator makes the left side equal to the right side scanner.nextLine();
078 }close braces end code blocks and must match an earlier open brace
079
080 TwentyQuestions game =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor TwentyQuestions();
081 ifif executes the next statement only if the condition in parenthesis evaluates to true (game.load(fname)) {open braces start code blocks and must be matched with a close brace
082 game.play();
083 ifif executes the next statement only if the condition in parenthesis evaluates to true (TwentyQuestions.answersYes("Save guessing data?")) {open braces start code blocks and must be matched with a close brace
084 game.save(fname);
085 }close braces end code blocks and must match an earlier open brace
086 }close braces end code blocks and must match an earlier open brace
087 }close braces end code blocks and must match an earlier open brace
088
089 /**
090 * Load root (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) from the name file.
091 *
092 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the name of the file from which to load the data
093 *
094 * @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 load went well, falsefalse is a value for the boolean type and means not true otherwise
095 */
096 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false load(String fname) {open braces start code blocks and must be matched with a close brace
097 book =this assignment operator makes the left side equal to the right side AdventureBook.readBookFromFile(fname);
098 returnreturn means to provide the result of the method and/or cease execution of the method immediately (book !=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);
099 }close braces end code blocks and must match an earlier open brace
100
101 /**
102 * Play the game. Use a PlayProcessor to traverse the decision tree,
103 * playing the game.
104 */
105 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value play() {open braces start code blocks and must be matched with a close brace
106 booleanboolean is a type that is either true or false playing =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
107 whilewhile is a looping structure for executing code repeatedly (playing) {open braces start code blocks and must be matched with a close brace
108 book.play();
109 playing =this assignment operator makes the left side equal to the right side TwentyQuestions.answersYes(
110 "Would you like to play again?");
111 }close braces end code blocks and must match an earlier open brace
112 }close braces end code blocks and must match an earlier open brace
113
114 /**
115 * Save 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 stored in root is saved into the
116 * named file.
117 *
118 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the name of the file in which to save the data
119 */
120 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value save(String fname) {open braces start code blocks and must be matched with a close brace
121 AdventureBook.writeBookToFile(fname, book);
122 }close braces end code blocks and must match an earlier open brace
123 }close braces end code blocks and must match an earlier open brace
124
125 //Uploaded on Mon Mar 29 21:40:13 EDT 2010
|