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

Consider a class that could be used to play a game of hangman. The class has the

ID: 3797586 • Letter: C

Question

Consider a class that could be used to play a game of hangman. The class has the following attributes:

• The secret word • The disguised word, in which each unknown letter in the secret word has been replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b and e have been guessed, the disguised word would be ab?a?a?a?ab?a.

• The number of guesses made.

• The number of incorrect guesses.

The class would have the following methods:

• makeGuess(char c) guesses that character c is in the word.

• getDisguisedWord() returns a String containing correctly guessed letters in the correct positions and unknown letters replaced with ?.

• getSecretWord() returns the secret word.

• getGuessCount() returns the number of guesses made.

• isFound() returns true if the hidden word has been discovered.

Your task is to implement the class with the above variables and methods, then implement a “game” of hangman using this class. Your program doens’t need to draw anything. please use JAVA to help me solve this question with the code. Please also make sure the code working and witht the comments show how to get the final result.

Explanation / Answer

Class Implementation of Hangman

public class Hangman
{
private String secretWord;
private String disguisedWord;
private int guessesMade;
private int incorrectGuesses;
private boolean isFound;
  
public String makeGuess(char c) {
guessesMade + = 1;
int index = secretWord.indexOf(c);
String sDisguised = disguisedWord;
if(index < 0) {
incorrectGuesses + = 1;
return 'Sorry, you made an incorrect guess!';
} else {
while(index >= 0) {
sDisguised = sDisguised.substring(0, index) + c +
sDisguised.substring(index + 1);
index = secretWord.indexOf(c, index + 1);
}
}
disguisedWord = sDisguised;
if(disguisedWord.indexOf('?') < 0) {
isFound = true;
return 'Congratulations! You won!';
}
return 'You guessed one correct Character!';
}
public String getDisguisedWord() {
return disguisedWord;
}
public String getSecretWord() {
return secretWord;
}
public int getGuessCount() {
return guessesMade;
}
public boolean isFound() {
return isFound;
}
  
public Hangman(String secret) {
secretWord = secret;
int length = secret.length();
disguisedWord = StringUtils.repeat('?', length);
guessesMade = 0;
incorrectGuesses = 0;
isFound = false;
}
  
}

To play the game of Hangman

Hangman game = new Hangman('google');
String sDisguised = game.getDisguisedWord();
System.out.println('Guess the following word - ' + sDisguised);
char sGuessedChar = 'c';
System.out.println('You have guessed the character - ' + sGuessedChar);
String sMessage = game.makeGuess(sGuessedChar);
sDisguised = game.getDisguisedWord();
System.out.println(sMessage);
System.out.println('Now the word looks like this - ' + sDisguised);
sGuessedChar = 'o';
System.out.println('You have guessed the character - ' + sGuessedChar);
sMessage = game.makeGuess(sGuessedChar);
sDisguised = game.getDisguisedWord();
System.out.println(sMessage);
System.out.println('Now the word looks like this - ' + sDisguised);
sGuessedChar = 'g';
System.out.println('You have guessed the character - ' + sGuessedChar);
sMessage = game.makeGuess(sGuessedChar);
sDisguised = game.getDisguisedWord();
System.out.println(sMessage);
System.out.println('Now the word looks like this - ' + sDisguised);
sGuessedChar = 'l';
System.out.println('You have guessed the character - ' + sGuessedChar);
sMessage = game.makeGuess(sGuessedChar);
sDisguised = game.getDisguisedWord();
System.out.println(sMessage);
System.out.println('Now the word looks like this - ' + sDisguised);
sGuessedChar = 'e';
System.out.println('You have guessed the character - ' + sGuessedChar);
sMessage = game.makeGuess(sGuessedChar);
sDisguised = game.getDisguisedWord();
System.out.println(sMessage);
System.out.println('Now the word looks like this - ' + sDisguised);

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