Create a class called Puzzle that is used to manage a word puzzle like those fou
ID: 644538 • Letter: C
Question
Create a class called Puzzle that is used to manage a word puzzle like those found on the TV game show Wheel of Fortune. A Puzzle should have at least two instance variables described below:
Solution (type String): Represents the String that is the word / phrase associated with the puzzle.
Puzzle (type StringBuilder): Presents the current state of the puzzle. Letters that have not been guessed should be represented with a hyphen (). Letters that have been correctly guessed should be shownin the puzzle. Here is an example usage of the class:
Puzzle puzzle = new Puzzle("BIG JAVA");
//SHOULD PRINT OUT: --- ----
//THE LETTERS SHOULD BE HYPHENS SINCE NO LETTERS HAVE BEEN GUESSED
System.out.println(puzzle.getPuzzle());
//OCCURRENCES SHOULD BE 1 SINCE THERE IS ONE B IN THE SOLUTION
int occurrences = puzzle.guessLetter('b') ;
//SHOULD PRINT OUT: B-- ----
//SINCE THE B IS GUESSED
System.out.println(puzzle.getPuzzle());
//OCCURRENCES SHOULD BE 0 SINCE
//THE LETTER B
HAS ALREADY BEEN GUESSED
int occurrences = puzzle.guessLetter('b') ;
Explanation / Answer
/**java puzzle game */
//Puzzle.java
public class Puzzle
{
//instance variables
private String solution;
private StringBuilder puzzle;
//constructor that sets the string and
//set hyphern for string builder
Puzzle(String solution)
{
this.solution=solution;
puzzle=new StringBuilder(solution);
for (int index = 0; index < solution.length(); index++)
{
puzzle.setCharAt(index, '-');
}
}
//The method guessLetter takes a character that retunrs the number
//of occurrence of the letter guess . If the character is already guessed
//then returns zero.
public int guessLetter(char guess)
{
int occurencess=0;
int index=0;
char toUpper = 0;
boolean found=false;
//check if the character is already guessed
for (int i = 0; i < solution.length()&& !found; i++)
{
if(Character.toUpperCase(guess)==puzzle.charAt(i))
found=true;
else
found=false;
}
//Continue if the letter is not guessed already
if(!found)
{
//find the number of occurrence of the letter
for (; index < solution.length(); index++)
{
toUpper=Character.toUpperCase(guess);
if(toUpper==solution.charAt(index))
{
puzzle.setCharAt(index,toUpper);
occurencess++;
}
}
}
//if letter is already guess return 0
//otherwise returns number of occurence
if(found)
return 0;
else
return occurencess;
}
//Returns the string builder
public StringBuilder getPuzzle()
{
return puzzle;
}
}
------------------------------------------------------------------------------------------------------------
//TestPuzzle.java
//Test java program that tests the Puzzle game
// Create an instance of Puzzle and set a word
//and calls getPuzzle to display the string wit
//hyphens and call guessLetter with a character
//that returns number of occurennces
//If the character is already guessed then
//then method returns the 0
public class TestPuzzle
{
public static void main(String[] args)
{
//create an instanc eof Puzzle class and set a word
Puzzle puzzle=new Puzzle("BIG JAVA");
//print a guessed word
System.out.println(puzzle.getPuzzle());
//call guessLetter with character b
int occurences=puzzle.guessLetter('b');
System.out.println("Occurences : "+occurences);
System.out.println(puzzle.getPuzzle());
occurences=puzzle.guessLetter('b');
System.out.println("Occurences : "+occurences);
occurences=puzzle.guessLetter('a');
System.out.println(puzzle.getPuzzle());
System.out.println("Occurences : "+occurences);
occurences=puzzle.guessLetter('a');
System.out.println(puzzle.getPuzzle());
System.out.println("Occurences : "+occurences);
}
}
------------------------------------------------------------------------------------------------
Sample output:
--------
Occurences : 1
B-------
Occurences : 0
B----A-A
Occurences : 2
B----A-A
Occurences : 0
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.