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

Create a hangman program. Sample output from your program should look like the f

ID: 3640603 • Letter: C

Question

Create a hangman program.
Sample output from your program should look like the following:

C:work>java Hangman
Current Status for userInpts=
_ _ _ _ _ _ _
Enter next letter
a

Current Status for userInpts=a
_ _ _ a _ _ _
Enter next letter
e

Current Status for userInpts=ae
_ _ _ a _ _ e
Enter next letter
f

Current Status for userInpts=aef
_ _ _ a f f e
Enter next letter
g

Current Status for userInpts=aefg
g _ _ a f f e
Enter next letter
i

Current Status for userInpts=aefgi
g i _ a f f e
Enter next letter
r

Current Status for userInpts=aefgir
g i r a f f e
Congratulations: you guessed the word!!
Do you want to play again? (y or n)
y
Current Status for userInpts=
_ _ _ _ _ _
Enter next letter
a

Current Status for userInpts=a
_ a _ _ _ _
Enter next letter
b

Current Status for userInpts=ab
b a b _ _ _
Enter next letter
c

Current Status for userInpts=abc
b a b _ _ _
Enter next letter
o

Current Status for userInpts=abco
b a b o o _
Enter next letter
n

Current Status for userInpts=abcon
b a b o o n
Congratulations: you guessed the word!!
Do you want to play again? (y or n)
y
Current Status for userInpts=
_ _ _ _ _ _
Enter next letter
a

Current Status for userInpts=a
_ _ _ _ _ _
Enter next letter
e

Current Status for userInpts=ae
_ _ _ _ e _
Enter next letter
i

Current Status for userInpts=aei
_ _ _ _ e _
Enter next letter
o

Current Status for userInpts=aeio
_ o _ _ e _
Enter next letter
p

Current Status for userInpts=aeiop
_ o _ _ e _
Enter next letter
m

Current Status for userInpts=aeiopm
m o _ _ e _
Enter next letter
n

Current Status for userInpts=aeiopmn
m o n _ e _
Enter next letter
k

Current Status for userInpts=aeiopmnk
m o n k e _
Enter next letter
y

Current Status for userInpts=aeiopmnky
m o n k e y
Congratulations: you guessed the word!!
Do you want to play again? (y or n)



**************************************************************************
**************************************************************************
**************************************************************************
**************************************************************************
**************************************************************************

Your program should fit the following programming structure:

import java.util.*;

class Hangman
{
static Scanner scan = new Scanner(System.in);
static Random rand = new Random();

// The following routine will determin if the character c
// is inside the String str. A true is returned if it is inside.

static boolean isIn(char c, String str)
{
//********** Fill in Details
}

// If userInputs contains "ard" and strToGuess contains "aardvark" then
// the following routine prints out an output that looks something like:
//
// Current Status for userInpts=ard
// a a r d _ a r _

// This routine returns true if all letters were guessed, otherwise false is returned.


static boolean printCurrStatus(String strToGuess, String userInputs)
{
//********** Fill in Details
}

// The following routine will return a random String from the list of words:
// elephant, tiger, monkey, baboon, barbeque, giraffe, simple, zebra,
// porcupine, aardvark

static String getNextWordToGuess()
{
//********** Fill in Details
// HINT: a switch statement can be quite useful here
}

// The following routine plays the hangman game. It calls getNextWordToGuess to
// get the word that should be guessed. It then has a loop which outputs the
// following prompt:
// Enter next letter
//
// A String named userInputs stores all letters selected already.
// Then the routine printCurrStatus is called to print out the current status of
// the guessed word. If printCurrStatus returns true, we are done.

static void playGame()
{
//********** Fill in Details

}

// main will call playGame to play the hangman game.
// Then main will issue the prompt:
// Do you want to play again (y or n)
// If the answer is "y", then call playGame again, otherwise exit

public static void main(String[] args)
{
//********** Fill in Details
}
}

Explanation / Answer

import java.util.*;

class Hangman
{
static Scanner scan = new Scanner(System.in);
static Random rand = new Random();

// The following routine will determin if the character c
// is inside the String str. A true is returned if it is inside.

static boolean isIn(char c, String str)
{
String s = "" +c;
if(str.contains(s))
{
return true;
}
return false;

}

// If userInputs contains "ard" and strToGuess contains "aardvark" then
// the following routine prints out an output that looks something like:
//
// Current Status for userInpts=ard
// a a r d _ a r _

// This routine returns true if all letters were guessed, otherwise false is returned.


static boolean printCurrStatus(String strToGuess, String userInputs)
{
//********** Fill in Details
boolean sw = false;
boolean isWordFound=false;
for(int i = 0;i< strToGuess.length();i++)
{
for(int j =0;j<userInputs.length();j++)
{
if(strToGuess.charAt(i)==userInputs.charAt(j))
{
System.out.print(strToGuess.charAt(i));
sw = true;
}

}
if(!sw)
{
System.out.print("_ ");
}

sw = false;
}

System.out.println();
for(int i = 0;i<strToGuess.length();i++)
{
if(!isIn(strToGuess.charAt(i),userInputs))
{
return false;
}
}

return true;
}

// The following routine will return a random String from the list of words:
// elephant, tiger, monkey, baboon, barbeque, giraffe, simple, zebra,
// porcupine, aardvark

static String getNextWordToGuess()
{
String[] g = {"elephant","tiger","monkey","baboon","barbeque","giraffe","simple","zebra","porcupine", "aardvark"};
int a = rand.nextInt(g.length);
return g[a];
}

// The following routine plays the hangman game. It calls getNextWordToGuess to
// get the word that should be guessed. It then has a loop which outputs the
// following prompt:
// Enter next letter
//
// A String named userInputs stores all letters selected already.
// Then the routine printCurrStatus is called to print out the current status of
// the guessed word. If printCurrStatus returns true, we are done.

static void playGame()
{
//********** Fill in Details
String strToGuess = getNextWordToGuess();
String userInputs="";
String resp;
printCurrStatus(strToGuess, userInputs);
do
{
System.out.println("Please enter a letter:");
resp = scan.next();
userInputs += resp;
if(printCurrStatus(strToGuess, userInputs))
{
System.out.println("Congratulations!!!");
System.out.println("***************************");
System.out.println("***************************");
System.out.println("***************************");
System.out.println("***************************");
break;
}
}while(true);

}

// main will call playGame to play the hangman game.
// Then main will issue the prompt:
// Do you want to play again (y or n)
// If the answer is "y", then call playGame again, otherwise exit

public static void main(String[] args)
{
//********** Fill in Details
String resp;
while(true)
{
playGame();
System.out.println("Do you want to play again? y/n");
resp = scan.next();
if(resp.startsWith("n")||resp.startsWith("N"))
{
System.out.println("Thanks for playing!");
break;
}
}
}
}
Hope this works for you!!!

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