|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.io;
002
003 importimport means to make the classes and/or packages available in this program java.util.Scanner;
004
005 /**
006 * A utility classclass is a group of fields and methods used for making objects wrapped around standard input. Provides all staticstatic means that an instance is not required for access (class level access)
007 * methods to prompt the user and read information from the user.
008 */
009 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 Keyboard {open braces start code blocks and must be matched with a close brace
010 /** scanner wrapped around standard input */
011 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);
012
013 /**
014 * Loop until user enters either 'y', 'n', 'yes', or 'no'.
015 *
016 * @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
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) booleanboolean is a type that is either true or false answeredYes() {open braces start code blocks and must be matched with a close brace
019 returnreturn means to provide the result of the method and/or cease execution of the method immediately answeredYesPrompt("");
020 }close braces end code blocks and must match an earlier open brace
021
022 /**
023 * Print the prompt on the standard output until the user answers
024 * either 'y', 'n', 'yes', or 'no'.
025 *
026 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt to show the user; nothing is printed ifif executes the next statement only if the condition in parenthesis evaluates to true
027 * the prompt is blank
028 *
029 * @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
030 */
031 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 answeredYesPrompt(String prompt) {open braces start code blocks and must be matched with a close brace
032 String userAnswer =this assignment operator makes the left side equal to the right side "";
033 // sentinel: userAnswer is a valid answer
034 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 &&)
035 !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 &&)
036 !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 &&)
037 !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
038 showPrompt(prompt);
039 userAnswer =this assignment operator makes the left side equal to the right side keyboard.nextLine();
040 }close braces end code blocks and must match an earlier open brace
041 // if we get here with "yes" or "y" we return true. Just check first
042 // character (ignore case)
043 returnreturn means to provide the result of the method and/or cease execution of the method immediately userAnswer.substring(0, 1).equalsIgnoreCase("y");
044 }close braces end code blocks and must match an earlier open brace
045
046 /**
047 * Get the next word from the keyboard. Will block, waiting forfor is a looping structure for repeatedly executing a block of code user,
048 * ifif executes the next statement only if the condition in parenthesis evaluates to true there is no input ready. DISPLAYS NO PROMPT!this is the not operator, which changes true to false and false to true
049 *
050 * @returnnull the next {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 entered by the user; contains no
051 * whitespace and is not 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 next() {open braces start code blocks and must be matched with a close brace
054 returnreturn means to provide the result of the method and/or cease execution of the method immediately nextPrompt("");
055 }close braces end code blocks and must match an earlier open brace
056
057 /**
058 * Get the next line form the keyboard without displaying a prompt.
059 * DISPLAYS NO PROMPT!this is the not operator, which changes true to false and false to true
060 *
061 * @returnnull the next line entered by user
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) String nextLine() {open braces start code blocks and must be matched with a close brace
064 returnreturn means to provide the result of the method and/or cease execution of the method immediately nextLinePrompt("");
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * Print the prompt on standard output, read the next line from the
069 * standard input and returnreturn means to provide the result of the method and/or cease execution of the method immediately that value.
070 *
071 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt/question to show the user
072 *
073 * @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
074 */
075 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 nextLinePrompt(String prompt) {open braces start code blocks and must be matched with a close brace
076 showPrompt(prompt);
077 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.nextLine();
078 }close braces end code blocks and must match an earlier open brace
079
080 /**
081 * Prompt user and read the next word from standard input, returning
082 * that word.
083 *
084 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt/question to show the user; nothing is
085 * printed ifif executes the next statement only if the condition in parenthesis evaluates to true the prompt is blank
086 *
087 * @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; contains no
088 * whitespace and is not empty
089 */
090 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 nextPrompt(String prompt) {open braces start code blocks and must be matched with a close brace
091 showPrompt(prompt);
092 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.next();
093 }close braces end code blocks and must match an earlier open brace
094
095 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value showPrompt(String prompt) {open braces start code blocks and must be matched with a close brace
096 ifif executes the next statement only if the condition in parenthesis evaluates to true (prompt.length() !=this is the not equals operator which evaluates to true if both sides are different 0) {open braces start code blocks and must be matched with a close brace
097 System.out.print(prompt +adds two numbers together or concatenates Strings together " ");
098 }close braces end code blocks and must match an earlier open brace
099 }close braces end code blocks and must match an earlier open brace
100 }close braces end code blocks and must match an earlier open brace
101
102 //Uploaded on Mon Mar 29 21:39:23 EDT 2010
|