From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch10;
002
003 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
004
005 /**
006 * A word to be guessed. Displays the number of blanks in the word. The
007 * {open braces start code blocks and must be matched with a close brace@code #guess(charchar is the type for a single letter or symbol and it is short for character)}close braces end code blocks and must match an earlier open brace method replaces blanks with the guessed letter
008 * (where it is correct). {open braces start code blocks and must be matched with a close brace@code #unguessedLetters()}close braces end code blocks and must match an earlier open brace counts the number
009 * of underscores remaining in the word, returning the number of letters
010 * as yet unknown. Note: Guessing the same letter a second time will
011 * 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 from guess (all checking is done against word); it is
012 * left as an exercise forfor is a looping structure for repeatedly executing a block of code the reader to modify the guess method so it
013 * returns falsefalse is a value for the boolean type and means not true ifif executes the next statement only if the condition in parenthesis evaluates to true a letter has been previously guessed and is found in
014 * word (hint: indexOf on showWord).
015 */
016 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 GuessableWord
017 extendsextends means to customize or extend the functionality of a class StringSprite {open braces start code blocks and must be matched with a close brace
018 /** guess in progress: _ or letter, doubledouble is the type for numbers that can contain decimal fractions-spaced */
019 privateprivate is used to restrict access to the current class only String showWord;
020
021 /** the word being guessed */
022 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) String word;
023
024 /**
025 * Create a newnew is used to create objects by calling the constructor guessable word. Display is a doubledouble is the type for numbers that can contain decimal fractions-spaced (space
026 * between each pair of letters) rendition of the word. Initially all
027 * positions are '_' characters; as guesses are made, correct letters
028 * replace the underscores.
029 *
030 * @paramthis is the Javadoc tag for documenting the purpose of parameters word the word stored in thisthis means the current object (the implicit parameter) guessable word
031 */
032 publicpublic is used to indicate unrestricted access (any other class can have access) GuessableWord(String word) {open braces start code blocks and must be matched with a close brace
033 thisthis means the current object (the implicit parameter).word =this assignment operator makes the left side equal to the right side word;
034 setMonospaced(truetrue is the boolean value that is the opposite of false);
035 initializeShowWord();
036 }close braces end code blocks and must match an earlier open brace
037
038 /**
039 * Expose the word. Puts the letters into all of the locations of the
040 * word.
041 */
042 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value expose() {open braces start code blocks and must be matched with a close brace
043 showWord =this assignment operator makes the left side equal to the right side "";
044 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different word.length(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
045 showWord =this assignment operator makes the left side equal to the right side showWord +adds two numbers together or concatenates Strings together " " +adds two numbers together or concatenates Strings together word.charAt(i);
046 }close braces end code blocks and must match an earlier open brace
047 setText(showWord);
048 }close braces end code blocks and must match an earlier open brace
049
050 /**
051 * Guess the given letter is in the word. Updates the display to
052 * expose any matching positions and 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 ifif executes the next statement only if the condition in parenthesis evaluates to true the letter is in
053 * the word to be guessed. Leave the display unchanged and returnreturn means to provide the result of the method and/or cease execution of the method immediately
054 * falsefalse is a value for the boolean type and means not true otherwise.
055 *
056 * @paramthis is the Javadoc tag for documenting the purpose of parameters ch the letter to guess is in the word
057 *
058 * @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 ch appears in the word; falsefalse is a value for the boolean type and means not true otherwise
059 */
060 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false guess(charchar is the type for a single letter or symbol and it is short for character ch) {open braces start code blocks and must be matched with a close brace
061 booleanboolean is a type that is either true or false letterFound =this assignment operator makes the left side equal to the right side contains(ch);
062 ifif executes the next statement only if the condition in parenthesis evaluates to true (letterFound) {open braces start code blocks and must be matched with a close brace
063 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer wordNdx =this assignment operator makes the left side equal to the right side 0, showWordNdx =this assignment operator makes the left side equal to the right side 1; wordNdx !=this is the not equals operator which evaluates to true if both sides are different word.length();
064 ++this is the increment operator, which increases the variable by 1wordNdx, showWordNdx +=this increases the variable on the left by the value on the right 2) {open braces start code blocks and must be matched with a close brace
065 ifif executes the next statement only if the condition in parenthesis evaluates to true (word.charAt(wordNdx) ==this is the comparison operator which evaluates to true if both sides are the same ch) {open braces start code blocks and must be matched with a close brace
066 showWord =this assignment operator makes the left side equal to the right side showWord.substring(0, showWordNdx) +adds two numbers together or concatenates Strings together ch +adds two numbers together or concatenates Strings together
067 showWord.substring(showWordNdx +adds two numbers together or concatenates Strings together 1);
068 }close braces end code blocks and must match an earlier open brace
069 }close braces end code blocks and must match an earlier open brace
070 setText(showWord);
071 }close braces end code blocks and must match an earlier open brace
072 returnreturn means to provide the result of the method and/or cease execution of the method immediately letterFound;
073 }close braces end code blocks and must match an earlier open brace
074
075 /**
076 * Has the word been guessed? Are there no unguessed letters?
077 *
078 * @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 there are no unguessed letters; falsefalse is a value for the boolean type and means not true otherwise
079 */
080 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isGuessed() {open braces start code blocks and must be matched with a close brace
081 returnreturn means to provide the result of the method and/or cease execution of the method immediately (unguessedLetterCount() ==this is the comparison operator which evaluates to true if both sides are the same 0);
082 }close braces end code blocks and must match an earlier open brace
083
084 /**
085 * Count the number of underscores ('_') in showWord. This is the
086 * number of letters still to be guessed. The word is guessed when the
087 * number of unguessed letters is 0.
088 *
089 * @returnnull the number of '_' in the shown word; the number of letters
090 * still to be guessed
091 */
092 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer unguessedLetterCount() {open braces start code blocks and must be matched with a close brace
093 /** how many matches have been found */
094 intint is the type for whole numbers and it is short for integer underscoreCount =this assignment operator makes the left side equal to the right side 0;
095
096 /** where is the most recent underscore found */
097 intint is the type for whole numbers and it is short for integer justMatchedIndex =this assignment operator makes the left side equal to the right side showWord.indexOf('_', 0);
098 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to true(justMatchedIndex ==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 // negative logic is bad!
099 ++this is the increment operator, which increases the variable by 1underscoreCount;
100 justMatchedIndex =this assignment operator makes the left side equal to the right side showWord.indexOf('_', justMatchedIndex +adds two numbers together or concatenates Strings together 1);
101 }close braces end code blocks and must match an earlier open brace
102 returnreturn means to provide the result of the method and/or cease execution of the method immediately underscoreCount;
103 }close braces end code blocks and must match an earlier open brace
104
105 /**
106 * Does the word contain the given letter?
107 *
108 * @paramthis is the Javadoc tag for documenting the purpose of parameters letter the letter to check forfor is a looping structure for repeatedly executing a block of code
109 *
110 * @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 there is one or more instances of letter in word;
111 * falsefalse is a value for the boolean type and means not true otherwise
112 */
113 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false contains(charchar is the type for a single letter or symbol and it is short for character letter) {open braces start code blocks and must be matched with a close brace
114 returnreturn means to provide the result of the method and/or cease execution of the method immediately (word.indexOf(letter) !=this is the not equals operator which evaluates to true if both sides are different -1);
115 }close braces end code blocks and must match an earlier open brace
116
117 /**
118 * Initialize the guess to have no correct letters yet (and doubledouble is the type for numbers that can contain decimal fractions
119 * space it). Note that showWord is always twice as longlong is the type for whole numbers (they have a larger range than int) as word and
120 * always begins with a blank space (thisthis means the current object (the implicit parameter) is important in the way
121 * guess works). Note that internal spaces are shown as spaces.
122 */
123 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value initializeShowWord() {open braces start code blocks and must be matched with a close brace
124 showWord =this assignment operator makes the left side equal to the right side "";
125 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different word.length(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
126 ifif executes the next statement only if the condition in parenthesis evaluates to true (word.charAt(i) !=this is the not equals operator which evaluates to true if both sides are different ' ') {open braces start code blocks and must be matched with a close brace
127 showWord +=this increases the variable on the left by the value on the right " _";
128 }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
129 showWord +=this increases the variable on the left by the value on the right " ";
130 }close braces end code blocks and must match an earlier open brace
131 }close braces end code blocks and must match an earlier open brace
132 setText(showWord);
133 }close braces end code blocks and must match an earlier open brace
134 }close braces end code blocks and must match an earlier open brace
135
136 //Uploaded on Mon Mar 29 21:39:30 EDT 2010
|
Download/View scg/ch10/GuessableWord.java