Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 5, Problem 6PP

Explanation of Solution

a.

Method heading for each method:

  • The method heading is “public void makeGuess(character c)” that takes a parameter “c” of character type and return nothing.
  • The method heading is “public String getDisguisedWord()” that takes no parameters and returns the string which contains the disguised word...

Explanation of Solution

b.

Preconditions and postconditions of each method:

  • Precondition and postcondition of “public void makeGuess(character c)” method.
    • Precondition: checks whether the character value is one of 26 alpha characters which present in secrete word or not.
    • Postcondition: update the number of guesses, incorrect guesses, and disguised word.
  • Precondition and postcondition of “public String getDisguisedWord()” method.
    • Precondition: None.
    • Postcondition: This method returns disguised string.
  • Precondition and postcondition of “public String getSecretWord()” method...

Explanation of Solution

c.

Test class for some Java statement:

//Create an object for Hangman class

Hangman game = new Hangman();

//Call initialize() method to set the secret word

game.initialize("Happiness");

//Call playGame() method to play the game

System.out.println("Lets play a round of hangman...

Explanation of Solution

d.

Implementation of class:

Hangman.java:

//Import the java package

import java.util.Scanner;

//Define the class

public class Hangman

{

    //Declare the required variables

    private String secretWord;

    private String disguisedWord;

    private String lettersRemaining;

    private int guessesMade;

    private int incorrectGuesses;

    //Define the makeGuess() method

    public void makeGuess(Character c)

    {

        //Check whether character is letter

        if (Character.isLetter(c))

        {

            //Declare and assign the variable

            String guess = "" + c;

            //Convert the guess to lower case

            guess = guess.toLowerCase();

            //Declare and initialize the letter position

int letterPosition = lettersRemaining.indexOf(guess);

            boolean goodGuess = letterPosition > -1;

/*Loop executes until the position is greater than "-1". */

            while (letterPosition > -1)

            {

/*Declare and assign the before letter variable. */

String before = lettersRemaining.substring(0, letterPosition);

/*Declare and assign the after letter variable. */

String after = lettersRemaining.substring(letterPosition + 1);

                //Set the letter remaining

                lettersRemaining = before + "#" + after;

                //Set the disguised waord

before = disguisedWord.substring(0, letterPosition);

after = disguisedWord.substring(letterPosition + 1);

                disguisedWord = before + guess + after;

                //Set the word position

letterPosition = lettersRemaining...

Explanation of Solution

e.

List of extra methods and attributes needed for this implementation:

There is no extra attributes are needed for this implementation.

The extra methods that were used apart from the mentioned methods are,

  • public void initialize() method:
    • This method is used to initialize the declared variables...

Explanation of Solution

f.

Implementation of class:

Hangman.java:

//Import the java package

import java.util.Scanner;

//Define the class

public class Hangman

{

    //Declare the required variables

    private String secretWord;

    private String disguisedWord;

    private String lettersRemaining;

    private int guessesMade;

    private int incorrectGuesses;

    //Define the initialize()  method

    public void initialize(String word)

    {

        //Initialize the declared variables

        secretWord = word.toLowerCase().trim();

        lettersRemaining = secretWord;

        disguisedWord = createDisguisedWord(secretWord);

        guessesMade = 0;

        incorrectGuesses = 0;

    }

    //Define the createDisguisedWord() method

    public String createDisguisedWord(String word)

    {

        //Convert the secret word into lower case word

        word = word.toLowerCase();

/*Call replace() method to replace the "?" with suitable letter. */

        word = word.replace('a', '?');

        word = word.replace('b', '?');

        word = word.replace('c', '?');

        word = word.replace('d', '?');

        word = word.replace('e', '?');

        word = word.replace('f', '?');

        word = word.replace('g', '?');

        word = word.replace('h', '?');

        word = word.replace('i', '?');

        word = word.replace('j', '?');

        word = word.replace('k', '?');

        word = word.replace('l', '?');

        word = word.replace('m', '?');

        word = word.replace('n', '?');

        word = word.replace('o', '?');

        word = word.replace('p', '?');

        word = word.replace('q', '?');

        word = word.replace('r', '?');

        word = word.replace('s', '?');

        word = word.replace('t', '?');

        word = word.replace('u', '?');

        word = word.replace('v', '?');

        word = word.replace('w', '?');

        word = word.replace('x', '?');

        word = word.replace('y', '?');

        word = word.replace('z', '?');

        //Return the secret word

        return word;

    }

    //Define the makeGuess() method

    public void makeGuess(Character c)

    {

        //Check whether character is letter

        if (Character.isLetter(c))

        {

            //Declare and assign the variable

            String guess = "" + c;

            //Convert the guess to lower case

            guess = guess.toLowerCase();

            //Declare and initialize the letter position

int letterPosition = lettersRemaining.indexOf(guess);

            boolean goodGuess = letterPosition > -1;

/*Loop executes until the position is greater than "-1". */

            while (letterPosition > -1)

            {

/*Declare and assign the before letter variable. */

String before = lettersRemaining.substring(0, letterPosition);

/*Declare and assign the after letter variable. */

String after = lettersRemaining.substring(letterPosition + 1);

                //Set the letter remaining

                lettersRemaining = before + "#" + after;

                //Set the disguised waord

before = disguisedWord.substring(0, letterPosition);

after = disguisedWord.substring(letterPosition + 1);

                disguisedWord = before + guess + after;

                //Set the word position

letterPosition = lettersRemaining.indexOf(guess);

            }

            //Increment the guess count by "1"

            guessesMade++;

            //Check whether the Guess is not valid

            if (!goodGuess)

                //Increment the incorrect guess by "1"

                incorrectGuesses++;

        }

        //Otherwise, display the error message

        else

System...

Blurred answer
Students have asked these similar questions
1. Level the resources (R) for the following network. Show exactly which activity is being moved at each cycle and how many days it is being moved. Show all cycles required to utilize the free float and the back float. B H 3 3 L 2 0-0-0 A C F G K N P Q T 0 3 2 2 1 2-2-2 7R 8R 4R 6R 4R 2R 5R 4R D 1 2R 2 M 000 4R 2 4R 1 2 3 4 B5 B BE B 5 5 7 D 2003 C NO C MBSCM В H 5 2 F 7 7 8 SH2F80 5 Н Н 6 7 7L3G4+ 6H2G4 J 4 4 14 8 L K 00 36 9 10 11 12 13 14 15 P 2 Z+ N N 4 4 Z t 2334 4 Σ + M M 4 +
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.

Chapter 5 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License