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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!