|
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.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 /**
019 * Print the prompt on the standard output until the user answers
020 * either 'y', 'n', 'yes', or 'no'.
021 *
022 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt to show the user
023 *
024 * @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
025 */
026 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
027 String userAnswer =this assignment operator makes the left side equal to the right side "";
028 // sentinel: userAnswer is a valid answer
029 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 &&)
030 !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 &&)
031 !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 &&)
032 !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
033 System.out.print(prompt);
034 System.out.print(" ");
035 userAnswer =this assignment operator makes the left side equal to the right side keyboard.nextLine();
036 }close braces end code blocks and must match an earlier open brace
037 // if we get here with "yes" or "y" we return true. Just check first
038 // character (ignore case)
039 returnreturn means to provide the result of the method and/or cease execution of the method immediately userAnswer.substring(0, 1).equalsIgnoreCase("y");
040 }close braces end code blocks and must match an earlier open brace
041
042 /**
043 * Print the prompt on standard output, read the next line from the
044 * standard input and returnreturn means to provide the result of the method and/or cease execution of the method immediately that value.
045 *
046 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt/question to show the user
047 *
048 * @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
049 */
050 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
051 System.out.print(prompt);
052 System.out.print(" ");
053 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.nextLine();
054 }close braces end code blocks and must match an earlier open brace
055
056 /**
057 * 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
058 * 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,
059 * load and play the game.
060 *
061 * @paramthis is the Javadoc tag for documenting the purpose of parameters args
062 */
063 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
064 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
065 String 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;
066 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();
067 game.load(fname);
068 game.play();
069 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
070 game.save(fname);
071 }close braces end code blocks and must match an earlier open brace
072 }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
073 System.out.println("Usage: java TwentyQuestions <fname>");
074 System.out.println(" where <fname> is the data file name");
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 /** root of the DecisionTree representing the smarts */
079 privateprivate is used to restrict access to the current class only AdventureBook book;
080
081 /**
082 * 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.
083 *
084 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the name of the file from which to load the data
085 */
086 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value load(String fname) {open braces start code blocks and must be matched with a close brace
087 book =this assignment operator makes the left side equal to the right side AdventureBook.readBookFromFile(fname);
088 }close braces end code blocks and must match an earlier open brace
089
090 /**
091 * Play the game. Use a PlayProcessor to traverse the decision tree,
092 * playing the game.
093 */
094 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
095 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;
096 whilewhile is a looping structure for executing code repeatedly (playing) {open braces start code blocks and must be matched with a close brace
097 book.play();
098 playing =this assignment operator makes the left side equal to the right side TwentyQuestions.answersYes(
099 "Would you like to play again?");
100 }close braces end code blocks and must match an earlier open brace
101 }close braces end code blocks and must match an earlier open brace
102
103 /**
104 * 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
105 * named file.
106 *
107 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the name of the file in which to save the data
108 */
109 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
110 AdventureBook.writeBookToFile(fname, book);
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 //Uploaded on Mon Mar 29 21:38:55 EDT 2010
|