28
28
import javax .swing .JTextArea ;
29
29
30
30
public class UI implements ActionListener {
31
+
31
32
private final JFrame frame ;
32
33
private final JPanel panel ;
33
34
private final JTextArea text ;
34
35
private final JButton but [], butAdd , butMinus , butMultiply , butDivide ,
35
36
butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy ,
36
- butCos , butSin , butTan , butxpowerofy , butlog , butrate , butabs ;
37
+ butCos , butSin , butTan , butxpowerofy , butlog , butrate , butabs , butBinary ;
37
38
private final Calculator calc ;
38
39
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" };
41
42
42
43
public UI () {
43
44
frame = new JFrame ("Calculator PH" );
@@ -67,6 +68,7 @@ public UI() {
67
68
butabs = new JButton ("abs(x)" );
68
69
69
70
butCancel = new JButton ("C" );
71
+ butBinary = new JButton ("Bin" );
70
72
71
73
calc = new Calculator ();
72
74
}
@@ -100,6 +102,8 @@ public void init() {
100
102
panel .add (butlog );
101
103
panel .add (butrate );
102
104
panel .add (butabs );
105
+ panel .add (butabs );
106
+ panel .add (butBinary );
103
107
104
108
panel .add (butEqual );
105
109
panel .add (butCancel );
@@ -118,6 +122,7 @@ public void init() {
118
122
butlog .addActionListener (this );
119
123
butrate .addActionListener (this );
120
124
butabs .addActionListener (this );
125
+ butBinary .addActionListener (this );
121
126
122
127
butEqual .addActionListener (this );
123
128
butCancel .addActionListener (this );
@@ -168,7 +173,7 @@ public void actionPerformed(ActionEvent e) {
168
173
169
174
if (source == butOneDevidedBy ) {
170
175
writer (calc .calculateMono (
171
- Calculator .MonoOperatorModes .oneDevidedBy , reader ()));
176
+ Calculator .MonoOperatorModes .oneDevidedBy , reader ()));
172
177
}
173
178
174
179
if (source == butCos ) {
@@ -205,9 +210,21 @@ public void actionPerformed(ActionEvent e) {
205
210
writer (calc .reset ());
206
211
}
207
212
213
+ if (source == butBinary ) {
214
+ parsetoBinary ();
215
+ }
216
+
208
217
text .selectAll ();
209
218
}
210
219
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
+
211
228
public Double reader () {
212
229
Double num ;
213
230
String str ;
0 commit comments