Create two Exception classes: Create an Exception class called BadGuessException
ID: 3836403 • Letter: C
Question
Create two Exception classes: Create an Exception class called BadGuessException [5 point] 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.[5 point] A parameterized constructor that will pass the parameter to the parent constructor using keyword super.[5 point] Create another Exception class called TooManyGuessesException [5 point] It will have 2 constructors A no-argument constructor that passes the message "Sorry, too many guesses!" to the parent constructor using keyword super.[5 point] 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. [5 point] 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 [5 point] Prompt the user to guess the number. Allow the user up to 5 total attempts to guess the number correctly. [5 point] 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. [15 point] 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. [15 point] Add exception handling to your code to throw and catch a TooManyGuessesException if the user guesses incorrectly more than 5 times [15 point] 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". [15 point]
Explanation / Answer
GuessingGame.java
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
// Declaring variables
int noOfGuesses = 0, randNum, userGuess, allowedAttempts = 5;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Creating an Random Class Object
Random r = new Random();
//generating a random number between 1 and 10
randNum = r.nextInt(10) + 1;
//Displaying the number
System.out.println("Hint: The answer is " + randNum);
System.out.print("Guess the number :");
try {
while (true) {
//Checking whether the number is integer or not
if (!sc.hasNextInt()) {
throw new IllegalArgumentException("Incorrect format for number");
}
userGuess = sc.nextInt();
//Checking whether the number is in the range or not
if (userGuess < 1 || userGuess > 10) {
throw new BadGuessException();
} else {
noOfGuesses++;
}
//Checking whether the number of guesses exceeded
if (noOfGuesses >= 5) {
throw new TooManyGuessesException(noOfGuesses);
}
else if (randNum == userGuess) {
System.out.println("YOU WIN!");
System.out.println("No Of attempts it took to guess :"+ noOfGuesses);
break;
} else {
System.out.print("Guess the number :");
continue;
}
}
} catch (Exception e) {
System.out.println("Exception :" + e);
}
}
}
_____________________
BadGuessException.java
public class BadGuessException extends Exception {
public BadGuessException()
{
super("Sorry, that was an invalid guess!");
}
public BadGuessException(String mesg)
{
super(mesg);
}
}
_____________________
TooManyGuessesException.java
public class TooManyGuessesException extends Exception {
public TooManyGuessesException()
{
super("Sorry, too many guesses!");
}
public TooManyGuessesException(int noOfGuesses)
{
super("Sorry, you guessed "+noOfGuesses+" times");
}
}
____________________
Output#1:
Hint: The answer is 8
Guess the number :One
Exception :java.lang.IllegalArgumentException: Incorrect format for number
____________________
Output#2:
Hint: The answer is 10
Guess the number :1
Guess the number :2
Guess the number :Three
Exception :java.lang.IllegalArgumentException: Incorrect format for number
__________________
Output#3:
Hint: The answer is 8
Guess the number :1
Guess the number :2
Guess the number :3
Guess the number :4
Guess the number :5
Exception :org.students.TooManyGuessesException: Sorry, you guessed 5 times
_____________________
Output#4:
Hint: The answer is 6
Guess the number :1
Guess the number :2
Guess the number :4
Guess the number :6
YOU WIN!
No Of attempts it took to guess :4
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.