Skip to content

Commit 7640902

Browse files
authored
Merge pull request #19 from SidaDan/master
Added "bin" button to show the result as binary
2 parents bd9a2f4 + 04dcb59 commit 7640902

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/simplejavacalculator/UI.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @name Simple Java Calculator
3-
* @package ph.calculator
4-
* @file UI.java
5-
* @author SORIA Pierre-Henry
6-
* @email pierrehs@hotmail.com
3+
* @package ph.calculator
4+
* @file UI.java
5+
* @author SORIA Pierre-Henry
6+
* @email pierrehs@hotmail.com
77
* @link http://github.com/pH-7
88
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
99
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
@@ -15,7 +15,6 @@
1515
* @modemail contact@achinthagunasekara.com
1616
* @modemail kchantza@csd.auth.gr
1717
*/
18-
1918
package simplejavacalculator;
2019

2120
import java.awt.FlowLayout;
@@ -28,16 +27,17 @@
2827
import javax.swing.JTextArea;
2928

3029
public class UI implements ActionListener {
30+
3131
private final JFrame frame;
3232
private final JPanel panel;
3333
private final JTextArea text;
3434
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
3535
butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy,
36-
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs;
36+
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary;
3737
private final Calculator calc;
3838

39-
private final String[] buttonValue = { "0", "1", "2", "3", "4", "5", "6",
40-
"7", "8", "9"};
39+
private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6",
40+
"7", "8", "9"};
4141

4242
public UI() {
4343
frame = new JFrame("Calculator PH");
@@ -67,6 +67,7 @@ public UI() {
6767
butabs = new JButton("abs(x)");
6868

6969
butCancel = new JButton("C");
70+
butBinary = new JButton("Bin");
7071

7172
calc = new Calculator();
7273
}
@@ -100,6 +101,8 @@ public void init() {
100101
panel.add(butlog);
101102
panel.add(butrate);
102103
panel.add(butabs);
104+
panel.add(butabs);
105+
panel.add(butBinary);
103106

104107
panel.add(butEqual);
105108
panel.add(butCancel);
@@ -118,6 +121,7 @@ public void init() {
118121
butlog.addActionListener(this);
119122
butrate.addActionListener(this);
120123
butabs.addActionListener(this);
124+
butBinary.addActionListener(this);
121125

122126
butEqual.addActionListener(this);
123127
butCancel.addActionListener(this);
@@ -168,7 +172,7 @@ public void actionPerformed(ActionEvent e) {
168172

169173
if (source == butOneDevidedBy) {
170174
writer(calc.calculateMono(
171-
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
175+
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
172176
}
173177

174178
if (source == butCos) {
@@ -205,9 +209,21 @@ public void actionPerformed(ActionEvent e) {
205209
writer(calc.reset());
206210
}
207211

212+
if (source == butBinary) {
213+
parsetoBinary();
214+
}
215+
208216
text.selectAll();
209217
}
210218

219+
private void parsetoBinary() {
220+
try {
221+
text.setText("" + Long.toBinaryString(Long.parseLong(text.getText())));
222+
} catch (NumberFormatException ex) {
223+
System.err.println("Error while parse to binary." + ex.toString());
224+
}
225+
}
226+
211227
public Double reader() {
212228
Double num;
213229
String str;

0 commit comments

Comments
 (0)