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)
1313 * @modweb http://www.achinthagunasekara.com
1414 * @modemail contact@achinthagunasekara.com
1515 */
16-
1716package simplejavacalculator ;
1817
1918import java .awt .FlowLayout ;
2625import javax .swing .JTextArea ;
2726
2827public class UI implements ActionListener {
28+
2929 private final JFrame frame ;
3030 private final JPanel panel ;
3131 private final JTextArea text ;
3232 private final JButton but [], butAdd , butMinus , butMultiply , butDivide ,
3333 butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy ,
34- butCos , butSin , butTan ;
34+ butCos , butSin , butTan , butBinary ;
3535 private final Calculator calc ;
3636
37- private final String [] buttonValue = { "0" , "1" , "2" , "3" , "4" , "5" , "6" ,
38- "7" , "8" , "9" };
37+ private final String [] buttonValue = {"0" , "1" , "2" , "3" , "4" , "5" , "6" ,
38+ "7" , "8" , "9" };
3939
4040 public UI () {
4141 frame = new JFrame ("Calculator PH" );
@@ -61,6 +61,7 @@ public UI() {
6161 butTan = new JButton ("Tan" );
6262
6363 butCancel = new JButton ("C" );
64+ butBinary = new JButton ("Bin" );
6465
6566 calc = new Calculator ();
6667 }
@@ -87,6 +88,7 @@ public void init() {
8788 panel .add (butCos );
8889 panel .add (butSin );
8990 panel .add (butTan );
91+ panel .add (butBinary );
9092
9193 panel .add (butEqual );
9294 panel .add (butCancel );
@@ -101,9 +103,9 @@ public void init() {
101103 butCos .addActionListener (this );
102104 butSin .addActionListener (this );
103105 butTan .addActionListener (this );
104-
105106 butEqual .addActionListener (this );
106107 butCancel .addActionListener (this );
108+ butBinary .addActionListener (this );
107109 }
108110
109111 @ Override
@@ -127,22 +129,22 @@ public void actionPerformed(ActionEvent e) {
127129
128130 if (source == butMultiply ) {
129131 writer (calc .calculateBi (Calculator .BiOperatorModes .multiply ,
130- reader ()));
132+ reader ()));
131133 }
132134
133135 if (source == butDivide ) {
134136 writer (calc
135- .calculateBi (Calculator .BiOperatorModes .divide , reader ()));
137+ .calculateBi (Calculator .BiOperatorModes .divide , reader ()));
136138 }
137139
138140 if (source == butSquare ) {
139141 writer (calc .calculateMono (Calculator .MonoOperatorModes .square ,
140- reader ()));
142+ reader ()));
141143 }
142144
143145 if (source == butSquareRoot ) {
144146 writer (calc .calculateMono (Calculator .MonoOperatorModes .squareRoot ,
145- reader ()));
147+ reader ()));
146148 }
147149
148150 if (source == butOneDevidedBy ) {
@@ -152,17 +154,17 @@ public void actionPerformed(ActionEvent e) {
152154
153155 if (source == butCos ) {
154156 writer (calc .calculateMono (Calculator .MonoOperatorModes .cos ,
155- reader ()));
157+ reader ()));
156158 }
157159
158160 if (source == butSin ) {
159161 writer (calc .calculateMono (Calculator .MonoOperatorModes .sin ,
160- reader ()));
162+ reader ()));
161163 }
162164
163165 if (source == butTan ) {
164166 writer (calc .calculateMono (Calculator .MonoOperatorModes .tan ,
165- reader ()));
167+ reader ()));
166168 }
167169
168170 if (source == butEqual ) {
@@ -173,9 +175,21 @@ public void actionPerformed(ActionEvent e) {
173175 writer (calc .reset ());
174176 }
175177
178+ if (source == butBinary ) {
179+ parsetoBinary ();
180+ }
181+
176182 text .selectAll ();
177183 }
178184
185+ private void parsetoBinary () {
186+ try {
187+ text .setText ("" + Long .toBinaryString (Long .parseLong (text .getText ())));
188+ } catch (NumberFormatException ex ) {
189+ System .err .println ("Error while parse to binary." + ex .toString ());
190+ }
191+ }
192+
179193 public Double reader () {
180194 Double num ;
181195 String str ;
0 commit comments