Question: Instructions There will be two classes in this project: Hangman and HangmanWord. Hangman will have a main method. It will provide the output and control

Instructions

There will be two classes in this project: Hangman and HangmanWord. Hangman will have a main method. It will provide the output and control when the game ends. HangmanWord will be used to create an object that will keep track of the games word, the letters the user has guessed correctly, the number of incorrect guesses the user has made, and whether the word has been solved or not.

The HangmanWord Class

Create a public class named HangmanWord. Inside this class, do the following:

  1. Create a private instance array of String references named possibleWords. Assign to possibleWords a populated array of strings of your choosing. This will be the list of words the program will randomly select from. Include at least three strings with at least five letters each.
  2. Create a private instance variable of type String named word. This variable will remember the word from the possibleWords array that will be randomly selected later.
  3. Create a private instance array of char named progress. After the games word is randomly selected, an array of char will be created with a number of elements equal to the length of the word. It will originally be filled with dash characters, but later, correctly guessed letters will replace the dashes.
  4. Create a private instance int variable named wrongCount and set it to zero. This will keep track of the number of incorrect guesses the user makes.
  5. Create a public constructor that has no parameters. Inside this constructor, do the following:
    1. Generate a random number from zero to the length of the possibleWords array minus one. For example, if you have five words in the possibleWords array, you should generate a random number from 0 to 4, inclusive. This will be the index of the randomly selected word.
    2. Assign, to the instance variable word, the String found in possibleWords at the index you just generated.
    3. Create and assign, to instance variable progress, a new array of char with a number of elements equal to the length of the String you just assigned to word.
    4. Use the appropriate method from the Arrays class to fill all the elements of progress with the dash character ('').
    5. Review, for your own understanding, everything that will happen when a HangmanWord object is created using this constructor.
  6. Create an instance method named display that has no parameters and returns nothing. Inside display, do the following:
    1. Loop through all of the characters inside of progress and display them all on one line using System.out.print().
    2. After the loop is finished, add one blank System.out.println() statement.
  7. Create a method named guess() that will accept one char named c as an argument and returns a boolean. Inside guess(), do the following:
    1. Declare a boolean variable named matchFound and set it to false. This variable will remain false until c matches one of the characters in word.
    2. Create a for loop with index variable i that starts at zero and ends less than the length of word. For example, if word is "cat", your i variable would loop from 0 to 2. This will loop through the indices of all of the characters in word. Inside this loop:
      1. Compare words character at index i to the incoming character c. If they match:
        1. * Update the progress char array by setting the element at index i to the value of c. * Set the value of matchFound to true;
        2. Let's examine what's going on here.
          1. Suppose that "supreme" and the incoming value of c is 'e'.
          2. In the constructor, you made a char array named progress with a size equal to the length of word and filled it with dashes. In this case, it would look like this: -------
          3. The word "supreme" has letters starting at index 0 and ending at index 6. Your for loop would loop from 0 to 6.
          4. The first time through the loop, index i is 0. word.charAt(i) is 's'. 's' is not equal to cs value of 'e', so do nothing.
          5. The next time through the loop, index i is 1. word.charAt(i) is 'u'. 'u' is not equal to cs value of 'e', so do nothing.
          6. When i reaches 4, word.charAt(i) is 'e'. 'e' is equal to c's value of 'e'. Since this is true, set progress[i] to the value of c. If we displayed it, progress would look like this: ----e--
          7. When i reaches 6, word.charAt(i) is 'e'. 'e' is equal to cs value of 'e'. Since this is true, set progress[i] to the value of c. If we displayed it, progress would look like this: ----e-e
          8. At the end of the guess() method, return the value of matchFound.
      2. Create an instance method named isSolved() that has no parameters and returns a boolean. This method will report true if all of the characters in the word have been guessed. Inside isSolved(), do the following:
        1. Loop through all of the characters in the progress array. If any of them are equal to a dash character, immediately return false from the method.
        2. After the loop, return true. If the loop did not find any dash characters, then all of the letters must have been guessed and the word has been solved, so return true.
      3. Create an instance method named getWrongCount() that has no parameters and returns an int. Have this method return the value of wrongCount.
      4. Create an instance method named getWord() that has no parameters and returns a String. Have this method return the value of word.
      5. Create a class named Hangman in the same package as HangmanWord. Inside HangmanWord, create a main method, and in main, do the following:
      6. Declare a constant int value named MAX_INCORRECT and assign it a value of 5. If the user guesses wrong this many times, the game will be over.
      7. Display the welcome message, Welcome to Hangman.
      8. Create a HangmanWord object and assign the reference to this object to a reference variable named wordObj.
      9. Use System.out.print() to display the message, Here is your word:
      10. Call the display() method on wordObj.
      11. Compile and run your program. If everything is working, you should see output that looks like this:
        1. Welcome to Hangman. Here is your word: ------
        The number of dashes will vary depending on which word wordObj randomly chose.
      12. Create a Scanner object to read input from the keyboard.
      13. Create a while loop that will loop indefinitely. Inside this loop, do the following:
        1. Use System.out.print to display a newline character and Your guess:
        2. Since there is no nextChar() method in Scanner, instead use nextLine() to read a String from your Scanner and assign the value to a variable named guess.
        3. Assign the first character of guess to a char named guessChar.
        4. Pass guessChar to the guess() method of wordObj. Use the result of this method call in an if-else statement.
      14. Run your program two or three times to confirm that it is working correctly. Compare your output to the samples below. Troubleshoot and fix any bugs in logic or output that you find.

        Sample output 1:

        Welcome to Hangman. Here is your word: ------

        Your guess: a a not found! You have used 1 out of 5 missed guesses. Progress: ------

        Your guess: e Correct! Progress: -e----

        Your guess: i i not found! You have used 2 out of 5 missed guesses. Progress: -e----

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!