Program Improvement:
The following program performs division and does not throw an exception when you input a zero for the divisor. 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 dividend; int divisor; System.out.print("Enter dividend: "); dividend = stdIn.nextDouble(); System.out.print("Enter divisor: "); divisor = stdIn.nextInt(); System.out.println(dividend / divisor); } // end main} // end Division2 classa) First, rewrite the program so that it still employs a
doubledividend and
intdivisor, but if the user input for the divisor is zero, it refuses to perform the division operation, and keeps asking for the divisor 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 the dividend or the divisor, the entire input query repeats until both formats are OK. Hint: Put
tryand
catch blocksin a loop that executes
while (OK = = false),and set
OK = trueafter all of the critical operations in the
tryblock have succeeded. Note: If the scanned format is bad, you’ll get infinite looping unless you re-instantiate
stdInin each iteration, or use a two-step operation for each input (input a string and then parse it).
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.