-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment17.java
209 lines (200 loc) · 6.24 KB
/
Experiment17.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
import javax.swing.*;
import java.awt.event.*;
public class Experiment17 implements ActionListener {
String op_str="";
double n=0;
JTextField t = new JFormattedTextField(" ");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("+");
JButton b5 = new JButton("4");
JButton b6 = new JButton("5");
JButton b7 = new JButton("6");
JButton b8 = new JButton("-");
JButton b9 = new JButton("7");
JButton b10 = new JButton("8");
JButton b11 = new JButton("9");
JButton b12 = new JButton("0");
JButton b13 = new JButton("*");
JButton b14 = new JButton("/");
JButton b15 = new JButton("%");
JButton b16 = new JButton("=");
JButton b17 = new JButton(".");
JButton b18 = new JButton("C");
public Experiment17() {
JFrame f = new JFrame();
JPanel p = new JPanel();
f.setSize(720,720);
f.setContentPane(p);
f.setLayout(null);
f.setVisible(true);
p.add(t);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p.add(b17);
p.add(b18);
t.setBounds(100,100,200,30);
b1.setBounds(100,140,50,30);
b2.setBounds(150,140,50,30);
b3.setBounds(200,140,50,30);
b4.setBounds(250,140,50,30);
b5.setBounds(100,170,50,30);
b6.setBounds(150,170,50,30);
b7.setBounds(200,170,50,30);
b8.setBounds(250,170,50,30);
b9.setBounds(100,200,50,30);
b10.setBounds(150,200,50,30);
b11.setBounds(200,200,50,30);
b12.setBounds(250,200,50,30);
b13.setBounds(100,230,50,30);
b14.setBounds(150,230,50,30);
b15.setBounds(200,230,50,30);
b16.setBounds(250,230,50,30);
b17.setBounds(100,260,100,30);
b18.setBounds(200,260,100,30);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
t.setText(t.getText() + "1");
}
else if(e.getSource() == b2) {
t.setText(t.getText()+"2");
}
else if(e.getSource() == b3) {
t.setText(t.getText()+"3");
}
else if(e.getSource() == b4) {
t.setText(t.getText()+" + ");
}
else if(e.getSource() == b5) {
t.setText(t.getText()+"4");
}
else if(e.getSource() == b6) {
t.setText(t.getText()+"5");
}
else if(e.getSource() == b7) {
t.setText(t.getText()+"6");
}
else if(e.getSource() == b8) {
t.setText(t.getText()+" - ");
}
else if(e.getSource() == b9) {
t.setText(t.getText()+"7");
}
else if(e.getSource() == b10) {
t.setText(t.getText()+"8");
}
else if(e.getSource() == b11) {
t.setText(t.getText()+"9");
}
else if(e.getSource() == b12) {
t.setText(t.getText()+"0");
}
else if(e.getSource() == b13) {
t.setText(t.getText()+" * ");
}
else if(e.getSource() == b14) {
t.setText(t.getText()+" / ");
}
else if(e.getSource() == b15) {
t.setText(t.getText()+" % ");
}
else if(e.getSource() == b16) {
op_str = t.getText();
compute();
}
else if(e.getSource() == b17) {
t.setText(t.getText()+".");
}
else if(e.getSource() == b18) {
t.setText("");
op_str = t.getText();
}
}
public void compute() {
String op_arr[] = op_str.trim().split(" ");
double temp;
String op = "n";
for (int i=0 ; i<op_arr.length; i++) {
if(i % 2 != 0) {
op = op_arr[i];
}
else {
if (op.equals("n")) {
n = Double.parseDouble(op_arr[i]);
}
else {
temp = Double.parseDouble(op_arr[i]);
switch (op) {
case "+":
n += temp;
t.setText(""+n);
break;
case "-":
n -= temp;
t.setText(""+n);
break;
case "*":
n *= temp;
t.setText(""+n);
break;
case "/":
try {
if(temp == 0)
throw new ArithmeticException("Div by zero");
else {
n=n/temp;
t.setText(""+n);
}
}
catch (Exception e) {
t.setText(e.getMessage());
}
break;
case "%":
n %= temp;
t.setText(""+n);
break;
}
}
}
}
}
public static void main(String[] args) {
new Experiment17();
}
}