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

I need to create a basic calculator that handles specific bad output situations

ID: 3647217 • Letter: I

Question

I need to create a basic calculator that handles specific bad output situations namely where a user enters data that is non-integer or non-numerical. In which case the program terminate abnormally. Otherwise ending after a single operation. I need to 1. Handle the bad input situation with the use of exception handling, Bad input that should cause an exception to be thrown are: b. Non-numeric operands c.Unknown operator not any of the four basic operations d. Division by zero. The program should not terminate but should inform the user of the specific error and should re-request the input value which was in error 2. Correct operation will include repeated operation so that the user may perform many operations without restarting the program after each. The program should not terminate but allow the use to recover with a corrected value./ values. This should be done with the use of exception handling. The program should not terminate abnormally under any circumstances. Please write out the complete code for this program and tell me what you did.

Explanation / Answer

I will start by suggesting a tool called an IDE(Eclipse or Netbeans) for doing this assignment. IDE you can design your GUI Interface and the Source code for the interface will be generated by the IDE itself.(You can also create a normal program without GUI also, then you dont have to use IDE) Here I am writing general code. You will have to then fill these code by the operation code where you will do the above mentioned exception handling. Assuming your basic calculator only has +,-,*, and / operators and there will be specific part of the code where you will write code for these operations. In this part you will first enclose a try and catch block I will show for the plus operation main() { do // do while is for many operations { try { System.out.println("Enter num1 and num2 : "); int num1 = new Scanner(); int num2 = new Scanner(); System.out.println("Enter sign : "); char sign = new Scanner(); } catch(IllegalArgumentException ex) // This will check for illegal inputs { System.out.println(ex.Message()); //Will print error message } if(sign == '+') { // part of code where you will write the plus operation System.out.println(num1+num2); } if(sign == '-') { System.out.println(num1-num2); } if(sign == '*') { System.out.println(num1*num2); } if(sign == '/') { try { System.out.println(num1/num2); } catch(ArithmeticException ex)// Check for divide by zero exception { System.out.println("Divide by zero"); } } if(sign == 'x') // to exit { flag = false } else { System.out.println("Error : Unknown Operator "); } }while(flag == true) // flag will be true until user enters 'x' as the choice to exit. } I hope this helps

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote