-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp12.java
136 lines (116 loc) · 4.32 KB
/
Exp12.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
// AWT CALCULATOR GUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Calculator implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[8];
JButton addButton, subButton, mulButton, divButton, eqButton, clrButton, delButton, dotButton;
JPanel panel;
double num1 = 0, num2 = 0, result = 0;
char operator;
Calculator() {
frame = new JFrame("Calculator");
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(30, 40, 340, 50);
textField.setFont(new Font("Arial", Font.PLAIN, 24));
textField.setEditable(false);
// Initialize number buttons
for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
// Initialize function buttons
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
eqButton = new JButton("=");
clrButton = new JButton("C");
delButton = new JButton("Del");
dotButton = new JButton(".");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = eqButton;
functionButtons[5] = clrButton;
functionButtons[6] = delButton;
functionButtons[7] = dotButton;
for (int i = 0; i < 8; i++) {
functionButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
}
panel = new JPanel();
panel.setBounds(30, 100, 340, 330);
panel.setLayout(new GridLayout(5, 4, 10, 10));
// Add components to the frame
frame.add(textField);
for (int i = 0; i < 10; i++) {
panel.add(numberButtons[i]);
}
for (int i = 0; i < 8; i++) {
panel.add(functionButtons[i]);
}
frame.add(panel);
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for (int i = 0; i < 10; i++) {
numberButtons[i].addActionListener(this);
}
for (int i = 0; i < 8; i++) {
functionButtons[i].addActionListener(this);
}
}
public static void main(String[] args) {
new Calculator();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ((command.charAt(0) >= '0' && command.charAt(0) <= '9') || command.equals(".")) {
textField.setText(textField.getText() + command);
} else if (command.charAt(0) == 'C') {
textField.setText("");
num1 = num2 = result = 0;
operator = '\0';
} else if (command.charAt(0) == 'D') {
String currentText = textField.getText();
if (!currentText.isEmpty()) {
textField.setText(currentText.substring(0, currentText.length() - 1));
}
} else if (command.charAt(0) == '=') {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
textField.setText("Error");
}
break;
}
textField.setText(String.valueOf(result));
num1 = result;
operator = '\0';
} else {
if (operator == '\0') {
num1 = Double.parseDouble(textField.getText());
operator = command.charAt(0);
textField.setText("");
}
}
}
}