CREATE FOR JAVA USING ECLIPSE 1. Create a class named Hangman with one import st
ID: 3667425 • Letter: C
Question
CREATE FOR JAVA USING ECLIPSE
1. Create a class named Hangman with one import statement: import java.util.Scanner;
2. All of the member variables should be private. You need three string variables and two int variables.
a. private String secretWord;
b. private String disguisedWord;
c. private String lettersRemaining;
d. private int guessesMade; // count total # of guesses e. private int incorrectGuesses; // count # of incorrect guesses
3. Create nine public methods, as follows:
a. Create a getter method for disguisedWord, as shown below. public String getDisguisedWord(){ return disguisedWord; }
b. On Your Own: Create a getter method for secretWord
c. On Your Own: Create a getter method for guessMade
d. public void initialize(String word) { }
e. public String createDisguisedWord(String word){ }
f. public void makeGuess(Character c){ }
g. public boolean isFound(){ }
h. public void playGame(){ }
i. public static void main(String[] args) { }
4. Implement the main method with four lines of code as follows:
a. Instantiate a Hangman game
b. Call the game’s initialize method passing some word like “Happiness”
c. Print a statement: System.out.println("Lets play a round of hangman.");
d. Call the game’s playGame method to start the game.
5. Implement the isFound method such that it compares the secretWord and the disguishedWord. The isFound method returns true if the secretWord equals the disguishedWord or returns false if the secretWord does not equal the disguishedWord. You do not need to write an if statement. Use the String .equals method. You only need to write one line of code that includes the return statement.
6. Implement createDisguisedWord with 28 lines of code as follows:
a. Convert word to all lowercase letters
i. word = word.toLowerCase();
ii. replace all letters with a question mark
1. word = word.replace('a', '?');
2. word = word.replace('b', '?');
3. …
4. word = word.replace('z', '?');
5. return word;
7. Implement initialize with 5 lines of code as follows:
a. Convert the word to all lower case letters and remove leading/trailing white spaces
i. secretWord = word.toLowerCase().trim();
b. set lettersRemaining to the scecretWord
i. lettersRemaining = secretWord;
c. Use the secreteWord to create the disguisedWord
i. disguisedWord = createDisguisedWord(secretWord);
d. Set guessesMade to zero
i. guessesMade = 0;
e. Set incorrectGuesses to zero
i. incorrectGuesses = 0;
8. Implement the last two methods:
public void makeGuess(Character c){
if(Character.isLetter(c)){
String guess = "" + c;
guess = guess.toLowerCase();
int letterPosition = lettersRemaining.indexOf(guess);
boolean goodGuess = letterPosition > -1;
while(letterPosition > -1){
String before = lettersRemaining.substring(0, letterPosition);
String after = lettersRemaining.substring(letterPosition+1);
lettersRemaining = before + "#" + after;
before = disguisedWord.substring(0, letterPosition);
after = disguisedWord.substring(letterPosition+1);
disguisedWord = before + guess + after;
letterPosition = lettersRemaining.indexOf(guess);
}
guessesMade++;
if(!goodGuess)
incorrectGuesses++;
} else
System.out.println("Sorry, your guess must be an alphabet character from a to z");
}
Explanation / Answer
import java.util.Scanner ;
public class Hangman
{
char alphabet ;
String str1;
String str2;
private String secretWord ;
private String disguisedWord ;
private String lettersRemaing ="";
int guessesMade = 0;
int incorrectGuesses = 0;
boolean isFound ;
Scanner input = new Scanner(System.in) ;
public static void main( String arguments[])
{
Hangman play =new Hangman() ;
play.initialize( " happiness " ) ;
System.out.println( " Lets start a round of hangman... " ) ;
play.playGame() ;
}
public void initialize(String str1)
{
secretWord = str1 ;
disguisedWord ="" ;
for(int guessesMade = 0 ;guessesMade < secretWord.length() ;guessesMade++ )
{
disguisedWord + ="?" ;
isFound =false ;
}
}
public void playGame()
{
while(!isFound)
{
System.out.println( " the disguised word is: " +disguisedWord ) ;
System.out.println( " guess a alphabet " ) ;
makeGuess() ;
}
}
private void makeGuess()
{
str2 =input.next() ;
alphabet =str2.charAt(0) ;
if(str2.compareTo( lettersRemaing ) < 0 )
{
lettersRemaing = alphabet + lettersRemaing ;
}
else
{
lettersRemaing = lettersRemaing + alphabet ;
}
if( secretWord.indexOf( alphabet ) < 0 )
{
incorrectGuesses++ ;
System.out.println( " Guesses made: " + guessesMade + "with" +incorrectGuesses +"wrong & letters guessed: " +lettersRemaing ) ;
}
else // alphabet is correct( process alphabet. )
{
guessesMade++ ;
System.out.println( " Guesses made: "+guessesMade +"with "+incorrectGuesses +"wrong & letters guessed: "+lettersRemaing ) ;
replaceGuessedLetter( alphabet ) ;
}
}
public void replaceGuessedLetter( char guessedLetter )
{
if( secretWord.indexOf( alphabet ) >= 0 )
{
String temp =disguisedWord ;
disguisedWord ="" ;
for( int j = 0 ;j < secretWord.length() ;j++ )
{
if( secretWord.charAt(j) == guessedLetter )
{
disguisedWord + =guessedLetter ;
}
else
{
disguisedWord + =temp.charAt(j) ;
}
}
if( disguisedWord.indexOf( '?' ) < 0 )
{
isFound = true ;
System.out.println( "congratulations , u found the secret word : " ) ;
System.out.println( secretWord ) ;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.