-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorExp.java
356 lines (308 loc) · 11.8 KB
/
CalculatorExp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package Calculators;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.SwingConstants;
public class CalculatorExp {
// Here we declare the global variables
private JFrame frame;
private JTextField textField;
double numb1;// Declares the first number
double numb2;// Declares the second number
double result;// Declares the calculations result
String calcOperator;// Declares the calculations operator
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorExp window = new CalculatorExp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CalculatorExp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(UIManager.getColor("Button.darkShadow"));
frame.setBounds(100, 100, 237, 374);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("My Calculator");
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(10, 11, 201, 50);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 25));
//-------------------------- Button area-----------------------------
/* ---------------------------------------------------------------
Buttons C, +/-, %, +
--------------------------------------------------------------------*/
JButton btnClear = new JButton("C");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(null);// Clears all the inputed numbers
}
});
btnClear.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnClear.setBounds(10, 72, 50, 50);
frame.getContentPane().add(btnClear);
JButton btnPM = new JButton("+/-");
btnPM.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
double ops = Double.parseDouble(String.valueOf(textField.getText()));
ops = ops * (-1);
textField.setText(String.valueOf(ops));
}
});
btnPM.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 10));
btnPM.setBounds(60, 72, 50, 50);
frame.getContentPane().add(btnPM);
JButton btnPercent = new JButton("%");
btnPercent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
textField.setText("");
calcOperator = "%";
}
});
btnPercent.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 10));
btnPercent.setBounds(110, 72, 50, 50);
frame.getContentPane().add(btnPercent);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
textField.setText("");
calcOperator = "+";
}
});
btnPlus.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnPlus.setBounds(160, 72, 50, 50);
frame.getContentPane().add(btnPlus);
/* ---------------------------------------------------------------
Buttons 7, 8, 9, -
--------------------------------------------------------------------*/
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn7.getText();
textField.setText(EnterNumber);
}
});
btn7.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn7.setBounds(10, 122, 50, 50);
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn8.getText();
textField.setText(EnterNumber);
}
});
btn8.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn8.setBounds(60, 122, 50, 50);
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn9.getText();
textField.setText(EnterNumber);
}
});
btn9.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn9.setBounds(110, 122, 50, 50);
frame.getContentPane().add(btn9);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
textField.setText("");
calcOperator = "-";
}
});
btnSub.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnSub.setBounds(160, 122, 50, 50);
frame.getContentPane().add(btnSub);
/* ---------------------------------------------------------------
Buttons 4, 5, 6, /
--------------------------------------------------------------------*/
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn4.getText();
textField.setText(EnterNumber);
}
});
btn4.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn4.setBounds(10, 172, 50, 50);
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn5.getText();
textField.setText(EnterNumber);
}
});
btn5.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn5.setBounds(60, 172, 50, 50);
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn6.getText();
textField.setText(EnterNumber);
}
});
btn6.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn6.setBounds(110, 172, 50, 50);
frame.getContentPane().add(btn6);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
textField.setText("");
calcOperator = "/";
}
});
btnDiv.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnDiv.setBounds(160, 172, 50, 50);
frame.getContentPane().add(btnDiv);
/* ---------------------------------------------------------------
Buttons 1, 2, 3, *
--------------------------------------------------------------------*/
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn1.getText();
textField.setText(EnterNumber);
}
});
btn1.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn1.setBounds(10, 222, 50, 50);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn2.getText();
textField.setText(EnterNumber);
}
});
btn2.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn2.setBounds(60, 222, 50, 50);
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn3.getText();
textField.setText(EnterNumber);
}
});
btn3.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn3.setBounds(110, 222, 50, 50);
frame.getContentPane().add(btn3);
JButton btnMult = new JButton("*");
btnMult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
textField.setText("");
calcOperator = "*";
}
});
btnMult.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnMult.setBounds(160, 222, 50, 50);
frame.getContentPane().add(btnMult);
/* ---------------------------------------------------------------
Buttons EXP, 0, ., =
--------------------------------------------------------------------*/
JButton btnExp = new JButton("X\u00B2");
btnExp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
numb1 = Double.parseDouble(textField.getText());
result = numb1 * numb1;// Returns the multiplication of the two numbers on the screen
String answer = String.format("%.2f", result);
textField.setText(answer);
}
});
btnExp.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 15));
btnExp.setBounds(10, 272, 50, 50);
frame.getContentPane().add(btnExp);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterNumber = textField.getText() + btn0.getText();
textField.setText(EnterNumber);
}
});
btn0.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btn0.setBounds(60, 272, 50, 50);
frame.getContentPane().add(btn0);
JButton btnDot = new JButton(".");
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterDot = textField.getText() + btnDot.getText();
textField.setText(EnterDot);
}
});
btnDot.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnDot.setBounds(110, 272, 50, 50);
frame.getContentPane().add(btnDot);
//------------------- EQUALS ------------------------------------
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String answer;
numb2 = Double.parseDouble(textField.getText());
switch (calcOperator) {
case "+":
result = numb1 + numb2;// Returns the sum of the two numbers on the screen
answer = String.format("%.2f", result);// This line formats the result answer into a float result with two decimal numbers
textField.setText(answer);
break;
case "-":
result = numb1 - numb2;// Returns the subtraction of the two numbers on the screen
answer = String.format("%.2f", result);
textField.setText(answer);
break;
case "/":
result = numb1 / numb2;// Returns the division of the two numbers on the screen
answer = String.format("%.2f", result);
textField.setText(answer);
break;
case "*":
result = numb1 * numb2;// Returns the multiplication of the two numbers on the screen
answer = String.format("%.2f", result);
textField.setText(answer);
break;
case "%":
result = (numb1 * numb2) / 100;// Returns the percentage of the two numbers on the screen
answer = String.format("%.2f", result);
textField.setText(answer);
break;
}
}
});
btnEqual.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
btnEqual.setBounds(160, 272, 50, 50);
frame.getContentPane().add(btnEqual);
}
}