-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculator.java
More file actions
121 lines (109 loc) · 3.99 KB
/
Copy pathBasicCalculator.java
File metadata and controls
121 lines (109 loc) · 3.99 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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class BasicCalculator extends JFrame implements ActionListener
{
private final JTextField display;
private String s0, s1, s2;
private final DecimalFormat df;
BasicCalculator()
{
s0 = s1 = s2 = "";
df = new DecimalFormat("#.##");
setTitle("Basic Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
display = new JTextField(30);
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 50));
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.DARK_GRAY);
String buttons[] = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "+", "="};
Color buttonColors[] = {Color.WHITE, new Color(255, 255, 204)};
int colorIndex = 0;
for (String text : buttons)
{
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 20));
button.setBackground(buttonColors[colorIndex % 2]);
button.setForeground(Color.BLACK);
button.setFocusPainted(false);
button.addActionListener(this);
panel.add(button);
colorIndex++;
}
JButton clear = new JButton("C");
clear.setFont(new Font("Arial", Font.BOLD, 20));
clear.setBackground(Color.RED);
clear.setForeground(Color.WHITE);
clear.setFocusPainted(false);
clear.addActionListener(this);
add(panel, BorderLayout.CENTER);
clear.setPreferredSize(new Dimension(400, 60));
add(clear, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.equals("."))
{
if (!s1.equals("")) s2 += s;
else s0 += s;
}
else if (s.equals("C"))
{
s0 = s1 = s2 = "";
}
else if (s.equals("="))
{
double result = switch (s1)
{
case "+" -> Double.parseDouble(s0) + Double.parseDouble(s2);
case "-" -> Double.parseDouble(s0) - Double.parseDouble(s2);
case "*" -> Double.parseDouble(s0) * Double.parseDouble(s2);
case "/" -> Double.parseDouble(s0) / Double.parseDouble(s2);
default -> 0;
};
s0 = df.format(result);
s1 = s2 = "";
}
else
{
if (s1.equals("")) s1 = s;
else if (!s2.equals(""))
{
double result = switch (s1)
{
case "+" -> Double.parseDouble(s0) + Double.parseDouble(s2);
case "-" -> Double.parseDouble(s0) - Double.parseDouble(s2);
case "*" -> Double.parseDouble(s0) * Double.parseDouble(s2);
case "/" -> Double.parseDouble(s0) / Double.parseDouble(s2);
default -> 0;
};
s0 = df.format(result);
s1 = s;
s2 = "";
}
}
display.setText(s0 + s1 + s2);
}
public static void main(String[] args)
{
new BasicCalculator();
}
}