Need Help for Java Code please: Part 1 to 3 involve with an IntCalculator.java.
ID: 3572118 • Letter: N
Question
Need Help for Java Code please:
Part 1 to 3 involve with an IntCalculator.java.
Part 1. We needed an extra if-statement inside the division case to not crash the program. Instead of having an if-statement, use a try and catch block to stop this error from happening. Think about where the try block should start and end. (Hint: the error is called “ArithmeticException”.)
TLDR: Your program should not crash with exception ArithmeticException.
Part 2. The calculator might also crash when you type in the inputs. For example, you give something other than integers for “first” and “second”. Handle this runtime error with a try and catch block as well. Again, think about where the try block should start and end. (Hint: the error type is “InputMismatchException”. You will have to import this from java.util.InputMismatchException)
TLDR: Your program should also not crash on exception InputMismatchException now.
Part 3. Parts 1 and 2 had the try and catch blocks inside the method where the error occurred. Rewrite the code so the try and catch blocks are inside main() instead of the method where the errors actually happen. (Note: as stated above, your code for parts 1 and 2 should not have been inside main().)
TLDR: Your program should not crash and the try and catch blocks are inside a separate method.
/////////////////////////////////////////////////////////////////////////////////////////
IntCalculator.java
Explanation / Answer
Please find the answers below :
Part 1 & Part 2 :
----------------------------------------------------
OUTPUT SAMPLES:
Please enter an integer calculation:
5
+
y
Invalid input
5 + 0 = 5
--------------------
Please enter an integer calculation:
7
/
0
Error. Cannot divide by zero!
7 / 0 = 0
-------------------------------------------------------------------------------------
part 3:
--------------------
SAMPLE OUTPUTS:
Please enter an integer calculation:
4
/
0
Error. Cannot divide by zero!
---------------------
Please enter an integer calculation:
5
+
w
Invalid input
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.