-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
145 lines (142 loc) · 3.52 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
import javax.swing.*;
import java.awt.event.*;
class Calculator implements ActionListener
{
static int a=0,b=0,count=0,result=0;
JFrame f;
JTextField t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
Calculator()
{
f=new JFrame("Calculator");
t=new JTextField();
t.setEditable(false);
t.setBounds(75,50,200,30);
b1=new JButton("1");b1.setBounds(100,100,50,50);
b2=new JButton("2");b2.setBounds(150,100,50,50);
b3=new JButton("3");b3.setBounds(200,100,50,50);
b4=new JButton("4");b4.setBounds(100,150,50,50);
b5=new JButton("5");b5.setBounds(150,150,50,50);
b6=new JButton("6");b6.setBounds(200,150,50,50);
b7=new JButton("7");b7.setBounds(100,200,50,50);
b8=new JButton("8");b8.setBounds(150,200,50,50);
b9=new JButton("9");b9.setBounds(200,200,50,50);
b10=new JButton("+");b10.setBounds(100,250,50,50);
b11=new JButton("0");b11.setBounds(150,250,50,50);
b12=new JButton("-");b12.setBounds(200,250,50,50);
b13=new JButton("*");b13.setBounds(100,300,50,50);
b14=new JButton("=");b14.setBounds(150,300,50,50);
b15=new JButton("/");b15.setBounds(200,300,50,50);
b16=new JButton("Clear");b16.setBounds(100,350,150,30);
f.add(t);f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(b9);f.add(b10);f.add(b11);f.add(b12);f.add(b13);f.add(b14);f.add(b15);f.add(b16);
f.setSize(400,450);
f.setLayout(null);
f.setVisible(true);
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);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
t.setText(t.getText().concat("1"));
}
else if(e.getSource()==b2)
{
t.setText(t.getText().concat("2"));
}
else if(e.getSource()==b3)
{
t.setText(t.getText().concat("3"));
}
else if(e.getSource()==b4)
{
t.setText(t.getText().concat("4"));
}
else if(e.getSource()==b5)
{
t.setText(t.getText().concat("5"));
}
else if(e.getSource()==b6)
{
t.setText(t.getText().concat("6"));
}
else if(e.getSource()==b7)
{
t.setText(t.getText().concat("7"));
}
else if(e.getSource()==b8)
{
t.setText(t.getText().concat("8"));
}
else if(e.getSource()==b9)
{
t.setText(t.getText().concat("9"));
}
else if(e.getSource()==b10)
{
a=Integer.parseInt(t.getText());
count=1;
t.setText("");
}
else if(e.getSource()==b11)
{
t.setText(t.getText().concat("0"));
}
else if(e.getSource()==b12)
{
a=Integer.parseInt(t.getText());
count=2;
t.setText("");
} else if(e.getSource()==b13)
{
a=Integer.parseInt(t.getText());
count=3;
t.setText("");
}
else if(e.getSource()==b14)
{
b=Integer.parseInt(t.getText());
try
{
switch(count)
{
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
default:result=0;
}
t.setText(" "+result);
}
catch(ArithmeticException o)
{
t.setText("Math Error");
}
}
else if(e.getSource()==b15)
{
count=4;
a=Integer.parseInt(t.getText());
t.setText("");
}
else if(e.getSource()==b16)
{
t.setText("");
}
}
public static void main(String arg[])
{
new Calculator();
}
}