-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
89 lines (82 loc) · 1.9 KB
/
Calculator.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton buttons[];
private String labels[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "AC" };
private double result = 0.0;
private boolean start = true;
private String operator = "=";
public Calculator()
{
display = new JTextField(20);
display.setEditable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
buttons = new JButton[17];
for (int i = 0; i < 17; i++) {
buttons[i] = new JButton(labels[i]);
panel.add(buttons[i]);
buttons[i].addActionListener(this);
}
add(display, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setTitle("Calculator");
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double second = 0.0;
String current = e.getActionCommand();
if (current.equals("0") || current.equals("1") || current.equals("2") || current.equals("3") ||
current.equals("4") || current.equals("5") || current.equals("6") || current.equals("7") ||
current.equals("8") || current.equals("9")) {
if (start) {
display.setText("");
start = false;
}
display.setText(display.getText() + current);
} else if (current.equals("AC")) {
result = 0.0;
display.setText("");
start = true;
} else {
if (start) {
if (current.equals("-")) {
display.setText(current);
start = false;
} else {
operator = current;
}} else {
try {
second = Double.parseDouble(display.getText());
} catch (NumberFormatException ex) {
System.out.println("Error: " + ex);
}
switch (operator) {
case "+":
result += second;
break;
case "-":
result -= second;
break;
case "*":
result *= second;
break;
case "/":
result /= second;
break;
case "=":
result = second;
break;
}
display.setText("" + result);
operator = current;
start = true;
}}
}
public static void main(String args[]) {
new Calculator();
}
}