-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
160 lines (138 loc) · 2.51 KB
/
Copy pathCalculator.java
File metadata and controls
160 lines (138 loc) · 2.51 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
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
package guiPrograms;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class calc extends JFrame implements ActionListener
{
JButton b[]=new JButton[16];
JTextField l;
calc()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
b[0]=new JButton("1");
b[1]=new JButton("2");
b[2]=new JButton("3");
b[3]=new JButton("/");
b[4]=new JButton("4");
b[5]=new JButton("5");
b[6]=new JButton("6");
b[7]=new JButton("x");
b[8]=new JButton("7");
b[9]=new JButton("8");
b[10]=new JButton("9");
b[11]=new JButton("-");
b[12]=new JButton("0");
b[13]=new JButton("C");
b[14]=new JButton("=");
b[15]=new JButton("+");
l=new JTextField();
l.setHorizontalAlignment(JTextField.CENTER);
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(4,4));
for(int i=0;i<16;i++)
{
b[i].addActionListener(this);
p2.add(b[i]);
}
setTitle("Calculator");
add(l,BorderLayout.NORTH);
l.setPreferredSize(new Dimension(100,40));
add(p2);
setSize(250,280);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
String text="";
int no1, no2;
char op;
public void actionPerformed(ActionEvent ae)
{
JButton b=(JButton)ae.getSource();
String c=b.getText();
if(text=="Math ERROR"||text=="Syntax ERROR")
clear();
if(c=="C")
clear();
else if(c=="=")
getResult();
else
{
text=text+c;
l.setText(text);
}
}
void getResult()
{
text=l.getText();
boolean opf=false;
try
{
int l=text.length();
String temp="";
for(int i=0;i<l;i++)
{
char c;
c=text.charAt(i);
if(c=='+'||c=='-'||c=='x'||c=='/')
{
op=c;
no1=Integer.parseInt(temp);
temp="";
opf=true;
}
else
temp=temp+c;
}
no2=Integer.parseInt(temp);
if(opf)
evaluate();
}
catch(ArithmeticException e)
{
System.out.println(e);
text="Math ERROR";
l.setText(text);
}
catch(Exception e)
{
System.out.println(e);
text="Syntax ERROR";
l.setText(text);
}
}
void evaluate()
{
int result;
if(op=='+')
result=no1+no2;
else if(op=='-')
result=no1-no2;
else if(op=='x')
result=no1*no2;
else
result=no1/no2;
System.out.println(no1+""+op+""+no2+"="+result);
text=String.valueOf(result);
l.setText(text);
}
void clear()
{
text="";
l.setText(text);
}
}
public class Calculator
{
public static void main(String args[])
{
new calc();
}
}