-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
283 lines (201 loc) · 7.88 KB
/
Main.java
File metadata and controls
283 lines (201 loc) · 7.88 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Main implements ActionListener {
JFrame frame;
JTextField textField;
JButton[] numberButton = new JButton[10];
JButton[] functionButton = new JButton[13];
JButton addButton, subButton, mulButton, divButton;
JButton decButton, equButton, backButton, clrButton, negButton;
JButton expButton, factButton, modButton, squButton;
JPanel panel;
Font myFont = new Font("Ink Free", Font.BOLD, 30);
double num1 = 0, num2 = 0, result = 0;
char operator;
Main() {
frame = new JFrame("Krodin's Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLayout(null);
frame.setBackground(Color.BLACK);
textField = new JTextField();
textField.setBounds(50, 25, 500, 50);
textField.setFont(myFont);
textField.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
backButton = new JButton("Backspace");
clrButton = new JButton("Clear");
negButton = new JButton("Negative");
expButton = new JButton("Exponent");
factButton = new JButton("Factorial");
modButton = new JButton("Modulus");
squButton = new JButton("Square Root");
functionButton[0] = addButton;
functionButton[1] = subButton;
functionButton[2] = mulButton;
functionButton[3] = divButton;
functionButton[4] = decButton;
functionButton[5] = equButton;
functionButton[6] = backButton;
functionButton[7] = clrButton;
functionButton[8] = negButton;
functionButton[9] = expButton;
functionButton[10] = factButton;
functionButton[11] = modButton;
functionButton[12] = squButton;
for (int i = 0; i < 13; i++) {
functionButton[i].addActionListener(this);
functionButton[i].setFont(myFont);
functionButton[i].setFocusable(false);// sometimes made a line around button
// when clicked so this makes
// invisable
} // end of for loop function buttons
for (int i = 0; i < 10; i++) {
numberButton[i] = new JButton(String.valueOf(i));
numberButton[i].addActionListener(this);
numberButton[i].setFont(myFont);
numberButton[i].setFocusable(false);
} // end of for loop for number buttons
negButton.setBounds(50, 415, 163, 50);
backButton.setBounds(216, 415, 175, 50);
clrButton.setBounds(395, 415, 165, 50);
expButton.setBounds(360, 101, 200, 67);
factButton.setBounds(360, 178, 200, 67);
modButton.setBounds(360, 255, 200, 67);
squButton.setBounds(360, 332, 200, 67);
panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.GRAY);// color demonstrates where the grid is
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(addButton);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(subButton);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButton[0]);
panel.add(equButton);
panel.add(divButton);
frame.add(panel);
frame.add(negButton);
frame.add(backButton);
frame.add(clrButton);
frame.add(expButton);
frame.add(factButton);
frame.add(modButton);
frame.add(squButton);
frame.add(textField);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}// end of constructor
public static void main(String[] args) {
Main calculator = new Main();
}// end of main
static double factorial(double n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}// function to find factorial of given number
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButton[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
} // end of if number button actioned
} // end of for loop for number buttons
if (e.getSource() == decButton) {
textField.setText(textField.getText().concat(String.valueOf(".")));
} // end of decimal button
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
} // end of addition button
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
} // end of subtraction button
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
} // end of multiplication button
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
} // end of division button
if (e.getSource() == expButton) {
num1 = Double.parseDouble(textField.getText());
operator = '^';
textField.setText("");
} // end of exponent button
if (e.getSource() == factButton) {
num1 = Double.parseDouble(textField.getText());
result = factorial(num1);
} // end of factorial button
if (e.getSource() == modButton) {
num1 = Double.parseDouble(textField.getText());
operator = 'm';
textField.setText("");
}
if (e.getSource() == squButton) {
num1 = Double.parseDouble(textField.getText());
result = Math.sqrt(num1);
} // end of modulus button
if (e.getSource() == equButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
case '^':
result = Math.pow(num1, num2);
break;
case 'm':
result = num1 % num2;
break;
}// end of switch
textField.setText(String.valueOf(result));
num1 = result;
} // end of equal button switch statement
if (e.getSource() == clrButton) {
textField.setText("");
} // end of clear button
if (e.getSource() == backButton) {
String string = textField.getText();
textField.setText("");
for (int i = 0; i < string.length() - 1; i++) {
textField.setText(textField.getText() + string.charAt(i));
}
} // end of back button
if (e.getSource() == negButton) {
double temp = Double.parseDouble(textField.getText());
temp *= -1;
textField.setText(String.valueOf(temp));
} // end of negative button
}// end of action performed
}// end of class