Write a Java application that provides a simple command-line calculator that sup
ID: 3646489 • Letter: W
Question
Write a Java application that provides a simple command-line calculator that supportsaddition, subtraction, multiplication, and division. Multiple operators can be processed in
a single expression.
Valid expressions are of the following form: NUM OP NUM OP ... NUM =, where NUM
represents a decimal number and OP is either a +, -, *, or /. Spaces are required to
separate the numbers from the operators. The equals sign at the end is required to
indicate to the program that it should process the expression and return the result.
For simplicity, this program does NOT support proper order of operations (or
parenthesization). Rather, operations are processed from left to right, regardless of
normal precedence rules. It turns out that good order of operations is a lot more difficult
to implement and requires more knowledge in programming than what we
Explanation / Answer
code is: import java.util.Scanner; public class calculator { public static void main(String[] args) { char answer; String input; int firstnumber; int secondnumber; System.out.println ("Welcome to the calculator!"); System.out.println ("To multiply use m, to divide use d, to add use a, to subtract use s"); Scanner keyboard = new Scanner(System.in); do { System.out.println ("Do you want to multiply, divide, add, or subtract? "); keyboard.nextLine(); input = keyboard.nextLine(); answer = input.charAt(0); if (answer == 'm') System.out.println ("Input first number: "); keyboard.nextLine(); input = keyboard.nextLine(); firstnumber = keyboard.nextInt(); System.out.println ("Would you like another calculation (y/n)? "); keyboard.nextLine(); input = keyboard.nextLine(); answer = input.charAt(0); } while(answer == 'y') if(answer == 'n'); System.exit(0); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.