Question

Please make a JAVA program for the following using switch structures Write a program that simulates...

Please make a JAVA program for the following using switch structures

Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the appropriate computation and display the results on the screen (you can use dialog boxes if you want). If the user inputs an invalid menu choice, the program will display the user’s entry with an error message.

Calculator Program

-----------------------------------------------------------------------

        A: Add

B: Subtract

C: Multiply

        D: Divide

        X: Exit

-----------------------------------------------------------------------

            Please make a selection: A

            Please enter the first number: 10

            Please enter the second number: 15

            10 + 15 = 25

Calculator Program

-----------------------------------------------------------------------

        A: Add

B: Subtract

C: Multiply

        D: Divide

        X: Exit

-----------------------------------------------------------------------

            Please make a selection: C

            Please enter the first number: 10

            Please enter the second number: 10

            10 + 10 = 100

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

CODE:

import java.lang.*;
import java.util.Scanner;
public class Calculator
{
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       char option;
       do{
           System.out.println("Calculator Program");
           System.out.println("----------------------------------------");
           System.out.println("A:Add\nB:Subtract\nC:Multiply\nD:Divide\nX:Exit");
           System.out.print("Please Make a selection : ");
           option=sc.next().charAt(0);//Taking character input from user
           if(option!='X'){
               int a,b;
               System.out.print("Please enter first number: ");
               a=sc.nextInt();
               System.out.print("Please enter second number: ");
               b=sc.nextInt();
               switch(option){
                   case 'A':
                       System.out.println(a+" + "+b+" = "+(a+b));
                       break;
                   case 'B':
                       System.out.println(a+" - "+b+" = "+(a-b));
                       break;
                   case 'C':
                       System.out.println(a+" * "+b+" = "+a*b);
                       break;
                   case 'D':
                       System.out.println(a+" / "+b+" = "+a/b);
                       break;
               }
           }
       }while(option!='X');//Iterating until he presses X

   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Please make a JAVA program for the following using switch structures Write a program that simulates...
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
  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit...

    Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit . Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Please answer in python and make it simple. Please implement proper indentation, not just...

  • Write MIPS code that emulates a simple calculator. The calculator initially has the value 0. Prom...

    Write MIPS code that emulates a simple calculator. The calculator initially has the value 0. Prompt the user to enter a command. A command is a number, where 0 is add, 1 is subtract, 2 is multiply, 3 is divide, 4 is clear, and 5 is exit. When the user enters 0, 1, 2, or 3, prompt the user for a number to add, subtract, multiply or divide from the current value. If the user enters 4, then clear the...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Java Data Structures

    Programming Instructions:Using      Java Object Oriented Principles, write a program which produces the      code as indicated in the following specifications: Your       program must be a console application that provides a user this exact       menu:Please select one of the following:1: Add Client to Bank2: Display Clients in the Bank3: Set Bank Name4: Search for a Client5: Exit Enter your Selection: <keyboard input here> The       menu must be displayed repeatedly until 5...

  • Write a C program for the following: (i) (ii) (iii) (iv) (v) A function to read...

    Write a C program for the following: (i) (ii) (iii) (iv) (v) A function to read an NxM matrix (from console input). A function to display the NxM matrix on the screen. A function to add these two matrices. A function to subtract these two matrices. A function to multiply these two matrices. The program must also contain a main function that will first declare two 3x3 matrices A and B, allow input of data for A and B (using...

  • Task 6: Write a Java program to make a calculator. The calculator should add, subtract, multiply...

    Task 6: Write a Java program to make a calculator. The calculator should add, subtract, multiply and divide the numbers inserted by the user. Feel free to add more operations to your calculator, such as power, sqrt etc. (Hint: Use Switch)

  • Menu-driven programs will display the menu options to the user and then prompt them for a...

    Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format: Calculator Options: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the area of a trapezoid Calculate the area of a sphere Exit Enter your choice (1-6) Once the user enters a choice for the menu, the program should use a...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

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