java hangman program

Anonymous
timer Asked: Feb 26th, 2018
account_balance_wallet $20

Question description

Java Programming I Final Portfolio
Complete the Java Programming I Final portfolio. When you are finished, submit your assessment to your instructor by using the Drop Box on page 13. 
Portfolio Overview
By completing this activity, you will gain experience with nearly every programming subject covered in the course.
Portfolio Goal
A document containing a copy of your program source code text with commented output added demonstrating winning and losing games.
Portfolio Details
For this final portfolio, you will be creating a game of Hangman. The program will randomly generate a word and display a set of spaces showing the missing characters.Welcome to Hangman.
Here is your word: --------
Your guess:

The player will guess characters one at a time. The game will report a message and update the player’s progress based on the results of each guess. 

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

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

The game ends when the player runs out of missed guesses or uncovers all the letters in the word.Your guess: p
Correct!
You found all the letters in the word "report".
You win!Your guess: b
Sorry, you lose.
The word was "antelope".This project involves several programming concepts you encountered throughout the course including object oriented programming. An object will keep track of most of the game’s information and provide methods that grant access to that data.The instructions for this project are fairly detailed, but it is up to you to understand the instructions and implement the proper solutions.

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 game’s 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 game’s 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.
    1. 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.
    2. 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.
    3. 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.
    4. 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 word’s character at index i to the incoming character c. If they match:
            * Update the progress char array by setting the element at index i to the value of c.
            * Set the value of matchFound to true;

            See the next slide for more details on this step.
              1. 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 c’s 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 c’s 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 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-e
              1. At the end of the guess() method, return the value of matchFound.

          1. 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.
          2. Create an instance method named getWrongCount() that has no parameters and returns an int. Have this method return the value of wrongCount.
          3. Create an instance method named getWord() that has no parameters and returns a String. Have this method return the value of word.

          The Hangman Class

          Create a class named Hangman in the same package as HangmanWord. Inside HangmanWord, create a main method, and in main, do the following:
          1. 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.
          2. Display the welcome message, “Welcome to Hangman.”
          3. Create a HangmanWord object and assign the reference to this object to a reference variable named wordObj.
          4. Use System.out.print() to display the message, “Here is your word: ”
          5. Call the display() method on wordObj.
          6. Compile and run your program. If everything is working, you should see output that looks like this:
              Welcome to Hangman.
              Here is your word: ------
            The number of dashes will vary depending on which word wordObj randomly chose.
          7. Create a Scanner object to read input from the keyboard.
          8. 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.



          1. Process a correct guess.

            1. If the result of wordObj.guess(guessChar) is true, do the following:
              • Display "Correct!"

              Has the game been won?

              • Call the isSolved() method on workdObj. If wordObj.isSolved() is true, do the following:
                • Display the following messages:
                  You found all the letters in the word "xxxx".
                  You win!
                  Replace xxxx with the word stored in wordObj, which you can retrieve by calling getWord(). Surround the word with quotation marks in the output.
                • Use the break keyword to break out of the loop and end the program.

              The game hasn't been won yet.

              • Else, do the following:
                • Use System.out.print() to display "Progress:"
                • Call wordObj.display().
                • The above two steps should display something like the following:
                  Progress: a--e---e

                  Your output will vary based on the word being used and the guesses so far.



          1. Process an incorrect guess.

            1. Else (if the result of wordObj.guess(guessChar) is false), do the following:
              • Call wordObj.getWrongCount() and use the result in an if-else statement.

              Has the game been lost?

              • If wordObj.getWrongCount() is greater than or equal to MAX_INCORRECT, display the following:
                Sorry, you lose.
                The word was "xxxx".
              • Replace xxxx with the word stored in wordObj, which you can retrieve by calling getWord(). Surround the word with quotation marks in the output.

              The game has not been lost yet.

              • Else, display the following:
                ? not found!
                You have used x out of y missing guesses.
                Progress: a--e-o-e
                Replace ? with the character the player just guessed. Replace x with the result of wordObj.getWrongCount() and y with the value of MAX_INCORRECT.

          Testing

          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----

        2. Sample output 1, continued:Your guess: o
          Correct!
          Progress: -e-o--Your guess: u
          u not found!
          You have used 3 out of 5 missed guesses.
          Progress: -e-o--Your guess: r
          Correct!
          Progress: re-or-Your guess: t
          Correct!
          Progress: re-ortYour guess: p
          Correct!
          You found all the letters in the word "report".
          You win
        3. Sample output 2:Welcome to Hangman.
          Here is your word: --------Your guess: a
          Correct!
          Progress: a-------Your guess: e
          Correct!
          Progress: a--e---eYour guess: i
          i not found!
          You have used 1 out of 5 missed guesses.
          Progress: a--e---eYour guess: o
          Correct!
          Progress: a--e-o-eSample output 2, continued:Your guess: u
          u not found!
          You have used 2 out of 5 missed guesses.
          Progress: a--e-o-eYour guess: r
          r not found!
          You have used 3 out of 5 missed guesses.
          Progress: a--e-o-eYour guess: s
          s not found!
          You have used 4 out of 5 missed guesses.
          Progress: a--e-o-eYour guess: t
          Correct!
          Progress: a-te-o-eSample output 2, continued:Your guess: n
          Correct!
          Progress: ante-o-eYour guess: b
          Sorry, you lose.
          The word was "antelope".
        4. copy and paste the following iteams into a document 
        5. the code from the hangman class
        6. The code form the hangman world class
        7. output from winging game 
        8. output from losing game




Tutor Answer

ravishekhar
School: University of Maryland

Hey, its done.. Please check and let me know if any issues.. 😃

HangmanWord.java
import java.util.Arrays;
import java.util.Random;
public class HangmanWord {
/*For t...

flag Report DMCA
Review

Anonymous
Thanks, good work

Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors