Write a program that reads in an operator and two operands, performs the operati
ID: 3797540 • Letter: W
Question
Write a program that reads in an operator and two operands, performs the operation, and then prints out the result.
Input
The input will be one or more lines of numbers that have been typed in by the user. Each line is to have an operator followed by two operands. The operator can be one of the seven: +, -, *, /, %, power, and average.
Output
The output will have for each line, the first operand followed by a space followed by the operator followed by a space followed by the second operand followed by a space followed by = followed by a space followed by the result.
Each number is to be printed using "%.2e" format.
Sample Input
+ 2 3
power 2.2 3
average 2.2 1.3
Sample Output
2.00e+00 + 3.00e+00 = 5.00e+00
2.20e+00 power 3.00e+00 = 1.06e+01
2.20e+00 average 1.30e+00 = 1.75e+00
USE THE HINT 1. Here use in.next() to read the operator and in.nextDouble() to read a number. 2. After reading the two doubles, use in.nextLine() – so that you discard the rest of the "white spaces". 3. To print two numbers num1 and num2 using %.2e format and printf 4. Use String class's equals method to compare two strings: example if (s.equals("+")) { … }
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class ArithmeticOp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double result;
while(true){
System.out.print("input(q to exit): ");
String op = sc.next();
if("q".equalsIgnoreCase(op))
break;
double opr1 = sc.nextDouble();
double opr2 = sc.nextDouble();
switch(op){
case "+":
result = opr1 + opr2;
System.out.println(String.format("%.2e", opr1)+" + "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "-":
result = opr1 - opr2;
System.out.println(String.format("%.2e", opr1)+" - "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "*":
result = opr1 * opr2;
System.out.println(String.format("%.2e", opr1)+" * "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "/":
result = opr1 / opr2;
System.out.println(String.format("%.2e", opr1)+" / "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "%":
result = opr1 % opr2;
System.out.println(String.format("%.2e", opr1)+" % "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "power":
result = Math.pow(opr1, opr2);
System.out.println(String.format("%.2e", opr1)+" power "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
case "average":
result = (opr1 + opr2)/2;
System.out.println(String.format("%.2e", opr1)+" average "+String.format("%.2e", opr2)+
" = "+String.format("%.2e", result));
break;
}
}
}
}
/*
Sample run:
input(q to exit): + 2 3
2.00e+00 + 3.00e+00 = 5.00e+00
input(q to exit): power 2.2 3
2.20e+00 power 3.00e+00 = 1.06e+01
input(q to exit): average 2.2 1.3
2.20e+00 average 1.30e+00 = 1.75e+00
input(q to exit): q
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.