Question
Package Name: hmwk11 Source File Name: (Submit zip of these) KidCalc.java KidCalcTest.java Test Files: C2800_Ch11_Hmwk1_KidCalc_Test.zip This program will allow us to practice exceptions happening in
Package Name: | hmwk11 |
Source File Name: (Submit zip of these) | KidCalc.java KidCalcTest.java |
Test Files: | C2800_Ch11_Hmwk1_KidCalc_Test.zip |
This program will allow us to practice exceptions happening in a variety of places.
Do you remember being in elementary school and doing basic arithmetic with buttons or m&ms or ? We are going to write a Kid Calculator that will take 2 numbers are draw out visually the +, -, *, and /.
KidCalc class:
2 Instance variable for the 2 integers
Constructor with two parameters to set instance variables. Throw RuntimeException if either number is negative
showAdd method that takes no parameters and prints a visual representation of addition. If the numbers passed to the constructor were 6 and 2, then this would print:
6 + 2 = 8 --> xxxxxx xx
showSub method that takes no parameters and prints a visual representation of subtraction. This method will throw ArithmeticException if the second number is larger than the first number. If the numbers passed to the constructor were 6 and 2, then this would print:
6 - 2 = 4 --> xxxx//
showMult method that takes no parameters and prints a visual representation of multiplication. If the numbers passed to the constructor were 6 and 2, then this would print:
6 * 2 = 12 --> (xx)(xx)(xx)(xx)(xx)(xx)
showDivide method that takes no parameters and prints a visual representation of division. This method will throw ArithmeticException if you cannot evenly divide num2 into num1 (i.e. if there is a remainder). If the numbers passed to the constructor were 6 and 2, then this would print:
6 / 2 = 3 --> (xx)(xx)(xx)
KidCalcTest class contains main()
Here are notes for main():
Use a loop and a try-catch block to continue reading from the user until 2 integers have been read. You should be catching InputMismatchException to handle when the user types letters instead of digits. DO NOT USE nextLine in the try block (however I encourage it in the catch block). DO USE nextInt() in the try block.
Have another try-catch block
Create a new KidCalc object
Call showAdd, showSub, showMult, and showDivide on your new KidCalc object
catch blocks forRuntimeException and ArithmeticExceptionBoth should print a message to standard error and then the stack trace
Unable to create object for RuntimeException
Unable to do the math for ArithmeticException
Include finally it prints ALL DONE!
Requirements:
Put System.out.println() after each line of input. This will help you match the Test Program and make it easier to use it.
NOTE if you are using the test program, it is unlikely that you will get identical, but you should only differ by a couple of lines. Filenames and line numbers are printed in the stack trace and those lines will likely be different
Sample Run #1: (the highlighted text is what the user types)
Num1 num2 6 2
6 + 2 = 8 --> xxxxxx xx
6 - 2 = 4 --> xxxx//
6 * 2 = 12 --> (xx)(xx)(xx)(xx)(xx)(xx)
6 / 2 = 3 --> (xx)(xx)(xx)
ALL DONE!
Sample Run #2: (the highlighted text is what the user types)
Num1 num2 -3 2
Unable to create object
java.lang.RuntimeException: Nums must be positive
at hmwk11.KidCalc.(KidCalc.java:11)
at hmwk11.KidCalcTest.main(KidCalcTest.java:34)
ALL DONE!
Sample Run #3: (the highlighted text is what the user types)
Num1 num2 2 6
2 + 6 = 8 --> xx xxxxxx
Unable to do the math
java.lang.ArithmeticException: Num2 is too large
at hmwk11.KidCalc.showSub(KidCalc.java:30)
at hmwk11.KidCalcTest.main(KidCalcTest.java:36)
ALL DONE!
Sample Run #4: (the highlighted text is what the user types)
Num1 num2 5 3
5 + 3 = 8 --> xxxxx xxx
5 - 3 = 2 --> xx///
5 * 3 = 15 --> (xxx)(xxx)(xxx)(xxx)(xxx)
Unable to do the math
java.lang.ArithmeticException: Not evenly divisible
at hmwk11.KidCalc.showDivide(KidCalc.java:55)
at hmwk11.KidCalcTest.main(KidCalcTest.java:38)
ALL DONE!
Sample Run #5: (the highlighted text is what the user types)
Num1 num2 3 xx
Integers entered were invalid
Num1 num2 6 2
6 + 2 = 8 --> xxxxxx xx
6 - 2 = 4 --> xxxx//
6 * 2 = 12 --> (xx)(xx)(xx)(xx)(xx)(xx)
6 / 2 = 3 --> (xx)(xx)(xx)
ALL DONE!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started