QuestionQuestion

Assignment 10 (Chapter 11,)
Create two Exception classes:

Create a class called GuessingGame

Submission

Create an Exception class called BadGuessException

It will have 2 constructors

A no-argument constructor that passes the message "Sorry, that was an invalid guess!" to the parent constructor using keyword super.

A parameterized constructor that will pass the parameter to the parent constructor using keyword super.

Create another Exception class called TooManyGuessesException It will have 2 constructors

A no-argument constructor that passes the message "Sorry, too many guesses!" to the parent constructor using keyword super.

A parameterized constructor that takes an integer parameter, and passes a String "Sorry, you guessed N times", to the parent constructor using keyword super.

Where N is the value of the parameter.

Using the Random class, generate a random number between 1 and 10. Print out the answer to the screen, to make testing the program easier.
For example, you can print out "Hint: The answer is N", where N is the number that was generated [5 point]

Prompt the user to guess the number. Allow the user up to 5 total attempts to guess the number correctly.

Once the user guesses the number correctly, print out "YOU WIN!" and also print out how many attempts it took the user to guess the number.

Using a single try-catch block

Add exception handling to your code to throw and catch a BadGuessException if the user enters any number that is not between 1 and 10.

Add exception handling to your code to throw and catch a TooManyGuessesException if the user guesses incorrectly more than 5 times

Add another catch block to handle the exception when the user enters an incorrect format for number they guessed, such as "five" or "5.0".
This should prevent your program from crashing or throwing a run time exception.

When this exception is caught, the program should not crash and should display "Sorry, you entered an invalid number format".

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;

/*
* Class which runs Guessing Game
*/
public class GuessingGame {

    // executable main method
    public static void main(String[] args) {
      
       Random r = new Random();
      
       int N = r.nextInt(10) + 1;
      
       System.out.println("~ Number Guessing Game ~");
      
       System.out.println("Hint: the...

By purchasing this solution you'll be able to access the following files:
GuessingGame.java, BadGuessException.java and TooManyGuessesException.java.

$30.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats