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

(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, number1 is greater than or equal to number2. For a division question such asnumber1 / 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.

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 1

What is 1 + 7? 8

Correct

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 1icon>

What is 4 + 0? 5

Your answer is wrong. The correct answer is 4

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 4

What is 4 / 5? 1

Your answer is wrong. The correct answer is 0.8

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice:

import java.util.Scanner;

import java.text.DecimalFormat;

public class MathTutor {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Main menu");

System.out.println("1: Addition");

System.out.println("2: Subtraction");

System.out.println("3: Multiplication");

System.out.println("4: Division");

System.out.println("5: Exit");

int userChoice = 0;

System.out.println("Enter a choice:");

userChoice = input.nextInt();

while (userChoice != 5) {

int firstNum = (int)(Math.random() * 10);

int secondNum = (int)(Math.random() * 10);

switch (userChoice) {

case 1:

int adding = firstNum + secondNum;

System.out.println(firstNum +" + "+secondNum+"?");

int addingInput = input.nextInt();

if (addingInput == adding)

System.out.println("correct");

else

System.out.println("Your answer is wrong. The correct answer is "+ adding);

break;

case 2:

int sub = firstNum - secondNum;

System.out.println(firstNum +" - "+secondNum+"?");

int subInput = input.nextInt();

if (subInput == sub)

System.out.println("correct");

else

System.out.println("Your answer is wrong. The correct answer is "+ sub);

break;

case 3:

int mult = firstNum * secondNum;

System.out.println(firstNum +" * "+secondNum+"?");

int multInput = input.nextInt();

if (multInput == mult)

System.out.println("correct");

else

System.out.println("Your answer is wrong. The correct answer is "+ mult);

break;

case 4:

while (secondNum == 0){

// to make sure the bottom number is never zero

secondNum = (int)Math.round((Math.random()*10));

};

double div = firstNum / secondNum;

System.out.println(firstNum +" / "+secondNum+"?");

double divInput = input.nextDouble();

if (divInput == div)

System.out.println("correct");

else

System.out.println("Your answer is wrong. The correct answer is "+ div);

break;

case 5:

System.exit(0); break;

default:

System.out.println("Error: Out of range");

System.exit(0);

}

System.out.println("Enter a choice:");

userChoice = input.nextInt();

}

System.out.println("Thank you for using the Math Program. Have a Great Day!");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!