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

CST 150 Michael Ruth Homework #5 Due: Wednesday, 4/12/17 (by midnight) For this

ID: 3812102 • Letter: C

Question

CST 150

Michael Ruth

Homework #5

Due: Wednesday, 4/12/17 (by midnight)

For this assignment, you will create a new Netbeans project using the following format

LastNameFirstName-Homework5 and follow all instructions as given for each part of the assignment.

Turn-in Instructions: Once finished, zip your project directory (which should result in a zip file named

LastNameFirstName-Homework5.zip) and turn in via blackboard by the due date. No late homeworks will

be accepted since the solution will appear on the deadline!

(60 points) Create a HangmanRound class using the following UML Diagram and descriptions:

HangmanRound

-hiddenWord:String

-disguisedWord:String

-charactersGuessed:String

-incorrectGuesses:int

<<constructor>>(word:String)

+guess(c:char):boolean

+guessWord(s:String):boolean

+getDisguisedWord():String

+guessesLeft():int

+getGuessedCharacters():String

+wordGuessed():boolean

o (10 points) Only the given methods, instance fields, and constructors as given in the UML

diagram should be included in the class and should all be exactly as described.

o (10 points) You should initialize all instance variables in the constructor, which accepts the word

to be guessed as a parameter.

The disguised word represents the word as shown to the user before the user guesses. It

should be an underscore “_” for all character’s not guessed yet and have spaces in between

the characters. For instance, if your word is “hide” and no characters have been guessed

yet, the disguised word string should be “_ _ _ _”

The charactersGuessed String is a string of all guessed characters (designed to ensure

players don’t pick the same character twice)

The guesses holds the number of incorrect guesses

o (10 points) The guessWord method simply determines if the string s is the same as the

hiddenWord (and sets the disguisedWord to the hiddenWord if it is), getDisguisedWord method

simply returns the disguisedWord variable, getGuessedCharacters() method simply returns the

string of guessed characters, and guessesLeft() returns the number of incorrect guesses left (6 is

the typical max)

o (10 points) The wordGuessed method reports whether or not the round is over by determining

if there are more characters to guess (by comparing disguisedWord and hiddenWord)

o (20 points) The guess method models a guessed character.

First, it adds the character to the list of guessed characters (this method should assume that

the character has not been guessed yet)

It then determines if character is present in the hiddenWord.

o If present, you need to modify disguisedWord to show the correctly guessed characters

and return true.

o If not present, you need to increment the incorrect guesses and return false

(25%) Create a HangmanPlayer class using the following UML Diagram and descriptions:

HangmanPlayer

-name:String

<<constructor>>()

<<constructor>>(name:String)

+getName():String

+setName(pName:String):void

+takeTurn(round:HangmanRound):void

o (5 points) Only the given methods, instance fields, and constructors as given in the UML diagram

should be included in the class and should all be exactly as described.

o (5 points) You should initialize all instance variables in both constructors and create getters &

setters for name

If not given a name, use your name as the name

o (15 points) The takeTurn method takes a round and proceeds as follows:

First, we show the disguised word and the number of incorrect guesses left

Then, we ask the user if they would prefer to guess a character or the word.

o If guess the word, ask the user for a word and try it out on the Round and report result.

o If guess a character, ask the user for a character making sure the character hasn’t been

guessed already. Once presented with a character, guess the character on the round.

Report result of the guess.

(15%) Create a HangmanGame class using the following UML Diagram and descriptions:

HangmanGame

-player:HangmanPlayer

-round:HangmanRound

<<constructor>>()

+play():void

+isGameOver():Boolean

+reportResults():void

o (5 points) Only the given methods, instance fields, and constructors as given in the UML diagram

should be included in the class and should all be exactly as described.

o (5 points) You should initialize all instance variables in both constructors.

You should ask the user for their name and use that to initialize the player.

You can use any string to initialize the Round (OR see extra credit opportunity)

o (5 points) The play(), isGameOver(), and reportResults() methods should operate similarly to the

methods we created in class.

IsGameOver is true iff wordGuessed OR guessesLeft == 0

Extra Credit Opportunity One (+25):

Read a list of words from the given dictionary file and randomly choose one to initialize the round.

You can add private method to the HangmanGame class to do this.

Extra Credit Opportunity Two (+25):

As the homework stands, there is no graphical representation of the man/woman hanging. You

can change that by adding ASCII art to represent the number of guesses left. This is something

additional that must be added in so leave the other parts of the assignment in if you decide to do

it. You can add a public method to HangmanRound class which returns a string to do this.

Note:

If the project or the zipped file is NOT named correctly, I will take 10 points off the total score.

If the project doesn’t contain sufficient comments, I will take 10 points off the total score.

Explanation / Answer

//This is how we can make different methods, I have solved first part of it.

public class HangmanRound{
String hiddenWord;
String disguisedWord;
String charactersGuessed;
int incorrectGuesses;

public static void main(String[] args) {
if(wordGuessed)
System.out.println("Guessed word is"+disguisedWord);
}
public HangmanRound(String word) // Constructor
{
   hiddenWord="hide";
   disguisedWord="_ _ _ _";
   charactersGuessed=0;
   incorrectGuesses=0;
   getGuessedCharacters();
  
}
public boolean guess(char c)
{
}
public boolean guessWord(String s)
{
   if(s.equals(hiddenWord))
   {
   disguisedWord=hiddenWord;
   return true;
   }
   else
   return false;
}
public String getDisguisedWord()
{
return disguisedWord;
}
public int guessesLeft()
{
   return incorrectGuesses;
}
String getGuessedCharacters()
{
while(!guessWord(disguisedWord.trim()))
{
  
   int len = hiddenWord.length();
   int guess=guessesLeft();
   if(guess>6);
   {
   System.out.println("You have exceeded max no. of guesses");
   return;
   }
   else{
   System.out.println("Enter a character to guess");
   Scanner s= new Scanner(System.in);
   char x = s.next().charAt(0);
   for (int i = 0; i < len; i++)
   {
           if(x == hiddenWord.charAt(i))
           {
           if(i==0)
           disguisedWord.charAt(i)=hiddenWord.charAt(i);//set the character guessed in disguised word.
           else
           disguisedWord.charAt(i+1)=hiddenWord.charAt(i);//as disguised word contains spaces so to handle it.
           charactersGuessed++;
           }  
           else
           incorrectGuesses++;
   }
   return disguisedWord.trim();
}
}

}
boolean wordGuessed()
{
   if(disguisedWord.trim()==hiddenWord)
   return true;
   else
   return false;
}
}