-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBasicCalculator.java
68 lines (56 loc) · 1.53 KB
/
BasicCalculator.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
// A basic calculator
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet = "BasicCalculator" width = 100 height = 100> </applet> */
public class BasicCalculator extends Applet implements ActionListener
{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1,b2,b3,b4;
public void init()
{
l1 = new Label ("First Number : ");
l2 = new Label ("Second Number : ");
l3 = new Label ("Result : ");
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
b1 = new Button("Add");
b2 = new Button("Sub");
b3 = new Button("Mul");
b4 = new Button("Div");
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(b1); add(b2); add(b3); add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int res = 0;
String s = ae.getActionCommand();
if(s=="Add")
{
res = n1 + n2;
}
if(s=="Sub")
{
res = n1 - n2;
}
if(s=="Mul")
{
res = n1 * n2;
}
if(s=="Div")
{
res = n1 / n2;
}
t3.setText(" "+ res);
}
}