Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Puzzle that is used to manage a word puzzle like those fou

ID: 644539 • 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

public class Puzzle {
   private String word;
   private String currentState;

   public Puzzle(String word) {
       this.word = word;
       currentState = "";
       for (int i = 0; i < word.length(); ++i) {
           char ch = word.charAt(i);
           if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
               currentState = currentState.concat(String.format("%c", '_'));
           } else {
               currentState = currentState.concat(String.format("%c", ch));
           }
       }
   }

   public String getPuzzle() {
       return currentState;
   }

   public int guessLetter(char ch) {
       int count = 0;
       for (int i = 0; i < word.length(); ++i) {
           if (Character.toLowerCase(word.charAt(i)) == Character
                   .toLowerCase(ch)) {
               if (Character.toLowerCase(currentState.charAt(i)) == Character
                       .toLowerCase(ch)) {
                   return 0;
               } else {
                   count++;
                   if (i != 0)
                       currentState = currentState.substring(0, i)
                               .concat(String.format("%c", word.charAt(i)))
                               .concat(currentState.substring(i + 1));
                   else
                       currentState = String.format("%c", word.charAt(i))
                               .concat(currentState.substring(i + 1));
               }
           }
       }
       return count;
   }

   public static void main(String args[]) {
       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');
       System.out.println(occurrences);

       // 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
       occurrences = puzzle.guessLetter('b');
       System.out.println(occurrences);
       System.out.println(puzzle.getPuzzle());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote