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