Question

IN JAVA: Write a program that displays a menu as shown in the sample run. You...

IN JAVA: 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. 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. Make sure the program can recover from an outofboundsexeption, so it resets the menu. Also, make the division show the decimal answer, and not just the whole number.

Use this program as the foundation:

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();
       //WORK ON WHILE
       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!");

   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.lang.Math;
public class Main
{
   public static void main(String[] args) {
   Scanner input = new Scanner(System.in);
   //Random rand = new Random();
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 choice=0;
System.out.println("Enter a choice:");
try{//use this try block for checking input mismatch
choice = input.nextInt();
while(choice!=5)
{
int firstNum=(int)(Math.random()*10);
int secondNum=(int)(Math.random()*10);
try{
switch(choice)
{
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:
while(firstNum<secondNum)//read the values until we get firstnum>secondNum
{
firstNum=(int)(Math.random()*10);
secondNum=(int)(Math.random()*10);
}
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 = (double)firstNum / secondNum;//use typecasting here to get decimal result
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);
  
}
}
catch(ArithmeticException e)//we used exception this is for arithmetic exception
{
System.out.println("We got arithmetic exception");
}
catch(IndexOutOfBoundsException e)//this is for indexout of bounds exception
{
//they will check one by one it will print when any catch statement satisfies this
System.out.println("Out of bounds exception");
}
catch(NumberFormatException e)
{
System.out.println("number format exception");
}
catch(Exception e)//general exception
{
//if above nothing cathes it defaully catches
System.out.println("Exception is "+e);
}
System.out.println("Main menu");
System.out.println("1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\n5: Exit");
System.out.println("Enter a choice:");
choice = input.nextInt();

}
}
catch(InputMismatchException e)
{
System.out.println("Input mismatch Enter numeric input");
}
   }
}

Add a comment
Know the answer?
Add Answer to:
IN JAVA: Write a program that displays a menu as shown in the sample run. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • Java language Part IV. Write programs (15 pts each) rite a program that generates a random...

    Java language Part IV. Write programs (15 pts each) rite a program that generates a random question of adding or subtracting two integers number and number2 under 20 (numbernumber2 in the case of subtraction) and prompt the user to enter the answer. After the user types the answer in the console, the program displays a message to indicate whether the answer is correct and gives the solution if the answer is wrong. Two sample runs are shown below. Tip: generat...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The...

    Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Op1 op op2 = There is no space between operands and operator. Note 5/2 = quotient 2, remainder 1 2$3 = 8 The output should be of the form 2*3=6. Read date from a file. TEST DATA (input.txt): AAAA+BBF= BFD+2DE= 100*AA= 100$5= 100/F= 10000000000000-1= AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT