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

/** * The Hangman object is used to keep track of the number of incorrect guesse

ID: 3561429 • Letter: #

Question

/** * The Hangman object is used to keep track of the number of incorrect guesses * the player has made (which is used to draw the hangman) and the Hangman * object is used to track the correct letters the player has guessed. * * This class Must have the following: PROPERTIES 1. int wrongGuessCount 2. String wordToGuess 3. String[] lettersGuessed METHODS 1. isWinner() 2. isLoser() 3. guessLetter() 4. print() */ public class Hangman { } /** * Class constructor. This constructor must do the following: * * 1. Initialize the int wrongGuessCount property to 0 * 2. Initialize the String wordToGuess property with the value passed * to this constructor's parameter * 3. Initialize the String[] lettersGuessed parameter to be an String * array with the same length as the wordToGuess property */ public Hangman(String wordToGuess) /** * This method determines if the player has won the game. The player * wins the game if they have successfully guessed all the letters of the * word and the lettersGuessed [] property has no null values. * * It must do the following: * * 1. Loop over all the Strings in the lettersGuessed [] property * 2. If any string in the lettersGuessed [] property is null, return false * 3. Otherwise return true. */ public boolean isWinner() /** * This method determines if the player has lost the game. The player * loses the game if hangman is drawn. It takes 6 failed tries to * draw the hangman. * * It must do the following: * * 1. Return true if the wrongGuessCount property is >= 6 * 2. Otherwise return false */ public boolean isLoser() /** * This method guesses a letter. A letter is either part of the word * or it is not. If it is not, the player guessed incorrectly and the * next body part of the hangman should be drawn. If it is, then the * player guessed correctly and the letter should be shown in the spot * it appears in the word. * * It must do the following: * * 1. Loop over all the letters in the wordToGuess property. * - If the user's guess matches a letter in the wordToGuess property, * add that letter to the lettersGuessed [] property * - Match letters case-insensitively. * 2. After looping over all the ketters, if a match was not found, * increment the wrongGuessCount property */ public void guessLetter(String userGuess) /** * This method is responsible for printing both the hangman and the * letters the user has guessed. * * An empty hangman looks like this: * * ----------- * | | * | * | * | * | * | * ---- * * _ _ _ _ _ _ _ _ * * A full hangman looks like this: * * ----------- * | | * | O * | /| * | / * | * | * ---- * * _ _ _ _ _ _ _ _ * * It must do the following: * * 1. Print the hangman. Use the wrongGuessCount property to determine * which body part to draw. The body must be drawn in this order * HEAD * TORSO * LEFT ARM * RIGHT ARM * LEFT LEG * RIGHT LEG * 2. Print the letters the player has guessed. Letters always printed * in UPPERCASE. Use the "_" character to represent a letter the user * has not guessed. Print an empty space " " between each character */ public void print() Guess

Explanation / Answer

Hangman.java

public class Hangman {
   int wrongGuessCount;
   String wordToGuess;
   String[] lettersGuessed;
  
   public Hangman(String wordToGuess){
       this.wrongGuessCount = 0;
       this.wordToGuess = wordToGuess;
       this.lettersGuessed = new String[this.wordToGuess.length()];
   }
  
   public boolean isWinner(){
       for(String letterGussed : this.lettersGuessed){
           if(letterGussed == null)
               return false;
       }
       return true;
   }
  
   public boolean isLoser(){
       return this.wrongGuessCount>=6?true:false;
   }
  
   public void guessLetter(String userGuess){
       boolean isPresent = false;
       for(int i = 0; i < this.wordToGuess.length();i++){
           if(userGuess.equalsIgnoreCase(this.wordToGuess.charAt(i)+"")){
               this.lettersGuessed[i] = userGuess;
               isPresent = true;
           }
       }
       if(!isPresent)
           this.wrongGuessCount++;
   }
  
   void print(){
       System.out.println("----------");
       switch(this.wrongGuessCount){
       case 0:
           System.out.println(" | | | | | | | ");break;
       case 1:
           System.out.println(" | | | O | | | | ");break;
       case 2:
           System.out.println(" | | | O | | | | | ");break;
       case 3:
           System.out.println(" | | | O | /| | | | ");break;
       case 4:
           System.out.println(" | | | O | /|\ | | | ");break;
       case 5:
           System.out.println(" | | | O | /|\ | / | | ");break;
       case 6:
           System.out.println(" | | | O | /|\ | /\ | | ");break;
       }
       System.out.println("----");
  
       for(String letterGussed : this.lettersGuessed){
           if(letterGussed == null)
               System.out.print("_ ");
           else
               System.out.print(letterGussed.toUpperCase()+" ");
       }
       System.out.println();
   }
   /*
   * Just to test the Hangman implemented
   */
   public static void main(String[] args) {
       Hangman h = new Hangman("meco");
       h.guessLetter("a");
       h.print();
       h.guessLetter("e");
       h.print();
       h.guessLetter("i");
       h.print();
       h.guessLetter("o");
       h.print();
       h.guessLetter("u");
       h.print();
       h.guessLetter("m");
       h.print();
       h.guessLetter("c");
       h.print();
   }
}

Output:

----------
| |
| O
|
|
|
|

----
_ _ _ _
----------
| |
| O
|
|
|
|

----
_ E _ _
----------
| |
| O
| |
|
|
|

----
_ E _ _
----------
| |
| O
| |
|
|
|

----
_ E _ O
----------
| |
| O
| /|
|
|
|

----
_ E _ O
----------
| |
| O
| /|
|
|
|

----
M E _ O
----------
| |
| O
| /|
|
|
|

----
M E C O