-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.java
194 lines (146 loc) · 3.57 KB
/
calc.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
import java.awt.*;
import java.awt.event.*;
class calc extends Frame implements ActionListener
{
TextField t1;
double num1;
double num2;
double result;
String op;
int c=0;
int r=0;
calc(String s)
{
super(s);
setLayout(new BorderLayout());
t1=new TextField();
add(t1,BorderLayout.NORTH);
//PANEL FIRST
Panel p1 = new Panel();
p1.setLayout(new GridLayout(4,1));
Button b1 = new Button("+");
Button b2 = new Button("-");
Button b3 = new Button("*");
Button b4 = new Button("/");
p1.add(b1);
b1.addActionListener(this);
p1.add(b2);
b2.addActionListener(this);
p1.add(b3);
b3.addActionListener(this);
p1.add(b4);
b4.addActionListener(this);
add(p1,BorderLayout.EAST);
//PANEL SECOND
Panel p2 = new Panel();
p2.setLayout(new GridLayout(4,1));
Button b5 = new Button("OFF");
Button b6 = new Button("SQRT");
Button b7 = new Button("CE");
Button b8 = new Button("00");
p2.add(b5);
b5.addActionListener(this);
p2.add(b6);
b6.addActionListener(this);
p2.add(b7);
b7.addActionListener(this);
p2.add(b8);
b8.addActionListener(this);
add(p2,BorderLayout.WEST);
//PANEL THIRD
Panel p3 = new Panel();
p3.setLayout(new GridLayout(3,3));
for(int i=1;i<=9;i++)
{
Button b = new Button(""+i);
p3.add(b);
b.addActionListener(this);
}
add(p3,BorderLayout.CENTER);
//PANEL FOURTH
Panel p4 = new Panel();
p4.setLayout(new GridLayout(1,3));
Button b9 = new Button("0");
Button b10 = new Button(".");
Button b11 = new Button("=");
p4.add(b9);
b9.addActionListener(this);
p4.add(b10);
b10.addActionListener(this);
p4.add(b11);
b11.addActionListener(this);
add(p4,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae)
{
String msg = ae.getActionCommand();
if(msg.equals("OFF"))
{
System.exit(0);
}
else if(msg.equals("0")||msg.equals("00")||msg.equals("1")||msg.equals("2")||msg.equals("3")||msg.equals("4")||msg.equals("5")||msg.equals("6")||msg.equals("7")||msg.equals("8")||msg.equals("9"))
{
if(r==0)
t1.setText(t1.getText()+msg);
else
{
t1.setText("");
r=0;
}
}
else if(msg.equals("CE"))
{
t1.setText("");
}
else if(msg.equals(".")&&c==0)
{
c=1;
t1.setText(t1.getText()+".");
}
else if(msg.equals("+") || msg.equals("-") || msg.equals("*") || msg.equals("/") )
{
num1=Double.parseDouble(t1.getText());
op=ae.getActionCommand();
t1.setText("");
c=0;
}
else if(msg.equals("="))
{
num2=Double.parseDouble(t1.getText());
t1.setText("");
if(op=="+")
{
result = num1+num2;
}
else if(op=="-")
{
result=num1-num2;
}
else if(op=="*")
{
result=num1*num2;
}
else if(op=="/")
{
result=num1/num2;
}
t1.setText(""+result);
t1.setEditable(false);
r=1;
}
else if(msg.equals("SQRT"))
{
num1=Double.parseDouble(t1.getText());
result=Math.sqrt(num1);
t1.setText(""+result);
t1.setEditable(false);
}
}
public static void main(String args[])
{
calc f1= new calc("CALCULATOR");
f1.setVisible(true);
f1.setSize(400,400);
f1.setLocation(100,100);
}
}