Math tutor) Write a program that displays a menu as shown in the sample run. You
ID: 3706309 • Letter: M
Question
Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2. 3, or 4 for choosing an addition. subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication or division. For a subtraction such as number1 - number2, numberl is greater than or equal to number2. For a division question such as numberl / number2, number2 is not zero. I have enclose the program below and also in D2L, what you are required to do is to include exception handler that deals with nonnumeric operands, ArithemeticException and any others you think is required by this program. OutputExplanation / Answer
Please find the below code.. you can comment if you have any doubt or any further improvement.
Code:
import java.util.Scanner;
import java.io.*;
public class Calculate
{
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
if(n > m) {
throw new ArithmeticException("number2 cannot be greater than number1");
}else{
sub = m - n;
System.out.println("Result:"+sub);
}
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
try {
div = (double)m / n;
System.out.println("Result:"+div);
}
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
break;
case 5:
System.exit(0);
}
}
}
}