-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
397 lines (378 loc) · 11.4 KB
/
Calculator.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*;===============================================================================
Title: create a GUI scientific calculator(JAVA)
Author: Soham manjrekar , tanya mishra ,prayag mayekar
Date: 18 Nov 2021
;=================================================================================*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GuiCalculator implements ActionListener {
int operator = 0;
double a = 0, b = 0, result = 0;
Operation obj = new Operation();
JLabel Name, SLab;
JButton _1, _2, _3, _4, _5, _6, _7, _8, _9, _0, clear, plus, negative, sin, divide, multi, cos, log, fact, tan, sqrt,
total;
JTextField Result;
JFrame frame;
JPanel panel;
JRadioButton type, type1;
Font text, text1;
ButtonGroup jodouble;
GuiCalculator() {
Result = new JTextField();
_1 = new JButton("1");
_2 = new JButton("2");
_3 = new JButton("3");
_4 = new JButton("4");
_5 = new JButton("5");
_6 = new JButton("6");
_7 = new JButton("7");
_8 = new JButton("8");
_9 = new JButton("9");
_0 = new JButton("0");
cos = new JButton("Cos");
clear = new JButton("Clear");
plus = new JButton("+");
log = new JButton("Log");
sin = new JButton("Sin");
divide = new JButton("/");
multi = new JButton("*");
negative = new JButton("-");
fact = new JButton("n!");
tan = new JButton("Tan");
sqrt = new JButton("sqrt");
total = new JButton("=");
type = new JRadioButton("Radian", true);
type1 = new JRadioButton("Degree");
jodouble = new ButtonGroup();
jodouble.add(type);
jodouble.add(type1);
SLab = new JLabel("");
// setFont
text = new Font("Sansserif", Font.BOLD, 35);
text1 = new Font("Sansserif", Font.BOLD, 18);
_1.setFont(text1);
_2.setFont(text1);
_3.setFont(text1);
_4.setFont(text1);
_5.setFont(text1);
_6.setFont(text1);
_7.setFont(text1);
_8.setFont(text1);
_9.setFont(text1);
_0.setFont(text1);
cos.setFont(text1);
clear.setFont(text1);
plus.setFont(text1);
log.setFont(text1);
// sin.setFont(text1);
divide.setFont(text1);
negative.setFont(text1);
multi.setFont(text1);
tan.setFont(text1);
// sqrt.setFont(text1);
fact.setFont(text1);
total.setFont(text1);
Result.setFont(text);
// color
Result.setBackground(Color.black);
Result.setToolTipText("Enter The Value");
Result.setForeground(Color.orange);
_1.setBackground(Color.yellow);
_2.setBackground(Color.yellow);
_3.setBackground(Color.yellow);
_4.setBackground(Color.yellow);
_5.setBackground(Color.yellow);
_6.setBackground(Color.yellow);
_7.setBackground(Color.yellow);
_8.setBackground(Color.yellow);
_9.setBackground(Color.yellow);
_0.setBackground(Color.yellow);
cos.setBackground(Color.yellow);
clear.setBackground(Color.orange);
plus.setBackground(Color.yellow);
log.setBackground(Color.yellow);
divide.setBackground(Color.yellow);
sin.setBackground(Color.yellow);
multi.setBackground(Color.yellow);
negative.setBackground(Color.yellow);
fact.setBackground(Color.yellow);
tan.setBackground(Color.yellow);
sqrt.setBackground(Color.yellow);
total.setBackground(Color.red);
total.setToolTipText("Click for Toal");
type.setBackground(Color.yellow);
type.setForeground(Color.black);
type1.setBackground(Color.yellow);
type1.setForeground(Color.black);
// setBounds
SLab.setBounds(0, 10, 100, 10);
Result.setBounds(30, 40, 395, 70);
type.setBounds(260, 120, 70, 30);
type1.setBounds(350, 120, 70, 30);
_1.setBounds(30, 160, 50, 40);
_2.setBounds(90, 160, 50, 40);
_3.setBounds(150, 160, 50, 40);
_4.setBounds(30, 210, 50, 40);
_5.setBounds(90, 210, 50, 40);
_6.setBounds(150, 210, 50, 40);
_7.setBounds(30, 260, 50, 40);
_8.setBounds(90, 260, 50, 40);
_9.setBounds(150, 260, 50, 40);
_0.setBounds(30, 310, 110, 40);
clear.setBounds(30, 360, 170, 40);
cos.setBounds(240, 360, 110, 40);
plus.setBounds(150, 310, 50, 40);
log.setBounds(240, 160, 110, 40);
sin.setBounds(360, 160, 60, 40);
divide.setBounds(240, 210, 50, 40);
multi.setBounds(300, 210, 50, 40);
negative.setBounds(240, 260, 50, 40);
fact.setBounds(300, 260, 50, 40);
tan.setBounds(240, 310, 110, 40);
sqrt.setBounds(360, 210, 60, 40);
total.setBounds(360, 260, 60, 140);
// action
_1.addActionListener(this);
_2.addActionListener(this);
_3.addActionListener(this);
_4.addActionListener(this);
_5.addActionListener(this);
_6.addActionListener(this);
_7.addActionListener(this);
_8.addActionListener(this);
_9.addActionListener(this);
_0.addActionListener(this);
clear.addActionListener(this);
cos.addActionListener(this);
plus.addActionListener(this);
log.addActionListener(this);
sin.addActionListener(this);
divide.addActionListener(this);
multi.addActionListener(this);
negative.addActionListener(this);
fact.addActionListener(this);
tan.addActionListener(this);
sqrt.addActionListener(this);
total.addActionListener(this);
// panel
panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
panel.setBackground(Color.black);
panel.add(Result);
panel.add(_1);
panel.add(_2);
panel.add(_3);
panel.add(_4);
panel.add(_5);
panel.add(_6);
panel.add(_7);
panel.add(_8);
panel.add(_9);
panel.add(_0);
panel.add(clear);
panel.add(cos);
panel.add(plus);
panel.add(log);
panel.add(sin);
panel.add(divide);
panel.add(multi);
panel.add(negative);
panel.add(fact);
panel.add(tan);
panel.add(sqrt);
panel.add(total);
panel.add(type);
panel.add(type1);
panel.add(SLab);
// Frame
frame = new JFrame("Calculator ");
frame.setBounds(600, 300, 460, 470);
frame.setVisible(true);
frame.getContentPane().add(panel);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == _1) {
Result.setText(Result.getText().concat("1"));
} else if (ae.getSource() == _2) {
Result.setText(Result.getText().concat("2"));
} else if (ae.getSource() == _3) {
Result.setText(Result.getText().concat("3"));
} else if (ae.getSource() == _4) {
Result.setText(Result.getText().concat("4"));
} else if (ae.getSource() == _5) {
Result.setText(Result.getText().concat("5"));
} else if (ae.getSource() == _6) {
Result.setText(Result.getText().concat("6"));
} else if (ae.getSource() == _7) {
Result.setText(Result.getText().concat("7"));
} else if (ae.getSource() == _8) {
Result.setText(Result.getText().concat("8"));
} else if (ae.getSource() == _9) {
Result.setText(Result.getText().concat("9"));
} else if (ae.getSource() == _0) {
Result.setText(Result.getText().concat("0"));
} else if (ae.getSource() == plus) {
a = Double.parseDouble(Result.getText());
operator = 1;
Result.setText("");
} else if (ae.getSource() == log) {
a = Double.parseDouble(Result.getText());
result = obj.LogValue(a);
Result.setText("" + result);
} else if (ae.getSource() == sin) {
if (type.isSelected()) {
a = Double.parseDouble(Result.getText());
result = obj.SinValue1(a);
Result.setText("" + result);
}
if (type1.isSelected()) {
result = obj.SinValue(a);
a = Double.parseDouble(Result.getText());
Result.setText("" + result);
}
} else if (ae.getSource() == cos) {
if (type.isSelected()) {
a = Double.parseDouble(Result.getText());
result = obj.CosValue1(a);
Result.setText("" + result);
}
if (type1.isSelected()) {
result = obj.CosValue(a);
a = Double.parseDouble(Result.getText());
Result.setText("" + result);
}
} else if (ae.getSource() == divide) {
a = Double.parseDouble(Result.getText());
operator = 4;
Result.setText("");
} else if (ae.getSource() == multi) {
a = Double.parseDouble(Result.getText());
operator = 5;
Result.setText("");
} else if (ae.getSource() == negative) {
a = Double.parseDouble(Result.getText());
operator = 6;
Result.setText("");
} else if (ae.getSource() == fact) {
a = Double.parseDouble(Result.getText());
result = obj.Factorial(a);
Result.setText("" + result);
} else if (ae.getSource() == tan) {
if (type.isSelected()) {
a = Double.parseDouble(Result.getText());
result = obj.TanValue1(a);
Result.setText("" + result);
}
if (type1.isSelected()) {
result = obj.TanValue(a);
a = Double.parseDouble(Result.getText());
Result.setText("" + result);
}
} else if (ae.getSource() == sqrt) {
a = Double.parseDouble(Result.getText());
result = obj.SquareRoot(a);
Result.setText("" + result);
// operator=9;
// Result.setText("");
} else if (ae.getSource() == total) {
b = Double.parseDouble(Result.getText());
switch (operator) {
case 1:
result = obj.Add(a, b);
break;
case 2:
break;
/*
* case 3: result=obj.SinValue(); break;
*/
case 4:
result = obj.Divide(a, b);
break;
case 5:
result = obj.Multi(a, b);
break;
case 6:
result = obj.Sub(a, b);
break;
case 7:
break;/*
* case 8: result=obj.TanValue(); break; case 9: break;
*/
}
Result.setText("" + result);
} else if (ae.getSource() == clear) {
Result.setText("");
}
}
}
class Operation {
public double Add(double input, double input2) {
double c = input + input2;
return (c);
}
public double Sub(double input, double input2) {
double c = input - input2;
return (c);
}
public double Multi(double input, double input2) {
double c = input * input2;
return (c);
}
public double LogValue(double input) {
double result = (Math.log10(input));
return result;
}
public double Divide(double input, double input2) {
double c = input / input2;
return (c);
}
public double Factorial(double input) {
double factorial = 1;
for (double j = input; j >= 1; j--) {
factorial = factorial * j;
}
return factorial;
}
public double SquareRoot(double input) {
input = (Math.sqrt(input));
return input;
}
public double SinValue(double input) {
double degree = Math.toRadians(input);
input = (Math.sin(degree));
return input;
}
public double SinValue1(double input) {
input = (Math.sin(input));
return input;
}
public double CosValue(double input) {
double degree = Math.toRadians(input);
input = (Math.cos(degree));
return input;
}
public double CosValue1(double input) {
input = (Math.cos(input));
return input;
}
public double TanValue(double input) {
double degree = Math.toRadians(input);
input = (Math.tan(degree));
return input;
}
public double TanValue1(double input) {
double degree = Math.toRadians(input);
input = (Math.tan(degree));
return input;
}
}
public class Calculator {
public static void main(String[] arg) {
new GuiCalculator();
}
}