Question
Implement a simple, four function calculator. Your programshould prompt the user to select one of the following operations:?a? or ?A? for addition; ?s? or ?S?
Implement a simple, four function calculator. Your programshould prompt the user to select one of the following operations:?a? or ?A? for addition; ?s? or ?S? for subtraction; ?m? or ?M? formultiplication; ?d? or ?D? for division; or ?q? or ?Q? to quit.Next, the user should be prompted to enter two double values. Theprogram should then print out the result of the calculation andprompt the user again to select an available function.
Some test cases to consider: (a) If the user enters a value thatis not one of the calculators functions, the program should tellthe user the input is not recognized; (b) The program shouldrecognize both lowercase and uppercase input.1) Create a new classnamed ?Calculator?.2) Modify your code using the followingsample:
import java.util.Scanner;
public class Calculator{
public static void main(String [] args){
}
public static void add(double x, double y){
}
public static void subtract(double x, doubley) {
}
public static void multiply(double x, doubley) {
}
public static void divide(double x, doubley) {
}
}
3) Implement the four Java methods ?add?, ?subtract?,?multiply?, and ?divide? to print the first operand, the mathsymbol for the operation, the second operand, an equal sign, andthen the result.
4) Implement the ?main? method to accept a user?s input andinvoke the appropriate method to complete the calculation.
5) Test your implementation and verify your output. See theexample outputs from test runs of the program:
Please select an operation ((A) addition); (5) ubtraction; (M) ultiplication: (D)ivision; or (0)uit: a Enter the first operand: 4.5 Enter the second operand: 5.5 4.50 +5.50 = 10.00 Please select an operation ((A) addition); (5) ubtraction: (M) ultiplication: (D) ivision; or (Q) uit: Enter the first operand: 2 Enter the second operand: 9 2.00 - 9.00 = -7.00 Please select an operation ((A) addition); (5) ubtraction: (M) ultiplication: (D) ivision; or (Q) uit: M Enter the first operand: 2 Enter the second operand: 8 2.00 - 8.00 = 16.00 Please select an operation ((A) addition); (5) ubtraction; (M) ultiplication: (D) ivision; or (Q) uit: d Enter the first operand: 9 Enter the second operand: 3 9.00 / 3.00 - 3.00 Please select an operation ((A) addition); (5) ubtraction; (M) ultiplication; (D)ivision; or (Q) uit: q Goodbye...
Step by Step Solution
3.53 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
Calculatorjava Java program to implement a simple four function calculator import javautilScanner public class Calculator public static void mainString args create an object of Scanner class to read i...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started