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

JAVA - Create a simple guessing game, similar to Hangman, in which the user gues

ID: 3650272 • Letter: J

Question

JAVA - Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks; for example,

Explanation / Answer

package hangman; import java.util.Scanner; public class Hangman { //Scanner Scanner stdIn = new Scanner(System.in); private int maxMisses; private int maxCount; private String word; private String correct; private String misses; //************************************************ // This method Returns a string that displays dashes // for each letter not guessed and letters // where correctly guessed. public String correct() { int i; for (i = 1; i = this.maxMisses; } //***************************************************************** //This Method searches the word to be guessed for the presence of c. If present, replaces //all the corresponding spaces in the correct word, if not present, increments //miss count and adds c to the missed letters string. public void guess (char c) { int missCount = 0; if( misses.indexOf( c ) > -1 ) { // we find char already used before missCount++; System.out.println("You have already played " + c); return; // don't do the rest of this method } if( word.indexOf( c ) < 0 ) { // the guess is not in word missCount++; misses += c; System.out.println( c + " is not in the word"); return; } } //***************************************************************** //This method Takes a String that is the word to be guessed and an integer that //represents the maximum number of incorrect guesses allowed. public void initialize(String word,int maxMisses) { System.out.println("What is the word to be guessed?"); String wordChosen = stdIn.next(); word = wordChosen; this.word = word; this.maxMisses = word.length(); } //******************************************************************** //This method Returns a String containing all the letters guessed that are not in //the word to be guessed. public String lettersMissed() { } //******************************************************************** //This method Returns the maximum number of misses allowed public int maxMissesAllowed() { System.out.println("You can only miss " + this.maxMisses + " letters."); return this.maxMisses; } //******************************************************************** //This method returns the current number of misses public String misses() { System.out.println("You have " + (this.maxMisses - this.maxMisses) + " left..."); return this.misses; } //****************************************************************** //This method returns the word to be guessed public String word() { System.out.println("The word is " + this.word + "."); return this.word; } }