Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Create TWO exception classes. Create an Exception class called BadGuessExce

ID: 3843138 • Letter: J

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

Explanation / Answer

import java.util.*;
import java.util.InputMismatchException;
class BadGuessException extends Exception
{
BadGuessException()
{
super("Sorry, that was an invalid guess");
}
BadGuessException(String s1)
{
super(s1);
}
}

class TooManyGuessesException extends Exception
{
TooManyGuessesException()
{
super("Sorry, too many guesses!");
}
TooManyGuessesException(int s2)
{
super("Sorry, you guessed "+s2+" times");
}
}
class GuessingGame
{
public static void main(String args[])
{
int min = 1;
int max = 10;
int guess;
Random rand=new Random();
int numberToGuess=rand.nextInt((max - min) + 1) + min;
int numberOfTries=0;
int numberofIncorrectTries=0;
Scanner input=new Scanner(System.in);
boolean win=false;
try
{
while(win==false)
{
for(int i=1;i<=5;i++)
{
System.out.println("Guess a number between 1 and 10");
guess=input.nextInt();
if(guess<1 || guess>10)
{
throw new BadGuessException("Sorry, that was an invalid guess!");
}
numberOfTries++;

if(guess==numberToGuess)
{
System.out.println("You Win!!");
win=true;
break;
}
else
{
numberofIncorrectTries++;
}
if(numberofIncorrectTries>5)
{
throw new TooManyGuessesException(numberofIncorrectTries);
}
}
}
}
catch(BadGuessException b1)
{
System.out.println("Exception is "+b1);
}
catch(TooManyGuessesException t1)
{
System.out.println("Exception is "+t1);
}
catch(InputMismatchException i1)
{
System.out.println("Sorry, you entered an invalid number format");
}
System.out.println("The Number was "+numberToGuess);
System.out.println("It took you "+numberOfTries+" tries");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote