Question

In: Computer Science

Java Math Tutor: Write a program that displays a menu as shown in the sample run....

Java

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 as number1 / number2, number2 is not zero.

Here is a sample run: (red indicates a user input)

Main menu 1:

Addition 2:

Subtraction 3:

Multiplication 4:

Division 5:

Exit Enter a choice: 1

What is 1 + 7? 8 Correct

Continue? (y/n) y

Enter a choice: 3

What is 2 * 3? 7

Your answer is wrong.

The correct answer is 6.

Continue? (y/n) n

Good bye!

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class allTest {

public static void main (String args[])
{
  
  
Scanner sc = new Scanner (System.in);
float res = 0;
int num1,num2,r;
Random randomNumbers = new Random();


System.out.println("Addition 1");
System.out.println("Subtraction 2");
System.out.println("Multiplication 3");
System.out.println("Division 4");
System.out.println("Exit 5");
  
while(true)
{
   //generate random number
   num1 = randomNumbers.nextInt(9);
num2 = randomNumbers.nextInt(9);
System.out.println("Enter choice: ");
int ch=sc.nextInt();
switch(ch)
{

     
case 1:
   //addintion of number
System.out.println("What is "+num1+" + "+num2);
res=num1+num2;
r=sc.nextInt();
if(res==r)
{
System.out.println("Correct");
  
}
else
{
  
   System.out.println("Your answer is wrong.");
   System.out.println("The correct answer is "+res);
}
  
break;
case 2:
   //subtraction ofnumber
if(num1>=num2)
{
System.out.println("What is "+num1+" - "+num2);
res=num1-num2;
  
  
}
else
{
System.out.println("What is "+num2+" - "+num1);
res=num2-num1;
  
}
r=sc.nextInt();
if(res==r)
{
System.out.println("Correct");
  
}
else
{
  
   System.out.println("Your answer is wrong.");
   System.out.println("The correct answer is "+res);
}
break;
  
case 3:
System.out.println("What is "+num1+" * "+num2);
res=num1*num2;
r=sc.nextInt();
if(res==r)
{
System.out.println("Correct");
  
}
else
{
  
   System.out.println("Your answer is wrong.");
   System.out.println("The correct answer is "+res);
}
break;
case 4:
if(num2==0)
{
System.out.println("divisor is zero");
  
}
else
{
System.out.println("What is "+num1+" / "+num2);
res=num1/num2;
  
}
r=sc.nextInt();
if(res==r)
{
System.out.println("Correct");
  
}
else
{
  
   System.out.println("Your answer is wrong.");
   System.out.println("The correct answer is "+res);
}
break;
  
case 5:
   System.out.println("good bye!");
   System.exit(1);

default:
System.out.println("Invalid input");
break;
  
  
  
}
  
  
System.out.println("Continue? (y/n)");
String c=sc.next();
//check user wants to exit or not
if(c.equals("n") || c.equals("N"))
{
System.out.println("good bye!");
break;
}
  
  
}
  
}

}


Related Solutions

in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Math V3.0 Modify the previous version of this program again so it displays a menu allowing...
Math V3.0 Modify the previous version of this program again so it displays a menu allowing the user to select addition, subtraction, multiplication, or division problem. The final selection on the menu should let the user quit the program. After the user has finished the math problem, the program should display the menu again. This process is repeated until the user chooses to quit the program. If a user selected an item not on the menu, display an error message...
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT