Problem: Here is a snippet of code that inputs two integers and divides them: Sc
ID: 3724223 • Letter: P
Question
Problem: Here is a snippet of code that inputs two integers and divides them:
Scanner scan = new Scanner(System.in);
int num1, num2;
double ratio;
num1 = scan.nextInt();
num2 = scan.nextInt();
ratio = (double) num1/num2;
Place this code into a try-catch block with multiple catches so that different error messages are printed if we attempt to divide by zero or if the user enters textual data instead of integers (java.util.InputMismatchException). If either of these conditions occurs, then the program should loop back and let the user enter new data.
Design and run a main method that asks the user for data (include the snippet). Pick options that cause the program to fail. Be aware that the grader will be testing your program with all kinds of data. DO NOT just write one generic catch block to catch all exceptions – your code should produce useful and specific comments for the user.
Extra credit option 1: maximum 3 points
Add a NegativeNumberException that would be triggered if the user put in a negative number for either of the inputs.
Extra credit option 2: maximum 4 points
Modify the previous problem so that the snippet of code is placed inside a method. The method should be named ReturnRatio, read the input from the keyboard, and throw different exceptions if there is division by zero or an input mismatch between text and integer. Create your own exception class for the case of division by zero. Invoke ReturnRatio from your main method and catch the exceptions in main. The main method should invoke the ReturnRatio method again if any exception occurs.
Please include both extra credit options if possible
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// ExceptionHandling.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionHandling {
static Scanner scan = new Scanner(System.in);
/**
* a method which will prompt the user to enter two integers and return the ratio
* @return - the ratio
* @throws DivisionByZeroException - if the second number is 0
* @throws InputMismatchException - if invalid characters are entered instead of an integer
* @throws NegativeNumberException - if any numbers are less than 0
*/
public static double returnRatio() throws DivisionByZeroException,
InputMismatchException, NegativeNumberException {
double ratio = 0;
System.out.println("Enter number 1:");
int num1 = scan.nextInt();
if (num1 < 0) {
/**
* Negative number
*/
throw new NegativeNumberException();
}
System.out.println("Enter number 2:");
int num2 = scan.nextInt();
if (num2 == 0) {
/**
* Division by zero
*/
throw new DivisionByZeroException();
} else if (num2 < 0) {
/**
* Negative number
*/
throw new NegativeNumberException();
}
ratio = (double) num1 / num2;
return ratio;
}
public static void main(String[] args) {
/**
* a variable which keeps the track of the loop
*/
boolean done = false;
/**
* loops until two valid numbers are entered and their ratio is
* displayed
*/
while (!done) {
try {
/**
* Invoking the returnRatio() method
*/
double ratio = returnRatio();
System.out.println("Ratio is " + ratio);
/**
* In any case, if there is any Exception, the program control
* will never reach this point, so if the control reach here, it
* means there were no Exceptions, so we are breaking the loop
*/
done = true;
} catch (InputMismatchException e) {
System.out.println("ERROR: Input should be an integer");
/**
* it is important to flush out the invalid value from the
* scanner, if you are using scanner.nextInt() method, or else
* the code will turn into an infinite loop, we can do this by
* ignoring a line
*/
scan.nextLine();
} catch (DivisionByZeroException e) {
System.out.println(e.getMessage());
scan.nextLine();
} catch (NegativeNumberException e) {
System.out.println(e.getMessage());
scan.nextLine();
}
}
}
}
// DivisionByZeroException.java
public class DivisionByZeroException extends Exception {
/**
* an exception class to represent division by zero
*/
public DivisionByZeroException() {
super("Error: Cannot divide by zero");
}
}
// NegativeNumberException.java
public class NegativeNumberException extends Exception {
/**
* an exception class to represent negative number input
*/
public NegativeNumberException() {
super("Error: Input can't be negative!");
}
}
/*OUTPUT*/
Enter number 1:
-2
Error: Input can't be negative!
Enter number 1:
jjj
ERROR: Input should be an integer
Enter number 1:
9
Enter number 2:
0
Error: Cannot divide by zero
Enter number 1:
9
Enter number 2:
5
Ratio is 1.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.