-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalculator.java
242 lines (233 loc) · 6.44 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
import java.awt.*;
import java.awt.event.*;
class EventHandlingCalc extends Frame implements ActionListener,WindowListener{
String str="0";
String strcpy,answer;
double ans;
char operation;
double num1,num2;
Button button0 = new Button("0");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
Button button5 = new Button("5");
Button button6 = new Button("6");
Button button7 = new Button("7");
Button button8 = new Button("8");
Button button9 = new Button("9");
Button buttonPlus = new Button("+");
Button buttonMinus = new Button("-");
Button buttonMulti = new Button("*");
Button buttonDivi = new Button("/");
Button buttonDot = new Button(".");
Button buttonEqual = new Button("=");
Button buttonc = new Button("C");
Frame f = new Frame();
Panel panel = new Panel();
TextField textField = new TextField();
Font font1 = new Font("Serif", Font.BOLD, 20);
Font font2 = new Font("SansSerif", Font.BOLD, 15);
Label label = new Label();
EventHandlingCalc(){
f.setTitle("Calculator");
f.setVisible(true);
//f.setSize(550,450);
textField.setBounds(10,50,250,50);
textField.setFont(font1);
label.setFont(font2);
label.setBounds(10,110,250,30);
label.setBackground(Color.cyan);
f.setBounds(200,200,270,470);
f.setLayout(null);
f.add(textField);
f.add(label);
f.setBackground(Color.LIGHT_GRAY);
panel.setFont(font1);
panel.setVisible(true);
panel.setBounds(10,150,250,250);
panel.setLayout(new GridLayout(4,4));
buttonc.setBackground(Color.GRAY);
panel.add(buttonc);
panel.add(button1);
panel.add(button2);
panel.add(button3);
buttonPlus.setBackground(Color.GRAY);
panel.add(buttonPlus);
panel.add(button4);
panel.add(button5);
panel.add(button6);
buttonMinus.setBackground(Color.GRAY);
panel.add(buttonMinus);
panel.add(button7);
panel.add(button8);
panel.add(button9);
buttonMulti.setBackground(Color.GRAY);
buttonDivi.setBackground(Color.GRAY);
buttonDot.setBackground(Color.GRAY);
panel.add(buttonMulti);
panel.add(buttonDivi);
panel.add(buttonDot);
panel.add(button0);
f.add(panel);
buttonEqual.setFont(font1);
buttonEqual.setBackground(Color.gray);
buttonEqual.setBounds(100,400,60,60);
f.add(buttonEqual);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonDivi.addActionListener(this);
buttonDot.addActionListener(this);
buttonc.addActionListener(this);
buttonMulti.addActionListener(this);
buttonEqual.addActionListener(this);
f.addWindowListener (this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button0) {
strcpy=textField.getText();
str = strcpy+'0';
textField.setText(str);
}
else if(e.getSource()==button1) {
strcpy=textField.getText();
str = strcpy+'1';
textField.setText(str);
}
else if(e.getSource()==button2) {
strcpy=textField.getText();
str = strcpy+'2';
textField.setText(str);
}
else if(e.getSource()==button3) {
strcpy=textField.getText();
str = strcpy+'3';
textField.setText(str);
}
else if(e.getSource()==button4) {
strcpy=textField.getText();
str = strcpy+'4';
textField.setText(str);
}
else if(e.getSource()==button5) {
strcpy=textField.getText();
str = strcpy+'5';
textField.setText(str);
}
else if(e.getSource()==button6) {
strcpy=textField.getText();
str = strcpy+'6';
textField.setText(str);
}
else if(e.getSource()==button7) {
strcpy=textField.getText();
str = strcpy+'7';
textField.setText(str);
}
else if(e.getSource()==button8) {
strcpy=textField.getText();
str = strcpy+'8';
textField.setText(str);
}
else if(e.getSource()==button9) {
strcpy=textField.getText();
str = strcpy+'9';
textField.setText(str);
}
else if(e.getSource()==buttonDot) {
strcpy=textField.getText();
str = strcpy+'.';
textField.setText(str);
}
else if(e.getSource()==buttonPlus) {
num1 =Double.parseDouble(textField.getText());
str ="";
textField.setText(str);
operation='+';
}
else if(e.getSource()==buttonMinus) {
num1 =Double.parseDouble(textField.getText());
str ="";
textField.setText(str);
operation='-';
}
else if(e.getSource()==buttonMulti) {
num1 =Double.parseDouble(textField.getText());
str ="";
textField.setText(str);
operation='*';
}
else if(e.getSource()==buttonDivi) {
num1 =Double.parseDouble(textField.getText());
str ="";
textField.setText(str);
operation='/';
}
else if(e.getSource()==buttonc) {
num1=0;
num2=0;
str ="";
textField.setText(str);
label.setText("");
}
else if(e.getSource()==buttonEqual) {
num2 =Double.parseDouble(textField.getText());
switch(operation) {
case '+':
ans=num1+num2;
answer = Double.toString(ans);
break;
case '-':
ans=num1-num2;
answer = Double.toString(ans);
break;
case '*':
ans=num1*num2;
answer = Double.toString(ans);
break;
case '/':
try {
ans=num1/num2;
answer = Double.toString(ans);
}
catch(ArithmeticException ae) {
answer=ae.toString();
}
break;
default:
answer = "Invalid Input";
}
textField.setText(answer);
String stri = new String(num1 +" "+operation+" " + num2 +" = "+answer);
label.setText(stri);
operation='/';
}
else {
textField.setText("Invalid Input");
}
}
public void windowClosing (WindowEvent e) {
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
public class Calculator {
public static void main(String []args) {
EventHandlingCalc eventCalc = new EventHandlingCalc();
}
}