Question

In: Computer Science

JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...

JAVA PROGRAMMING

Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method.

This application needs to hold the user input's numbers as an array.

Thanks for your time. It is a great help!

Solutions

Expert Solution

simple Java code to create a calculator using switch statements:

import java.util.Scanner;

public class JavaExample {

    public static void main(String[] args) {

        double num1, num2;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number:");

       
        num1 = scanner.nextDouble();
        System.out.print("Enter second number:");
        num2 = scanner.nextDouble();

        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = scanner.next().charAt(0);

        scanner.close();
        double output;

        switch(operator)
        {
            case '+':
                output = num1 + num2;
                break;

            case '-':
                output = num1 - num2;
                break;

            case '*':
                output = num1 * num2;
                break;

            case '/':
                output = num1 / num2;
                break;

           
            default:
                System.out.printf("You have entered wrong operator");
                return;
        }

        System.out.println(num1+" "+operator+" "+num2+": "+output);
    }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Java Programming - please read the description I need this description. Inside a do/ while loop...
Java Programming - please read the description I need this description. Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods. One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result. Thanks for your help! Thanks for asking....
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
C programming. Explain by taking a programming example how do while loop is different from while...
C programming. Explain by taking a programming example how do while loop is different from while loop?
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT