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

1. Can someone explain this java program to me in detail step-by-step. I want a

ID: 3555795 • Letter: 1

Question

1. Can someone explain this java program to me in detail step-by-step. I want a detailed explanation step by step

public class Hangman {

private String secretWord;
private String disguisedWord ="";
private String guessedLetters = "";
private int numberOfGuesses = 0;
private int numberOfIncorrectGuesses = 0;

Hangman(String secretWord){
this.secretWord = secretWord;
for (int i = 0; i < secretWord.length(); i++){
this.disguisedWord += "?";
}
}

public String getSecretWord(){
return secretWord;
}

public String getGuesses(){
return guessedLetters;
}

public String getDisguisedWord(){
return disguisedWord;
}

public int getGuessCount(){
return numberOfGuesses;
}

public int getIncorrectGuessCount(){
return numberOfIncorrectGuesses;
}

public boolean isFound(){
if (secretWord.equalsIgnoreCase(disguisedWord)){
return true;
}
else
return false;
}

public boolean makeGuess(char guess){

for (int i = 0; i < guessedLetters.length(); i++){
if (guess == guessedLetters.charAt(i)){
System.out.println("You have already guessed that letter.");
return false;
}
}

guessedLetters+= guess;
String tempString = "";
numberOfGuesses++;
for (int i = 0; i < secretWord.length(); i++){
if (guess == secretWord.charAt(i)){
tempString += guess;
}
else{
tempString += disguisedWord.charAt(i);
}
}
if (!tempString.equalsIgnoreCase(disguisedWord)){
disguisedWord = tempString;
return true;
}
else{
System.out.println("Incorrect Guess!");
numberOfIncorrectGuesses++;
return false;
}
}

public static void main(String[] args){
java.util.Scanner keyboard = new java.util.Scanner(System.in);
Hangman hangman = new Hangman("pirate"); // enter secret word as a string

while (hangman.isFound()== false){
System.out.println("The word is: " + hangman.getDisguisedWord());
System.out.println("Guess a letter or type 'guesses' to see your guesses.");
String input = keyboard.nextLine();
if (input.equalsIgnoreCase("guesses")){
System.out.println(hangman.getGuesses());
}
else{
hangman.makeGuess(input.charAt(0));
}
}

System.out.println("The secret word was: " + hangman.getSecretWord());
System.out.println("It took you " + hangman.getGuessCount() + " guesses to get the correct answer.");
System.out.println("You had " + hangman.getIncorrectGuessCount()+ " incorrect guesses.");
}
}

Explanation / Answer

here program asks user to make a guess of characters of a hidden word if user makes a correct guess of character then it displays that character with the position
here hidden word is pirate so the program asks to make a guess of character in word ??????
suppose you made a guess p then it will display p?????
again if you made a guess r then it will dispaly p?r???

program first declares hangman class

public class Hangman

then it declares

private String secretWord;
private String disguisedWord ="";
private String guessedLetters = "";
private int numberOfGuesses = 0;   
private int numberOfIncorrectGuesses = 0;

here secretWord is the hidden word disguised word is used to display like ?????? or p?????

which is initially empty ""

guessedLetters is used to display total guessed characters like if you guessed characters v,n,c,d,s

then it will dispaly vncds

numberOfGuesses is used to count total number of guesses made and

numberOfIncorrectGuesses is used to count total number of incorrect guesses

rest i have commented the program if still you dont understand then just comment

public class Hangman {

private String secretWord;
private String disguisedWord =""; //used to form word ??????
private String guessedLetters = "";
private int numberOfGuesses = 0;   
private int numberOfIncorrectGuesses = 0;
/*
* a constructor is a method that is called during creation of a object like ->
* Hangman hangman = new Hangman("pirate");
* here Hangman("pirate") calls the method below
* this is Hangman constructor it takes input string and forms a new string disguisedWord using the length of given string
* here secretWord is pirate length is 6 so Hangman constructor creates disguisedWord as ??????
*/

Hangman(String secretWord){
this.secretWord = secretWord;
for (int i = 0; i < secretWord.length(); i++){
this.disguisedWord += "?";
}
}

public String getSecretWord(){
return secretWord;
}

public String getGuesses(){
return guessedLetters;
}

public String getDisguisedWord(){
return disguisedWord;
}

public int getGuessCount(){
return numberOfGuesses;
}

public int getIncorrectGuessCount(){
return numberOfIncorrectGuesses;// returns total number of incorrect guesses
}

public boolean isFound(){
   /*
   * returns true if secretWord is equal to disguisedWord else returns false
   */

if (secretWord.equalsIgnoreCase(disguisedWord)){
return true;
}
else
return false;
}

public boolean makeGuess(char guess){
/*
* in this function a user enters a guess character ,that guess character is compared with all characters of
* guessed letters and if character matches then it prints "You have already guessed that letter"
* here matching of guess character is done at guess == guessedLetters.charAt(i)
*/

for (int i = 0; i < guessedLetters.length(); i++){
if (guess == guessedLetters.charAt(i)){
System.out.println("You have already guessed that letter.");
return false;
}
}

guessedLetters+= guess;
/*
* if user have not already guessed the letter then that guess is appended to string gussedLetters
*/

String tempString = "";
numberOfGuesses++; //increases the count of numberOfGuesses
/*
* in the for loop below if guess character equals any character of seceretWord then that word is shown
* here first tempString is "",suppose disguisedWord is ??????
* if character is p then statement is true here guess == secretWord.charAt(i)i=0
* because secretWord is pirate so it will make the disguisedWord as p????? by code
* if (guess == secretWord.charAt(i)){
   tempString += guess;//if equal then add that char in tempString
       }
       else{
       tempString += disguisedWord.charAt(i);//use characters of disguisedWord eg??
}
*/

for (int i = 0; i < secretWord.length(); i++){
if (guess == secretWord.charAt(i)){
tempString += guess;
}
else{
tempString += disguisedWord.charAt(i);
}
}
/*
* in the code below it checks if tempString is equal to disguisedWord then it prints incorrect guess
* otherwise user has made a correct guess so it updates value of disguisedWord to tempString
*/

if (!tempString.equalsIgnoreCase(disguisedWord)){
disguisedWord = tempString;
return true;
}
else{
System.out.println("Incorrect Guess!");
numberOfIncorrectGuesses++;
return false;
}
}

public static void main(String[] args){
java.util.Scanner keyboard = new java.util.Scanner(System.in);//takes input from the keyboard
Hangman hangman = new Hangman("pirate"); // enter secret word as a string and Hangman() //constructor is called

while (hangman.isFound()== false){
System.out.println("The word is: " + hangman.getDisguisedWord());
System.out.println("Guess a letter or type 'guesses' to see your guesses.");
String input = keyboard.nextLine();
if (input.equalsIgnoreCase("guesses")){
System.out.println(hangman.getGuesses());
}
else{
hangman.makeGuess(input.charAt(0));
}
}

System.out.println("The secret word was: " + hangman.getSecretWord());
System.out.println("It took you " + hangman.getGuessCount() + " guesses to get the correct answer.");
System.out.println("You had " + hangman.getIncorrectGuessCount()+ " incorrect guesses.");
}
}