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

JAVA Exception Handling This program must be written as a console application. N

ID: 3808029 • Letter: J

Question

JAVA

Exception Handling

This program must be written as a console application. Not GUI,

The "high-low" guessing game. How it works:

The user attempts to guess a random generated number by the program. If the number is too high, then the program will display "too high." If the number is too low, then the program will display "too low." This will continue until the user correctly guesses the number.

Task for this assignment is to implement a guessing game using exceptions. Implement an exception-handling console application with the following:

Program generates random number that the user tries to guess

User enters guess

Program generates tooHigh, tooLow or correct exceptions.  You must write custom exception classes for each of these situation (For example: TooHigh.java, TooLow.java)

Exception handler throws appropriate exception and a response is issued from the exception class using the getMessage() method.

User is allowed to continue guessing and evaluation is repeated until correct guess is made.

Explanation / Answer

GuessingGame.java

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

   public static void main (String[] args)
   {
   Random rand = new Random();
   Scanner scan = new Scanner(System.in);
   int numberToGuess = rand.nextInt(1000);
   int guess;
   boolean win = false;
  
   while (win == false) //loop until correct answer is guessed
   {
   System.out.println("Guess a number between 1 and 1000: ");
   guess = scan.nextInt();
   try{
   if (guess == numberToGuess)
   {
   win = true;
   }
   else if (guess< numberToGuess)
   {
       throw new TooLow( "The number you guessed is too low please ");
   }
   else if (guess > numberToGuess)
   {
   throw new TooHigh( "The number you guessed is too high please ");
   }
   }
   catch(TooLow e){
       System.out.println(e.getMessage());
   }
   catch(TooHigh e){
       System.out.println(e);
   }
   }
  
   System.out.println("You win!");
   }
}

TooHigh.java


public class TooHigh extends Exception{
   String errorMsg ;
   public TooHigh(String s){
       this.errorMsg = s;
   }
public String toString(){
return (errorMsg ) ;
}  
}

TooLow.java


public class TooLow extends Exception{
   String errorMsg ;
   public TooLow(String s){
       this.errorMsg = s;
   }
public String toString(){
return (errorMsg ) ;
}  
}

Output:

Guess a number between 1 and 1000:
300
The number you guessed is too high please
Guess a number between 1 and 1000:
100
The number you guessed is too low please
Guess a number between 1 and 1000:
200
The number you guessed is too high please
Guess a number between 1 and 1000:
150
The number you guessed is too low please
Guess a number between 1 and 1000:
175
The number you guessed is too low please
Guess a number between 1 and 1000:
190
The number you guessed is too high please
Guess a number between 1 and 1000:
185
The number you guessed is too high please
Guess a number between 1 and 1000:
180
The number you guessed is too low please
Guess a number between 1 and 1000:
182
The number you guessed is too high please
Guess a number between 1 and 1000:
181
You win!