-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
114 lines (96 loc) · 3.83 KB
/
Copy pathMain.java
File metadata and controls
114 lines (96 loc) · 3.83 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Addition {
double calculate(double num1, double num2) {
return num1 + num2;
}
}
class Subtraction {
double calculate(double num1, double num2) {
return num1 - num2;
}
}
class Multiplication {
double calculate(double num1, double num2) {
return num1 * num2;
}
}
class Division {
double calculate(double num1, double num2) {
return num1 / num2;
}
}
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple Calculator");
frame.setSize(350, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JTextField display = new JTextField();
display.setFont(new Font("Arial", Font.BOLD, 28));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
frame.add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 10, 10));
String[] buttons = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"C", "0", "=", "/"
};
final double[] firstNumber = new double[1];
final String[] operator = new String[1];
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 24));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = button.getText();
if (value.matches("[0-9]")) {
display.setText(display.getText() + value);
}
else if (value.equals("+") || value.equals("-") || value.equals("*") || value.equals("/")) {
firstNumber[0] = Double.parseDouble(display.getText());
operator[0] = value;
display.setText("");
}
else if (value.equals("=")) {
double secondNumber = Double.parseDouble(display.getText());
double result = 0;
if (operator[0].equals("+")) {
Addition add = new Addition();
result = add.calculate(firstNumber[0], secondNumber);
}
else if (operator[0].equals("-")) {
Subtraction subtract = new Subtraction();
result = subtract.calculate(firstNumber[0], secondNumber);
}
else if (operator[0].equals("*")) {
Multiplication multiply = new Multiplication();
result = multiply.calculate(firstNumber[0], secondNumber);
}
else if (operator[0].equals("/")) {
if (secondNumber == 0) {
display.setText("Error");
return;
}
Division divide = new Division();
result = divide.calculate(firstNumber[0], secondNumber);
}
display.setText(String.valueOf(result));
}
else if (value.equals("C")) {
display.setText("");
firstNumber[0] = 0;
operator[0] = "";
}
}
});
panel.add(button);
}
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
}