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...
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 * ** *** **** ***** ****** *******
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...
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...
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents....
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents.  The formula for converting a temperature from Celsius to Fahrenheit is F = 9/ 5 C + 32 where F → Fahrenheit temperature C → Celsius temperature.  Allow the user to enter the range of temperatures in Celsius to be converted into Fahrenheit.  Your program must use a loop to display the values of the temperature conversions (see sample output). Sample...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT