Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started