Problem

Program Improvement:The following program performs division and does not throw an exceptio...

Program Improvement:

The following program performs division and does not throw an exception when you input a zero for the denominator. It also does not detect input number format exceptions. Minimize the total lines of code required to meet the requirements.

/************************************************************** Division2.java* Dean & Dean** This attempts to prevent division by zero.*************************************************************/import java.util.Scanner;public class Division2{public static void main(String[] args){Scanner stdIn = new Scanner(System.in);double n;int d;System.out.print("Enter numerator: ");n = stdIn.nextDouble();System.out.print("Enter divisor: ");d = stdIn.nextInt();System.out.println(n / d);} // end main} // end Division2 class

a) First, rewrite the program so that it still employs a double numerator and int denominator, but if the value input for the denominator is zero, it refuses to perform the division operation, and keeps asking for the denominator until the user supplies something other than zero.


b) Next, rewrite the program of part a so that if the user inputs an improper format for either numerator or denominator, the entire input query repeats until both formats are OK. Hint: Put try and catch blocks in a loop that executes while (OK == false), and set OK = true after all of the critical operations in the try block have succeeded. Note: If the scanned format is bad, you’ll get infinite looping unless you re-instantiate stdIn in each iteration, or use a two-step operation for each input (input a string and then parse it).

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 14
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT