Skip to content

Commit fa7c9ab

Browse files
authored
Merge branch 'master' into master
2 parents 631a860 + 2be1bd9 commit fa7c9ab

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/simplejavacalculator/Calculator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* @name Simple Java Calculator
3+
* @package ph.calculator
4+
* @file Main.java
5+
* @author SORIA Pierre-Henry
6+
7+
* @link http://github.com/pH-7
8+
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
9+
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
10+
*/
11+
112
package simplejavacalculator;
213

314
import static java.lang.Double.NaN;
@@ -6,6 +17,7 @@
617
import static java.lang.Math.pow;
718

819
public class Calculator {
20+
921
public enum BiOperatorModes {
1022
normal, add, minus, multiply, divide , xpowerofy
1123
}

src/simplejavacalculator/UI.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828
import javax.swing.JTextArea;
2929

3030
public class UI implements ActionListener {
31+
3132
private final JFrame frame;
3233
private final JPanel panel;
3334
private final JTextArea text;
3435
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
3536
butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy,
36-
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs;
37+
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary;
3738
private final Calculator calc;
3839

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

4243
public UI() {
4344
frame = new JFrame("Calculator PH");
@@ -67,6 +68,7 @@ public UI() {
6768
butabs = new JButton("abs(x)");
6869

6970
butCancel = new JButton("C");
71+
butBinary = new JButton("Bin");
7072

7173
calc = new Calculator();
7274
}
@@ -100,6 +102,8 @@ public void init() {
100102
panel.add(butlog);
101103
panel.add(butrate);
102104
panel.add(butabs);
105+
panel.add(butabs);
106+
panel.add(butBinary);
103107

104108
panel.add(butEqual);
105109
panel.add(butCancel);
@@ -118,6 +122,7 @@ public void init() {
118122
butlog.addActionListener(this);
119123
butrate.addActionListener(this);
120124
butabs.addActionListener(this);
125+
butBinary.addActionListener(this);
121126

122127
butEqual.addActionListener(this);
123128
butCancel.addActionListener(this);
@@ -168,7 +173,7 @@ public void actionPerformed(ActionEvent e) {
168173

169174
if (source == butOneDevidedBy) {
170175
writer(calc.calculateMono(
171-
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
176+
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
172177
}
173178

174179
if (source == butCos) {
@@ -205,9 +210,21 @@ public void actionPerformed(ActionEvent e) {
205210
writer(calc.reset());
206211
}
207212

213+
if (source == butBinary) {
214+
parsetoBinary();
215+
}
216+
208217
text.selectAll();
209218
}
210219

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

0 commit comments

Comments
 (0)