Question
JAVA question, extending the Exception class. following is the instruction: This time when the user enters an int that is Java legal but out of
JAVA question, extending the Exception class.
following is the instruction:
This time when the user enters an int that is Java legal but out of the 1..100 range, you will manually throw an Exception of type NumberOutOfRangeException. This NumberOutOfRangeException type is not a built-in Java Exception type. This is a type (a new class) that you will write by extending the built-in Exception type.
There are 3 possible Exceptions that the .nextInt() method can throw
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted DONT WRITE A CATCH BLOCK FOR THIS
IllegalStateException - if the scanner is closed DONT WRITE A CATCH BLOCK FOR THIS
You do not have to code up catch blocks for the last two cases because the given code should not trigger these cases. Just let the general Exception case at the bottom of your stack catch these and any other unexpected errors. You should write the following blocks.
open the scanner to read kbd do { prompt user for the number try { read the number from the keyboard if the number is out of range then: throw new NumberOutOfRangeException(); // YOU WRITE THIS CLASS BELOW MAIN } catch ( InputMismatchException e ) // jumps in here if your enter "tim" instead of a number { print "Input was not an integer " clear the keyboard buffer with .nextLine() // // must do for this case } catch ( NumberOutOfRangeException e ) // jumps in here if you threw your NumberOutOfRangeException { dont really have to write any code here } catch ( Exception e ) // catches EVERYTHING ELSE { System.out.prinln( e ); System.exit(0); } } while ( number not in range 1..100 ); print the thank you message
Below main you must write your new Exception class named NumberOutOfRangeException that extends Exception. In this class you will overwrite the default constructor such that it prints "Number out of range. Must be in 1..100 "
The behavior and output of your Project#11 solution will be identical in appearance to your Exercise#2 solution from Lab#11. The difference is in how it is written. If a user were to run your Lab#11 Exercise#2 solution and then run your Project#11 solution, they should not be able to detect any difference in appearance or behavior regardless of what inputs are fed to it by the user.
The difference, of course, is in how it is written: but by all appearances they are indistinguishable. You must rename it to: Project11.java and add a class definition (named NumberOutOfRangeException) to the bottom of the Project#11 file. This new class will extend the general Exception class.
the following is my code for Exercise2 and the code for Project11 that I am working on right now
import java.io.*;
import java.util.*;
public class Exercise2
{
public static void main( String args[] )
{
int n = 0;
do
{
try
{
Scanner kbd = new Scanner(System.in);
System.out.print("Enter int in range 1..100 inclusive: ");
n = kbd.nextInt();
if(n > 100 || n < 1)
{
throw new Exception("Number out of range. Must be in 1..100");
}
}
catch(InputMismatchException i)
{
System.out.println("Input was not an integer");
}
catch(Exception e)
{
System.out.println(e);
}
}while(n > 100 || n < 1);
System.out.format(" Thank you. You entered %d ", n );
}
}
import java.io.*;
import java.util.*;
public class Project11
{
public static void main( String args[] )
{
int n = 0;
do
{
try
{
Scanner kbd = new Scanner(System.in);
System.out.print("Enter int in range 1..100 inclusive: ");
n = kbd.nextInt();
outOfRange(n);
}
catch(InputMismatchException i)
{
System.out.println("Input was not an integer");
}
catch(NumberOutOfRangeException o)
{
System.out.println(o.getMessage());
}
catch(Exception e)
{
System.out.println(e);
System.exit(0);
}
}while(n > 100 || n < 1);
System.out.format(" Thank you. You entered %d ", n );
}
public static int outOfRange(int n) throws NumberOutOfRangeException
{
if(n > 100 || n < 1)
{
throw new NumberOutOfRangeException("Number out of range. Must be in 1..100");
}
return n;
}
public class NumberOutOfRangeException extends Exception
{
public NumberOutOfRangeException(String message)
{
super(message);
}
}
}
Please help me with this question. Thank you.
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