2323
2424public class UI implements ActionListener {
2525
26- private JFrame frame ;
27- private JPanel panel ;
28- private JTextArea text ;
29- private JButton but1 , but2 , but3 , but4 , but5 , but6 , but7 , but8 , but9 , but0 , butAdd , butMinus , butMultiply , butDivide , butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy , butCos , butSin , butTan ;
30- private Double num1 , num2 , result ;
31- private int add = 0 , minus = 0 , multiply = 0 , divide = 0 ;
32-
26+ private JFrame frame ;
27+ private JPanel panel ;
28+ private JTextArea text ;
29+ private JButton but [], butAdd , butMinus , butMultiply , butDivide , butEqual , butCancel , butSquareRoot , butSquare , butOneDevidedBy , butCos , butSin , butTan ;
30+ private Double num1 , num2 , result ;
31+ private int add = 0 , minus = 0 , multiply = 0 , divide = 0 ;
32+ String [] buttonValue = {"0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9" };
33+
3334 public UI () {
3435 frame = new JFrame ("Calculator PH" );
35- frame .setResizable (false );
36+ frame .setResizable (false );
3637 panel = new JPanel (new FlowLayout ());
3738
3839 text = new JTextArea (2 ,25 );
39- but1 = new JButton ("1" );
40- but2 = new JButton ("2" );
41- but3 = new JButton ("3" );
42- but4 = new JButton ("4" );
43- but5 = new JButton ("5" );
44- but6 = new JButton ("6" );
45- but7 = new JButton ("7" );
46- but8 = new JButton ("8" );
47- but9 = new JButton ("9" );
48- but0 = new JButton ("0" );
40+ but = new JButton [10 ];
41+ for (int i = 0 ; i < 10 ; i ++) {
42+ but [i ] = new JButton (String .valueOf (i ));
43+ }
4944
5045 butAdd = new JButton ("+" );
5146 butMinus = new JButton ("-" );
5247 butMultiply = new JButton ("*" );
5348 butDivide = new JButton ("/" );
5449 butEqual = new JButton ("=" );
55- butSquareRoot = new JButton ("√" );
56- butSquare = new JButton ("x*x" );
57- butOneDevidedBy = new JButton ("1/x" );
58- butCos = new JButton ("Cos" );
59- butSin = new JButton ("Sin" );
60- butTan = new JButton ("Tan" );
50+ butSquareRoot = new JButton ("√" );
51+ butSquare = new JButton ("x*x" );
52+ butOneDevidedBy = new JButton ("1/x" );
53+ butCos = new JButton ("Cos" );
54+ butSin = new JButton ("Sin" );
55+ butTan = new JButton ("Tan" );
6156
6257 butCancel = new JButton ("C" );
63-
6458 }
6559
6660 public void init () {
@@ -70,100 +64,48 @@ public void init() {
7064 frame .add (panel );
7165
7266 panel .add (text );
73- panel .add (but1 );
74- panel .add (but2 );
75- panel .add (but3 );
76- panel .add (but4 );
77- panel .add (but5 );
78- panel .add (but6 );
79- panel .add (but7 );
80- panel .add (but8 );
81- panel .add (but9 );
82- panel .add (but0 );
83-
67+ for (int i = 0 ; i < 10 ; i ++) {
68+ panel .add (but [i ]);
69+ but [i ].addActionListener (this );
70+ }
71+
8472 panel .add (butAdd );
8573 panel .add (butMinus );
8674 panel .add (butMultiply );
8775 panel .add (butDivide );
88- panel .add (butSquare );
89- panel .add (butSquareRoot );
90- panel .add (butOneDevidedBy );
91- panel .add (butCos );
92- panel .add (butSin );
93- panel .add (butTan );
94-
95- panel .add (butEqual );
76+ panel .add (butSquare );
77+ panel .add (butSquareRoot );
78+ panel .add (butOneDevidedBy );
79+ panel .add (butCos );
80+ panel .add (butSin );
81+ panel .add (butTan );
82+
83+ panel .add (butEqual );
9684 panel .add (butCancel );
9785
98- but1 .addActionListener (this );
99- but2 .addActionListener (this );
100- but3 .addActionListener (this );
101- but4 .addActionListener (this );
102- but5 .addActionListener (this );
103- but6 .addActionListener (this );
104- but7 .addActionListener (this );
105- but8 .addActionListener (this );
106- but9 .addActionListener (this );
107- but0 .addActionListener (this );
108-
10986 butAdd .addActionListener (this );
11087 butMinus .addActionListener (this );
11188 butMultiply .addActionListener (this );
11289 butDivide .addActionListener (this );
113- butSquare .addActionListener (this );
114- butSquareRoot .addActionListener (this );
115- butOneDevidedBy .addActionListener (this );
116- butCos .addActionListener (this );
117- butSin .addActionListener (this );
118- butTan .addActionListener (this );
90+ butSquare .addActionListener (this );
91+ butSquareRoot .addActionListener (this );
92+ butOneDevidedBy .addActionListener (this );
93+ butCos .addActionListener (this );
94+ butSin .addActionListener (this );
95+ butTan .addActionListener (this );
11996
12097 butEqual .addActionListener (this );
12198 butCancel .addActionListener (this );
122-
12399 }
124100
125101 @ Override
126102 public void actionPerformed (ActionEvent e ) {
127103 Object source = e .getSource ();
128-
129- if (source == but1 ) {
130- text .append ("1" );
131- }
132-
133- if (source == but2 ) {
134- text .append ("2" );
135- }
136104
137- if (source == but3 ) {
138- text .append ("3" );
139- }
140-
141- if (source == but4 ) {
142- text .append ("4" );
143- }
144-
145- if (source == but5 ) {
146- text .append ("5" );
147- }
148-
149- if (source == but6 ) {
150- text .append ("6" );
151- }
152-
153- if (source == but7 ) {
154- text .append ("7" );
155- }
156-
157- if (source == but8 ) {
158- text .append ("8" );
159- }
160-
161- if (source == but9 ) {
162- text .append ("9" );
163- }
164-
165- if (source == but0 ) {
166- text .append ("0" );
105+ for (int i = 0 ; i < 10 ; i ++) {
106+ if (source == but [i ]) {
107+ text .append (buttonValue [i ]);
108+ }
167109 }
168110
169111 if (source == butAdd ) {
@@ -210,40 +152,40 @@ public void actionPerformed(ActionEvent e) {
210152 multiply = 0 ;
211153 divide = 1 ;
212154 }
213-
214- if (source == butSquare ) {
155+
156+ if (source == butSquare ) {
215157 num1 = reader ();
216- result = num1 *num1 ;
158+ result = num1 *num1 ;
217159 text .setText (Double .toString (result ));
218160 }
219-
220- if (source == butSquareRoot ) {
161+
162+ if (source == butSquareRoot ) {
221163 num1 = reader ();
222- result = Math .sqrt (num1 );
164+ result = Math .sqrt (num1 );
223165 text .setText (Double .toString (result ));
224166 }
225-
226- if (source == butOneDevidedBy ) {
167+
168+ if (source == butOneDevidedBy ) {
227169 num1 = reader ();
228- result = 1 /num1 ;
170+ result = 1 /num1 ;
229171 text .setText (Double .toString (result ));
230172 }
231-
232- if (source == butCos ) {
173+
174+ if (source == butCos ) {
233175 num1 = reader ();
234- result = Math .cos (num1 );
176+ result = Math .cos (num1 );
235177 text .setText (Double .toString (result ));
236178 }
237-
238- if (source == butSin ) {
179+
180+ if (source == butSin ) {
239181 num1 = reader ();
240- result = Math .sin (num1 );
182+ result = Math .sin (num1 );
241183 text .setText (Double .toString (result ));
242184 }
243-
244- if (source == butTan ) {
185+
186+ if (source == butTan ) {
245187 num1 = reader ();
246- result = Math .tan (num1 );
188+ result = Math .tan (num1 );
247189 text .setText (Double .toString (result ));
248190 }
249191
@@ -269,15 +211,13 @@ public void actionPerformed(ActionEvent e) {
269211 result = num1 /num2 ;
270212 text .setText (Double .toString (result ));
271213 }
272-
273214 }
274215
275216 if (source == butCancel ) {
276217 num1 = 0.0 ;
277218 num2 = 0.0 ;
278219 text .setText ("" );
279220 }
280-
281221 }
282222
283223 public double reader () {
@@ -288,5 +228,4 @@ public double reader() {
288228
289229 return num ;
290230 }
291-
292231}
0 commit comments