Question

Please solve in java Write a program that serves as a simple calculator. The program should...

Please solve in java

Write a program that serves as a simple calculator. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. You'll need to read the operator as a String and compare it using Strings, as you did in 4.1.

Turn in your source code and your output. A single output is sufficient.

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

If you have any doubts, please give me comment...

import java.util.Scanner;

public class SimpleCalculator{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter first number: ");

int n1 = in.nextInt();

System.out.print("Enter an operator: ");

String op = in.next();

System.out.print("Enter second number: ");

int n2 = in.nextInt();

switch(op){

case "+":

System.out.println("Addition of "+n1+" and "+n2+" = "+(n1+n2));

break;

case "-":

System.out.println("Subtraction of "+n1+" and "+n2+" = "+(n1-n2));

break;

case "x":

System.out.println("Multiplication of "+n1+" and "+n2+" = "+(n1*n2));

break;

case "/":

System.out.println("Division of "+n1+" and "+n2+" = "+(n1/n2));

break;

default:

System.out.println("Invalid operator!");

}

}

}

Add a comment
Know the answer?
Add Answer to:
Please solve in java Write a program that serves as a simple calculator. The program should...
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
  • Simple calculator program. The program should ask the user for a number, an operator, and then...

    Simple calculator program. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. Note that "operator" is a reserved word in C++, so you cannot use it as a variable name. Use double variables and setprecision(2).

  • Python Code, please 2.28 Domain Lab 4.1 -- Simple interest Calculator Simple Interest Calculator Your program...

    Python Code, please 2.28 Domain Lab 4.1 -- Simple interest Calculator Simple Interest Calculator Your program should ask the user (in this order) for: Principal Annual Interest Rate Time Period (years) Your program should then calculate and display: Total Interest Generated Total Future Value

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • 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)

  • Writing in Java ,using Arraylist and collection please. 3. Write a simple phonebook program that reads...

    Writing in Java ,using Arraylist and collection please. 3. Write a simple phonebook program that reads in a series of name-number pairs from the user (that is, name and number on one line separated by whitespace) and stores them in a Map from Strings to Integers. Then ask the user for a name and return the matching number, or tell the user that the name wasn't found.

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table...

    Write a program named "VegetablePricer" which prompts the user for a vegetable from the pricing table shown below. The program will then display the cost per pound for that vegetable, again using the table provided. Your program must use a switch statement to process the input value and display the desired output. Be sure to provide a default case in your switch statement which handles unrecognized input. Use named constants for all constant strings (e.g. prompts and vegetable names) and...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • JAVA Write a program which will receive three pieces of data from the user: two floating...

    JAVA Write a program which will receive three pieces of data from the user: two floating point numbers, and a single character +, -, *, or /. Perform input validation on the character. Your code then will send these three pieces of data to a method which will calculate and output the results based on the user's entries. For example, if the user entered: "12.1, 23.2, +" the output would be "12.1 + 23.2 = 35.3." STRONG HINT: in a...

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