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

Can anybody help with this? I am going bizerk! This is my fourth attempt of tryi

ID: 3643649 • Letter: C

Question

Can anybody help with this? I am going bizerk! This is my fourth attempt of trying to solve this java program from "Introduction to java programming" by D. Liang. This is a programming exercise from chapter 9, question 9.31. Here goes: (Game: hangman) Write a hangman game that randomly generates a word andprompts the user to guess one letter at a time. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue for another word. Allow the user to enter ? to show the of incorrect guesses and the number of allowed incorrect guesses remaining. Declare an array to store words, as follows: // Use any words you like. String[] words = {"array", "write","method", ...}

Here is a sample output:

(Guess) Enter a letter in word ***** > a

(Guess) Enter a letter in word a**a* > r

(Guess) Enter a letter in word arra* > y...


Thank so very much:)

Explanation / Answer

please rate


import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

/**
* The Class Hangman.
*/
public class Hangman {
   
    /** The word list. */
    String[] wordList = {"world","hello","program","hangman","game","random"};
   
    /** The guess word. */
    String guessWord;
   
    /** The no of misses. */
    int noOfMisses = 0;
   
    /** The allowed misses. */
    static final int allowedMisses = 5;
   
    /** The found char to mark characters found in the guessed word. */
    boolean[] foundChar;
   
    /** The radom value that will be used to get a word from list. */
    static Random r = new Random();
   
    /**
    * Check if game is completed.
    *
    * @return true, if successful
    */
    public boolean checkCompleted(){
        for(int i=0;i<foundChar.length;i++)
            if(!foundChar[i])
                return false;
        return true;
    }
   
    /**
    * Prints the completed chars.
    */
    public void printCompletedChars(){
        for(int i=0;i<guessWord.length();i++){
            if(foundChar[i])
                System.out.print(guessWord.charAt(i));
            else
                System.out.print("*");
        }
        System.out.println();
    }
   
    /**
    * Try to guess each character in the game.
    */
    public void tryToGuess(){
        Scanner input = new Scanner(System.in);
        foundChar = new boolean[guessWord.length()];
        boolean completed = false;
        while( (noOfMisses < allowedMisses) && !completed){
            System.out.print("(Guess) Enter a letter in word ");
            printCompletedChars();
            System.out.println("> ");
            char guess = input.next().charAt(0);
            if(guess=='?'){
                System.out.println("Number of incorrect guesses till now: "+noOfMisses);
                System.out.println("Total allowed "+Hangman.allowedMisses);
                continue;
            }
            if(guessWord.contains(guess+"")){
                for(int i=0;i<guessWord.length();i++){
                    if(guessWord.charAt(i)==guess)
                        foundChar[i] = true;
                }               
            }else{
                noOfMisses++;
            }
            completed = checkCompleted();
        }
        if(completed){
            System.out.println("You guessed correctly. ");
        }
        System.out.println("Correct answer is "+guessWord);       
        System.out.println("Total number of incorrect guesses "+noOfMisses);
    }
   
    /**
    * Instantiates a new hangman game.
    */
    public Hangman(){
       
        System.out.println("==================");
        System.out.println("Welcome to HANGMAN");
        System.out.println("==================");
        System.out.println("Incorrect guesses allowed "+allowedMisses);
        System.out.println("Start guessing..");       
        guessWord = wordList[r.nextInt(wordList.length)];
        tryToGuess();
    }
   
    /**
    * The main method.
    *
    * @param args the arguments
    */
    public static void main(String[] args){
        int choice = 0;
        boolean finished = false;
        while(!finished){
            new Hangman();
            System.out.println("Do you want to continue? (y/n) ");
            try {
                choice = System.in.read();
            } catch (IOException e) { }
            if(choice=='y'){
                continue;
            }
            finished = true;
        }
    }
}

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