Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Create TWO exception classes. Create an Exception class called BadGuessException It will have 2 constructors A no-argument constructor that passes the message Sorry, that

Java

Create TWO exception classes.

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.

Create a class called GuessingGame

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.

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".

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago

Question

=+2 Why did OBI create Centers of Excellence?

Answered: 1 week ago