-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.java
109 lines (81 loc) · 2.04 KB
/
arithmetic.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import javax.swing.JButton;
class arithmetic extends basicValues {
protected arithmetic(){
super(); //Using super keyword to invoke the super class basicValues' constructor
}
final void doOperation(JButton operator) {
switch(operator.getText()) {
case "+":
result=firstNum+secondNum;
break;
case "-":
result=firstNum-secondNum;
break;
case "x":
result=firstNum*secondNum;
break;
case "/":
result=firstNum/secondNum;
break;
case "%":
result=firstNum%secondNum;
break;
case "x\u02B8": //for doing x power y operation
result=Math.pow(firstNum,secondNum);
break;
case "log\u2090x": //for doing log a base b operation
result=Math.log10(firstNum)/Math.log10(secondNum);
break;
}
textField.setText(String.valueOf(result));
op=null;
}
void doOperation(String operator) { //Method Overloading
switch(operator) {
case "sqrt":
result=Math.sqrt(firstNum);
break;
case "exp":
result=Math.exp(firstNum);
break;
case "x^2":
result=Math.pow(firstNum,2);
break;
case "x^3":
result=Math.pow(firstNum,3);
break;
case "10^x":
result=Math.pow(10,firstNum);
break;
case "2^x":
result=Math.pow(2,firstNum);
break;
case "log":
result=Math.log10(firstNum);
break;
case "ln":
result=Math.log(firstNum);
break;
case "inverse":
result=1/firstNum;
break;
case "mod":
result=Math.abs(firstNum);
break;
case "random":
result=Math.random();
break;
case "floor":
result=Math.round(firstNum);
break;
case "xfact":
double fact=1;
for(double i=firstNum; i>0; i--) {
fact*=i;
}
result=fact;
break;
}
textField.setText(String.valueOf(result));
}
}