-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
109 lines (96 loc) · 2.78 KB
/
Copy pathSimpleCalculator.java
File metadata and controls
109 lines (96 loc) · 2.78 KB
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
import javax.swing.*;
import java.awt.event.*;
public class SimpleCalculator implements ActionListener{
JFrame f;
JTextField tf1;
JButton b1, b2, b3, b4, b5;
String st1,st2,st3,op;
SimpleCalculator(){
f = new JFrame("Simple Calculator");
tf1 = new JTextField();
tf1.setBounds(100,80,180,30);
b1 = new JButton("+");
b1.setBounds(120,150,50,30);
b1.addActionListener(this);
b2 = new JButton("-");
b2.setBounds(200,150,50,30);
b2.addActionListener(this);
b3 = new JButton("*");
b3.setBounds(120,200,50,30);
b3.addActionListener(this);
b4 = new JButton("/");
b4.setBounds(200,200,50,30);
b4.addActionListener(this);
b5 = new JButton("=");
b5.setBounds(150,250,70,30);
b5.addActionListener(this);
f.add(tf1);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
st1 = tf1.getText();
tf1.setText("");
op = "+";
}
else if(e.getSource() == b2){
st1 = tf1.getText();
tf1.setText("");
op = "-";
}
else if(e.getSource() == b3){
st1 = tf1.getText();
tf1.setText("");
op = "*";
}
else if(e.getSource() == b4){
st1 = tf1.getText();
tf1.setText("");
op = "/";
}
if(e.getSource()==b5){
st2 = tf1.getText();
int a = Integer.parseInt(st1);
int b = Integer.parseInt(st2);
float c=0;
switch(op){
case "+":
c=a+b;
st3 = String.valueOf(c);
tf1.setText(st3);
break;
case "-":
c=a-b;
st3 = String.valueOf(c);
tf1.setText(st3);
break;
case "*":
c=a*b;
st3 = String.valueOf(c);
tf1.setText(st3);
break;
case "/":
if(b!=0)
{
c=(float)a/b;
st3 = String.valueOf(c);
}
else
st3="Not Defined";
tf1.setText(st3);
break;
}
System.out.println(a + " " + op + " " + b + " = " + c);
}
}
public static void main(String args[]){
new SimpleCalculator();
}
}