-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
175 lines (160 loc) · 5.75 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Jason Lee
Jsnlee At Ucdavis.edu
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
r
public class Calculator implements ActionListener
{
static JButton[] digitButton = new JButton[10];
static JButton[] temp = new JButton[18];
static JButton addButton, subButton, multiplyButton, divideButton, equalButton, clearButton, clearEverything, negativeButton;
static JTextField textField;
static double operationOne, operationTwo, result;
static char operator;
static boolean newNumber;
static void addDigit(int i) {
if (newNumber) {
textField.setText(""+i);
newNumber = false;
}
else {
textField.setText(textField.getText() + i);
}
}
// To control actions
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 10; i++) {
if(event.getSource() == digitButton[i]) {
addDigit(i);
return;
}
}
// To Add Button
if(event.getSource() == addButton) {
operationOne = Double.parseDouble(textField.getText());
newNumber = true;
operator = '+';
return;
}
// To Subtract Button
if(event.getSource() == subButton) {
operationOne = Double.parseDouble(textField.getText());
newNumber = true;
operator = '-';
return;
}
// To Multiply Button
if(event.getSource() == multiplyButton) {
operationOne = Double.parseDouble(textField.getText());
newNumber = true;
operator = '*';
return;
}
// To Divide Button
if(event.getSource() == divideButton) {
operationOne = Double.parseDouble(textField.getText());
newNumber = true;
operator = '/';
return;
}
// To Clear Button
if(event.getSource() == clearButton) {
operationOne = 0;
textField.setText(" "+result);
newNumber = true;
return;
}
// To Clear Everything
if(event.getSource() == clearEverything) {
operationTwo = 0;
operationOne = 0;
result = 0;
textField.setText(" "+result);
newNumber = true;
return;
}
// To Negative Button
if(event.getSource() == negativeButton) {
if(operationTwo != 0) {
operationTwo = operationTwo * -1;
}
if(operationOne != 0) {
operationOne = operationOne * -1;
}
if(result != 0) {
result = result * -1;
}
textField.setText(" "+ result);
newNumber = true;
return;
}
// To Equal Button
if (event.getSource() == equalButton) {
operationTwo = Double.parseDouble(textField.getText());
switch (operator) {
case '/': result = operationOne / operationTwo; break;
case '*': result = operationOne * operationTwo; break;
case '-': result = operationOne - operationTwo; break;
case '+': result = operationOne + operationTwo; break;
}
textField.setText(" "+result);
newNumber = true;
return;
}
}
public static void main(String[] args) {
//initialize the calculator
JFrame frm = new JFrame(" Jason Lee ");
ActionListener AL = new Calculator();
Container contentPane = frm.getContentPane();
contentPane.setLayout(new FlowLayout());
frm.pack();
//set the sizing
frm.setSize(310,240);
contentPane.add(new JLabel("Calculator",JLabel.CENTER));
contentPane.add(textField = new JTextField("",25));
textField.setHorizontalAlignment(JTextField.RIGHT);
JPanel C = new JPanel(new GridLayout(4,4,2,2));
String digits[] = {"C", "CE", " +/- ", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", ".", " = "};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
for (int i=0; i<18; i++) {
C.add(temp[i] = new JButton(digits[i]));
temp[i].addActionListener(AL);
}
//create digit buttons
digitButton[1]= temp[12]; // 1
digitButton[2]= temp[13]; // 2
digitButton[3]= temp[14]; // 3
digitButton[4]= temp[8]; // 4
digitButton[5]= temp[9]; // 5
digitButton[6]= temp[10]; // 6
digitButton[7]= temp[4]; // 7
digitButton[8]= temp[5]; // 8
digitButton[9]= temp[6]; // 9
//create buttons
divideButton = temp[3]; // /
addButton = temp[15]; // +
subButton = temp[11]; // -
equalButton = temp[17]; // =
multiplyButton = temp[7]; // *
clearButton = temp[0]; // Clear
clearEverything = temp[1]; // Clear Everthing
negativeButton = temp[2]; // +/-
//create layout using jpanel
JPanel D = new JPanel(new GridLayout(1,2,2,2));
D.add(digitButton[0] = new JButton(" 0 "));
digitButton[0].addActionListener(AL);
JPanel E = new JPanel(new GridLayout(1,2,2,2));
E.add(temp[16]);
E.add(temp[17]);
//create layout using jpanel
contentPane.add(C);
contentPane.add(D);
D.add(E);
// closing the calcuatlor with the exit
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}