Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
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
Objective: The objective of this assignment is to gain practice with pen testing a live web application running on a remote server. The live web application is a known vulnerable web application called DVWA (Damn Vulnerable Web Application) with security settings set to low. The web app is running on an AWS EC2 (Elastic Compute Cloud) instance running Ubuntu 22. Note: The point of this assignment is to step it up a notch, we learnt about different web application vulnerabilities and applied that knowledge, now we are going to pen test and enumerable the vulnerabilities of a web app + the underlying infrastructure it is running on. Before you begin please find out what your IP address is and place it in this sheet so that I can track who is doing what: IP Addresses.docx . Tasks: 1- Start by connecting to the target, I did not install a TLS certificate on purpose that is why you are going to connect via http and not via https: http://3.99.221.134/dvwa/login.php 2- Broken Authentication:…
No AI solutions please
No AI solutions please

Chapter 5 Solutions

Java: An Introduction to Problem Solving and Programming (8th 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