-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
112 lines (100 loc) · 3.68 KB
/
SimpleCalculator.java
File metadata and controls
112 lines (100 loc) · 3.68 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleCalculator extends JFrame implements ActionListener {
private JTextField display;
private double firstNumber = 0;
private String operator = "";
private boolean startNewNumber = true;
public SimpleCalculator() {
setTitle("Simple Calculator");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Corrected 'setDefaultCloseOperator'
setLayout(new BorderLayout());
// Display field setup
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 24)); // Fixed missing double quotes around "Arial"
add(display, BorderLayout.NORTH);
// Button panel setup
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
// Button labels
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
// Add buttons to the panel
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 20)); // Fixed typo 'Fon' to 'Font'
button.addActionListener(this);
buttonPanel.add(button);
}
add(buttonPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// Handle number inputs
if ("0123456789".contains(command)) {
if (startNewNumber) {
display.setText(command);
startNewNumber = false;
} else {
display.setText(display.getText() + command);
}
}
// Handle operator inputs
else if ("/*-+".contains(command)) {
firstNumber = Double.parseDouble(display.getText());
operator = command;
startNewNumber = true;
}
// Handle equals input
else if (command.equals("=")) {
try {
double secondNumber = Double.parseDouble(display.getText());
double result = 0;
switch (operator) {
case "/":
if (secondNumber == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
result = firstNumber / secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "+":
result = firstNumber + secondNumber;
break;
}
display.setText(String.valueOf(result));
startNewNumber = true;
} catch (ArithmeticException ex) {
display.setText("Error: " + ex.getMessage());
startNewNumber = true;
} catch (NumberFormatException ex) {
display.setText("Error: Invalid Number");
}
}
// Handle clear input
else if (command.equals("C")) {
display.setText("");
startNewNumber = true;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SimpleCalculator frame = new SimpleCalculator();
frame.setVisible(true);
});
}
}