Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Java programming:

Write a RandomNumberGuesser game that extends from the NumberGuesser Game.

your program should have : main.java, NumberGuesser.java and a RandomNumberGuesser.java that is an extended class of NumberGuesser.

public class RandomNumberGuesser extends NumberGuesser {

    // your code here

}

Somewhere in the code in main.java should look something like this:

NumberGuesser guesser = new RandomNumberGuesser(1, 100);

Expert Solution
Check Mark
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Here is another feature we are asked to add to Number Guessing game. A "Guess History". When the program guesses the correct number, it should show the guesses it made along the way.

We'll return to our command line game. Here is a sample run with the new feature:

Guess a number from 1 to 100.

Is it 27? (h/l/c): h

Is it 86? (h/l/c): l

Is it 68? (h/l/c): l

Is it 59? (h/l/c): l

Is it 40? (h/l/c): c

Here are the guesses: 27 86 68 59 40

Great! Do you want to play again? (y/n): y

Guess a number from 1 to 100.

Is it 44? (h/l/c): l

Is it 27? (h/l/c): c

Here are the guesses: 44 27

Great! Do you want to play again? (y/n): n

The last lines of each game are outcome of new added feauture.

You can add the feature by adding a method to NumberGuesser that returns the guesses using this data type: ArrayList<Integer>

The signature should look like this:

    public ArrayList<Integer> getGuessHistory()

The method will be inherited by RandomNumberGuesser. Make sure that it works in both classes. Once it is working you will be able to print out the guess history like this:

    System.out.print("Here are the guesses: ");

    ArrayList<Integer> guesses = guesser.getGuessHistory();

    for (Integer g: guesses) {

      System.out.print(g + " ");

    }

 

    System.out.println();

 

HINT:

You can add a member field to NumberGeusser that will hold the ArrayList:

    private ArrayList<Integer> guessHistory;

You should initialize that member field in the constructor, and re-initialize it in the reset() method.

Your getGuessHistory method should return a copy of that member field. You can make a copy using the ArrayList constructor:

    return new ArrayList<Integer>(this.guessHistory);

You could add a protected helper method to NumberGuesser that adds a guess into that array list:

    protected void addGuess(int guess) {

        this.guessHistory.add(guess);

    }

That could be called where it is that you want to add the guesses to the history. Make sure not to add duplicates.

 

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

We are asked to add an exception to the game. Please see below.

For this part of the assignment you can start by creating your own exception in a file named NumberGuesserIllegalStateException.java, with this code:

public class NumberGuesserIllegalStateException extends Exception {

 public NumberGuesserIllegalStateException(String errorMessage) {

   super(errorMessage);

 }

}

Next modify your code so that the higher and lower methods of both the NumberGuesser and RandomNumberGuesser should throw the exception if there are no more remaining numbers to guess. You might be able to achieve this by adding the logic to your NumberGuesser and letting RandomNumberGuesser inherit the behavior. Or you might need to add the logic to both classes. It will depend on your implementation.

Finally add a try-catch block to your number guessing game so that the user is notified if the user cheats.

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Here is another feature we are asked to add to Number Guessing game. A "Guess History". When the program guesses the correct number, it should show the guesses it made along the way.

We'll return to our command line game. Here is a sample run with the new feature:

Guess a number from 1 to 100.

Is it 27? (h/l/c): h

Is it 86? (h/l/c): l

Is it 68? (h/l/c): l

Is it 59? (h/l/c): l

Is it 40? (h/l/c): c

Here are the guesses: 27 86 68 59 40

Great! Do you want to play again? (y/n): y

Guess a number from 1 to 100.

Is it 44? (h/l/c): l

Is it 27? (h/l/c): c

Here are the guesses: 44 27

Great! Do you want to play again? (y/n): n

The last lines of each game are outcome of new added feauture.

You can add the feature by adding a method to NumberGuesser that returns the guesses using this data type: ArrayList<Integer>

The signature should look like this:

    public ArrayList<Integer> getGuessHistory()

The method will be inherited by RandomNumberGuesser. Make sure that it works in both classes. Once it is working you will be able to print out the guess history like this:

    System.out.print("Here are the guesses: ");

    ArrayList<Integer> guesses = guesser.getGuessHistory();

    for (Integer g: guesses) {

      System.out.print(g + " ");

    }

 

    System.out.println();

 

HINT:

You can add a member field to NumberGeusser that will hold the ArrayList:

    private ArrayList<Integer> guessHistory;

You should initialize that member field in the constructor, and re-initialize it in the reset() method.

Your getGuessHistory method should return a copy of that member field. You can make a copy using the ArrayList constructor:

    return new ArrayList<Integer>(this.guessHistory);

You could add a protected helper method to NumberGuesser that adds a guess into that array list:

    protected void addGuess(int guess) {

        this.guessHistory.add(guess);

    }

That could be called where it is that you want to add the guesses to the history. Make sure not to add duplicates.

 

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

We are asked to add an exception to the game. Please see below.

For this part of the assignment you can start by creating your own exception in a file named NumberGuesserIllegalStateException.java, with this code:

public class NumberGuesserIllegalStateException extends Exception {

 public NumberGuesserIllegalStateException(String errorMessage) {

   super(errorMessage);

 }

}

Next modify your code so that the higher and lower methods of both the NumberGuesser and RandomNumberGuesser should throw the exception if there are no more remaining numbers to guess. You might be able to achieve this by adding the logic to your NumberGuesser and letting RandomNumberGuesser inherit the behavior. Or you might need to add the logic to both classes. It will depend on your implementation.

Finally add a try-catch block to your number guessing game so that the user is notified if the user cheats.

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education