Question: The four methods you will write are: createKey, createPuzzle, isSolved, and checkGuess createKey public String createKey(String phrase) createKey takes a String as a parameter and

The four methods you will write are: createKey, createPuzzle, isSolved, and checkGuess

createKey

public String createKey(String phrase)

createKey takes a String as a parameter and returns a String based on these rules:

the given (that means the parameter) String is changed to all uppercase

the returned String is twice as long as the given String

the returned String is comprised of the first char of the given String, followed by a blank char ' ', the second char of the given String, followed by a blank char ' ', and so on.

the returned String ends with a blank char ' '.

For example:

createKey("Hi Ho!") --> "H I H O ! "

createPuzzle

public char[] createPuzzle(String phrase)

createPuzzle takes a String as a parameter and returns an array of char based on these rules:

the given (that means the parameter) String is changed to all uppercase

the returned array is twice as long as the given String

the returned array is comprised of an underscore '_' wherever there is a letter in the phrase, followed by a blank char ' ', Any character in the phrase that is not a letter is kept, as is, in the puzzle, followed by a blank char ' '.

the returned array has a blank char ' ' in the last position.

For example:

createPuzzle("Ho!") --> ['_',' ','_',' ','!',' ']

isSolved

public boolean isSolved(String key, char[] puzzle)

isSolved answers if the puzzle is solved. It takes two parameters, the key (a String) and the puzzle (an array of char). If all the characters in the array are the same as the characters in the String, in the same order, then isSolved returns true. if the length of the given key is not the same as the length of the puzzle, isSolved returns false. if the lengths are the same, and any character is different, isSolved returns false.

checkGuess

public void checkGuess(char guess, String key, char[] puzzle)

checkGuess takes a char, the guessed letter, and if that char matches any character in the key, it puts that char into the puzzle array at the same position as the matched char in the key. checkGuess is void, but, the puzzle array may get changed.

For example, if:

puzzle = ['_',' ','_',' ','!',' ']

and the checkGuess method was called:

checkGuess('h',"Ho!",puzzle)

then puzzle would contain:

['H',' ','_',' ','!',' ']

a sample run of the game follows:

Enter a phrase: Westward, Ho! _ _ _ _ _ _ _ _ , _ _ ! Guess a letter: E _ E _ _ _ _ _ _ , _ _ ! Guess a letter: s _ E S _ _ _ _ _ , _ _ ! Guess a letter: t _ E S T _ _ _ _ , _ _ ! Guess a letter: w W E S T W _ _ _ , _ _ ! Guess a letter: a W E S T W A _ _ , _ _ ! Guess a letter: r W E S T W A R _ , _ _ ! Guess a letter: n W E S T W A R _ , _ _ ! Guess a letter: o W E S T W A R _ , _ O ! Guess a letter: h W E S T W A R _ , H O ! Guess a letter: d W E S T W A R D , H O ! Hooray! It took you 10 guesses

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!