2828import javax .swing .JTextArea ;
2929
3030public 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