Skip to content

Commit c3f6d9e

Browse files
authored
Merge pull request #8 from xatza/patch-2
Add "x^y", "og10(x)", "x%" new buttons "x^y" button, which calculates x(first number) power of y(second number). "log10(x)", which calculates the logarithm of a number x. "x%" button, which calculates the percentage of the number x.
2 parents 725d047 + f1565bb commit c3f6d9e

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

SimpleJavaCalculator.jar

3.01 KB
Binary file not shown.

src/simplejavacalculator/Calculator.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package simplejavacalculator;
22

3+
import static java.lang.Math.log;
4+
import static java.lang.Math.log10;
5+
import static java.lang.Math.pow;
6+
37
public class Calculator {
48
public enum BiOperatorModes {
5-
normal, add, minus, multiply, divide
9+
normal, add, minus, multiply, divide , xpowerofy
610
}
711

812
public enum MonoOperatorModes {
9-
square, squareRoot, oneDevidedBy, cos, sin, tan
13+
square, squareRoot, oneDevidedBy, cos, sin, tan ,log , rate
1014
}
1115

1216
private Double num1, num2;
@@ -28,6 +32,9 @@ private Double calculateBiImpl() {
2832
if (mode == BiOperatorModes.divide) {
2933
return num1 / num2;
3034
}
35+
if (mode == BiOperatorModes.xpowerofy) {
36+
return pow(num1,num2);
37+
}
3138

3239
// never reach
3340
throw new Error();
@@ -78,6 +85,14 @@ public Double calculateMono(MonoOperatorModes newMode, Double num) {
7885
if (newMode == MonoOperatorModes.tan) {
7986
return Math.tan(num);
8087
}
88+
if (newMode == MonoOperatorModes.log) {
89+
return log10(num);
90+
}
91+
if (newMode == MonoOperatorModes.rate) {
92+
return num / 100;
93+
}
94+
95+
8196

8297
// never reach
8398
throw new Error();

src/simplejavacalculator/UI.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* @create 2012-03-30
1111
*
1212
* @modifiedby Achintha Gunasekara
13+
* @modifiedby Kydon Chantzaridis
1314
* @modweb http://www.achinthagunasekara.com
1415
* @modemail contact@achinthagunasekara.com
16+
* @modemail kchantza@csd.auth.gr
1517
*/
1618

1719
package simplejavacalculator;
@@ -31,7 +33,7 @@ public class UI implements ActionListener {
3133
private final JTextArea text;
3234
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
3335
butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy,
34-
butCos, butSin, butTan;
36+
butCos, butSin, butTan, butxpowerofy, butlog, butrate;
3537
private final Calculator calc;
3638

3739
private final String[] buttonValue = { "0", "1", "2", "3", "4", "5", "6",
@@ -59,6 +61,9 @@ public UI() {
5961
butCos = new JButton("Cos");
6062
butSin = new JButton("Sin");
6163
butTan = new JButton("Tan");
64+
butxpowerofy = new JButton("x^y");
65+
butlog = new JButton("log10(x)");
66+
butrate = new JButton("x%");
6267

6368
butCancel = new JButton("C");
6469

@@ -67,15 +72,17 @@ public UI() {
6772

6873
public void init() {
6974
frame.setVisible(true);
70-
frame.setSize(350, 280);
75+
frame.setSize(330, 300);
7176
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
7277
frame.add(panel);
7378

7479
panel.add(text);
75-
for (int i = 0; i < 10; i++) {
80+
81+
for (int i = 1; i < 10; i++) {
7682
panel.add(but[i]);
7783
but[i].addActionListener(this);
7884
}
85+
panel.add(but[0]);
7986

8087
panel.add(butAdd);
8188
panel.add(butMinus);
@@ -87,6 +94,9 @@ public void init() {
8794
panel.add(butCos);
8895
panel.add(butSin);
8996
panel.add(butTan);
97+
panel.add(butxpowerofy);
98+
panel.add(butlog);
99+
panel.add(butrate);
90100

91101
panel.add(butEqual);
92102
panel.add(butCancel);
@@ -101,6 +111,9 @@ public void init() {
101111
butCos.addActionListener(this);
102112
butSin.addActionListener(this);
103113
butTan.addActionListener(this);
114+
butxpowerofy.addActionListener(this);
115+
butlog.addActionListener(this);
116+
butrate.addActionListener(this);
104117

105118
butEqual.addActionListener(this);
106119
butCancel.addActionListener(this);
@@ -134,6 +147,10 @@ public void actionPerformed(ActionEvent e) {
134147
writer(calc
135148
.calculateBi(Calculator.BiOperatorModes.divide, reader()));
136149
}
150+
if (source == butxpowerofy) {
151+
writer(calc
152+
.calculateBi(Calculator.BiOperatorModes.xpowerofy, reader()));
153+
}
137154

138155
if (source == butSquare) {
139156
writer(calc.calculateMono(Calculator.MonoOperatorModes.square,
@@ -164,6 +181,14 @@ public void actionPerformed(ActionEvent e) {
164181
writer(calc.calculateMono(Calculator.MonoOperatorModes.tan,
165182
reader()));
166183
}
184+
if (source == butlog) {
185+
writer(calc.calculateMono(Calculator.MonoOperatorModes.log,
186+
reader()));
187+
}
188+
if (source == butrate) {
189+
writer(calc.calculateMono(Calculator.MonoOperatorModes.rate,
190+
reader()));
191+
}
167192

168193
if (source == butEqual) {
169194
writer(calc.calculateEqual(reader()));

0 commit comments

Comments
 (0)