-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
62 lines (59 loc) · 2.78 KB
/
Calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/** ***********************************************************************************************************************
* Calculator class - Uses the Calculator.computation method to compute, display and return the answer attribute,
* given the parameters operand1, operator, and operand2.
*
* Abstract class implements Execute interface.
*
*
* @return answer the calculated answer to the user entered request
* *************************************************************************************************************************
*/
public abstract class Calculator implements Execute {
/** ***********************************************************************************************************************
* Calculator() - default constructor. * *
* *************************************************************************************************************************
*/
private Calculator() {
} // empty constructor
private static double answer = 0;
/** ***********************************************************************************************************************
* computation method - Receives operand1, operator, operand2 parameters and computes and returns the answer attribute.
*
* @param double operand1, String operator, double operand2
* @return answer the calculated answer to the user entered request
* *************************************************************************************************************************
*/
public static double computation(double operand1, String operator, double operand2) {
switch (operator) {
case "+":
answer = operand1 + operand2;
break;
case "-":
answer = operand1 - operand2;
break;
case "*":
answer = operand1 * operand2;
break;
case "/":
answer = operand1 / operand2;
break;
case "^" :
answer = Math.pow(operand1,operand2);
break;
case "sqrt" :
answer = Math.sqrt(operand1);
break;
case "%" :
answer = operand1 % operand2;
break;
case "log" :
answer = Math.log(operand1);
break;
default :
String msg = "Calculator.computation: Usage Error, please choose an operator from the following list: +, -, *, /, sqrt, ^, %, log";
System.out.println(msg);
}
System.out.println("Calculated answer --> " + answer);
return answer;
}
}