Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program accepts the input of an integer from the keyboard and perf

ID: 3596410 • Letter: T

Question

The following program accepts the input of an integer from the keyboard and performs subtraction and multiplication operations using the Java Math.subtractExact and Math.multiplyExact functions. It has a try/catch sequence designed to catch exceptions thrown for non-numeric keyboard input, subtraction overflow, and multiplication overflow. However, it will not compile. Explain why and what must be done to correct the error.

public class ExactSumMult public static void main(String[] args) int constantlnteger- 10; int difference -0; int product = 0; int input!nteger 0; String inputString = ""; Scanner keyboard - new Scanner(System.in); try System.out.println("enter an integer..."); inputString - keyboard.nextLine; inputlnteger - Integer.parselnt(inputString); difference Math.subtractExact(inputlnteger, constantlnteger); product = Math.multiplyExact(d.fference, inputinteger); System.out.println ("difference "difference "product-"product); catch (NumberFormatException f) f System.out.println ("Input must be digits:" inputString) catch (ArithmeticException sub){ System.out.println ("Overflow at Subtraction: " + inputlnteger+" - " + constantlnteger); catch (ArithmeticException mult) System.out.println ("Overflow at Multiplication: " + difference +"- " + inputlnteger);

Explanation / Answer

The program wasn't able to compile because ArithmeticException have been caught twice in the catch. You have to comment either one of ArithmeticException for successful compilation and execution of the code.