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

For this question, you will create your own objects in order to implement the ga

ID: 3679340 • Letter: F

Question

For this question, you will create your own objects in order to implement the game of hangman. Hangman
is a guessing game for two players, or a player and a computer. One player (or a computer) thinks of
(generates) a word, phrase or sentence and the other tries to guess it by suggesting letters. The guessing
player has a limit on the maximum number of incorrect guesses she can make before losing the game.
She knows how many letters are in the word, and is told which letters are correct as she make guesses.
She also has access to the list of letters she has already guessed.
1. For the rst part, you will create a Letter class. A letter in hangman has two private attributes:
(a) A char value, value, indicating the character value of the letter.
(b) A boolean value, isGuessed, indicating whether or not its value has been guessed by the
player.
The Letter constructor takes as input the character of the letter and sets value accordingly. It
also sets the default value of isGuessed to be false.
This class also has the following public methods:
getValue() This method returns the value of the value attribute.
getRevealed() This method returns the value of the isGuessed attribute.
reveal() This method changes the value of isGuessed from false to true.
Be sure to test your Letter class thoroughly before moving on to the next part!
2. In the next part, you will create the Hangman class. This class has the following private attributes:
An array of Letter values called letters.
A char array called guesses. This array will store (at least) all of the incorrect, valid characters
guessed by the player.
A boolean, gameOver, indicating whether or not the game is over.
An int, maxNumGuesses, which stores the maximum number of incorrect guesses permitted.
An int, numGuessesMade, which keeps track of the number of incorrect guesses made by the
player.
The Hangman class has the following public methods:
A constructor that takes as input a String corresponding to the mystery word. It converts the
String to uppercase and then sets all of the values in the letters array to the characters of the
input String. This constructor also sets the maximum numbers of incorrect guesses to a default
value of eight.
A constructor that takes as input a String corresponding to the mystery word, as well as the
maximum number of incorrect guesses. It then sets the letters and maxNumGuesses attributes
accordingly.
A playGame method that takes no input, returns nothing, and plays a complete game of hangman
from start to nish.
Note: in the constructors, you may assume that the input String contains only valid characters. Addi-
tionally, be sure to initialize the guesses array to a reasonable size, as well as initialize all of the other
private attributes.
You will also need to write the following private methods:
A method called guess that takes as input a character and returns a boolean value indicating
whether or not the guess was valid (invalid guesses include symbols, and incorrect characters that
have already been guessed). If it is valid and incorrect, this method will convert the character to
uppercase, and add the character to the guesses array. If it is valid and correct, it will update the boolean isGuessed attribute of any applicable Letter values in the letters array. If applicable, it
will also update the value of the numGuessesMade attribute.
A method called displayBoard that prints the following information: The valid incorrect guesses
made thus far, the (partially revealed) word, and the number of remaining guesses.
You may write any additional helper methods that you deem appropriate. Ensure that any such methods
you write are private.
When the game nishes, you should either indicate to the player that they have won, or let them know
that they have lost. In both cases, you should print the full mystery word.
A sample run-through of the game is shown on the last page of this assignment. You are welcome to
change the phrasing of the dialogue, as long as you include all relevant information.

Explanation / Answer

import java.util.Random; import java.util.Scanner; public class HangMan { public static char[] star; public static void main (String args[]) { char game[]; Scanner input = new Scanner(System.in); Random r = new Random(); String[] arr = { "pakistan", "india", "jarmany", "america", "rashia", "iran", "iraq", "japan", "sudan", "canada"}; String word = arr[r.nextInt(arr.length)]; int count = word.length(); char[] CharArr=word.toCharArray(); char[] star = word.toCharArray(); for(int i=0;i
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