-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathQuiz.java
464 lines (373 loc) · 17.4 KB
/
MathQuiz.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class MathQuiz extends JFrame {
JRadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7, radioButton8;
ButtonGroup typeGroup = new ButtonGroup();
ButtonGroup levelGroup = new ButtonGroup();
//labels
JLabel QuestionLabel = new JLabel("Question:");
JLabel answerLabel = new JLabel("Answer:");
JLabel shownLabel = new JLabel("Question will be shown :");
JLabel questionshownLabel = new JLabel("Question:");
JLabel countLabel = new JLabel("Correct count be shown");
JLabel timeLabel = new JLabel("Time spent will be shown");
JLabel selectLabel = new JLabel("Select the interval: ");
JLabel toLabel = new JLabel(" to ");
JLabel correctAnswer = new JLabel("0");
JLabel wrongAnswer = new JLabel("0");
//textfields
JTextField input = new JTextField(12);
JTextField interval1 = new JTextField(4);
JTextField interval2 = new JTextField(4);
//jbuttons
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
//variables
int num1, num2, operator, interval;
int correctCount, wrongCount = 0;
double result;
String question;
long startTime, finishTime, timeSpent;
//invisible panel of selecting intervals
JPanel panel4 = new JPanel();
//New Frame objects
JPanel panel6 = new JPanel();
JPanel panel61 = new JPanel();
JPanel panel62 = new JPanel();
//ours design elements
Font font = new Font("Verdana", Font.BOLD, 50);
Color backgroundOfPanel4 = new Color(73, 175, 205);
Color backgroundOfPanel61 = new Color(64, 173, 95);
Color backgroundOfPanel62 = new Color(212, 42, 42);
//creating new frame
JFrame gameOver = new JFrame();
public MathQuiz() {
//frame layout
setLayout(new BorderLayout());
//creating panels
JPanel panel1 = new JPanel(); // types + levels
JPanel panel11 = new JPanel(); // types
JPanel panel12 = new JPanel(); // levels
JPanel panel2 = new JPanel(); // question + answer + label of input + input + start & stop buttons
JPanel panel21 = new JPanel(); // question + answer
JPanel panel211 = new JPanel(); // question
JPanel panel212 = new JPanel(); // answer
JPanel panel22 = new JPanel(); // label of input + input
JPanel panel23 = new JPanel(); // start & stop button
JPanel panel3 = new JPanel(); // correct count + time spent
JPanel panel31 = new JPanel(); // correct count
JPanel panel32 = new JPanel(); // time spent
//we create panel4 outside of constructor
JPanel panel5 = new JPanel(); //invisble panel4 + panel2
//setting panel4 to invisible
panel4.setVisible(false);
//adding borders to panels
panel11.setBorder(new TitledBorder("Choose a type"));
panel12.setBorder(new TitledBorder("Choose a level"));
panel31.setBorder(new TitledBorder("Correct Count"));
panel32.setBorder(new TitledBorder("Time Spent"));
//designing titles, panel backgrounds and font colors
TitledBorder titledBorder = new TitledBorder("Choose a level"); // the object named "title" is created
titledBorder.setTitleColor(Color.WHITE); // the color is added to the object "title".
panel4.setBorder(titledBorder); // titledborder is set to panel4.
panel4.setBackground(backgroundOfPanel4);
selectLabel.setForeground(Color.white);
toLabel.setForeground(Color.white);
//layouts (the others are flow layout by default)
panel1.setLayout(new GridLayout(1, 2));
panel11.setLayout(new GridLayout(4, 1));
panel12.setLayout(new GridLayout(4, 1));
panel2.setLayout(new GridLayout(3, 1));
panel21.setLayout(new GridLayout(1, 2));
panel211.setLayout(new FlowLayout(FlowLayout.LEFT)); /* Gridlerin içini flow yaptýk. Çünki */
panel212.setLayout(new FlowLayout(FlowLayout.LEFT)); /* label'lar ortalý duruyorlar idi. */
panel3.setLayout(new GridLayout(1, 2));
panel5.setLayout(new BorderLayout());
//defining objects
radioButton1 = new JRadioButton("Add");
radioButton2 = new JRadioButton("Subtract");
radioButton3 = new JRadioButton("Multipy");
radioButton4 = new JRadioButton("Divide");
radioButton5 = new JRadioButton("Starter");
radioButton6 = new JRadioButton("Intermediate");
radioButton7 = new JRadioButton("Advanced");
radioButton8 = new JRadioButton("Select the interval");
//adding objects to group
typeGroup.add(radioButton1);
typeGroup.add(radioButton2);
typeGroup.add(radioButton3);
typeGroup.add(radioButton4);
levelGroup.add(radioButton5);
levelGroup.add(radioButton6);
levelGroup.add(radioButton7);
levelGroup.add(radioButton8);
//adding objects to panel
panel11.add(radioButton1);
panel11.add(radioButton2);
panel11.add(radioButton3);
panel11.add(radioButton4);
panel12.add(radioButton5);
panel12.add(radioButton6);
panel12.add(radioButton7);
panel12.add(radioButton8);
panel211.add(questionshownLabel);
panel212.add(answerLabel);
panel22.add(shownLabel);
panel22.add(input);
panel23.add(startButton);
panel23.add(stopButton);
panel31.add(countLabel);
panel32.add(timeLabel);
panel4.add(selectLabel);
panel4.add(interval1);
panel4.add(toLabel);
panel4.add(interval2);
//adding nested panels
panel1.add(panel11);
panel1.add(panel12);
panel21.add(panel211);
panel21.add(panel212);
panel2.add(panel21);
panel2.add(panel22);
panel2.add(panel23);
panel3.add(panel31);
panel3.add(panel32);
panel5.add(panel4, BorderLayout.NORTH);
panel5.add(panel2, BorderLayout.SOUTH);
//adding panel to frame
add(panel1, BorderLayout.NORTH);
add(panel5, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
//START OF NEW FRAME
//designing new frame (colors, font, etc...)
correctAnswer.setFont(font);
correctAnswer.setForeground(Color.white);
wrongAnswer.setFont(font);
wrongAnswer.setForeground(Color.white);
panel61.setBackground(backgroundOfPanel61);
panel62.setBackground(backgroundOfPanel62);
//titledborders of new frame
TitledBorder titledBorder2 = new TitledBorder("Correct Answer");
titledBorder2.setTitleColor(Color.WHITE);
panel61.setBorder(titledBorder2);
TitledBorder titledBorder3 = new TitledBorder("Wrong Answer");
titledBorder3.setTitleColor(Color.WHITE);
panel62.setBorder(titledBorder3);
//layout of new frame's panel
panel6.setLayout(new GridLayout(1, 2, 10, 10));
//adding objects to panel and panel to frame
panel61.add(correctAnswer);
panel62.add(wrongAnswer);
panel6.add(panel61);
panel6.add(panel62);
gameOver.add(panel6);
//frame settings
gameOver.setTitle("Game Over");
gameOver.setVisible(false);
gameOver.setSize(270, 150);
gameOver.setLocationRelativeTo(null);
//END OF NEW FRAME
//Register a listener for the radio buttons.
radioButton1.addActionListener(new addListener());
radioButton2.addActionListener(new subListener());
radioButton3.addActionListener(new multipyListener());
radioButton4.addActionListener(new divideListener());
radioButton5.addActionListener(new starterListener());
radioButton6.addActionListener(new intermediateListener());
radioButton7.addActionListener(new advancedListener());
radioButton8.addActionListener(new selectListener());
input.addKeyListener(new enterListener());
startButton.addActionListener(new startListener());
stopButton.addActionListener(new stopListener());
}
class starterListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
num1 = (int)(Math.random() * 6); // generating number 0 - 5
num2 = (int)(Math.random() * 6);
interval = 1; // for generating random numbers after first progress is completed
}
}
class intermediateListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
num1 = (int)(Math.random() * 7 + 3); // generating number 3 - 10
num2 = (int)(Math.random() * 7 + 3);
interval = 2; // for generating random numbers after first progress is completed
}
}
class advancedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
num1 = (int)(Math.random() * 21); // generating number 0 - 20
num2 = (int)(Math.random() * 21);
interval = 3; // for generating random numbers after first progress is completed
}
}
class selectListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel4.setVisible(true); // setting panel4 to visible for getting intervals from user
interval = 4; // for generating random numbers after first progress is completed
}
}
class addListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
operator = 1; // for generating formula to calculate correct answer
}
}
class subListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
operator = 2; // for generating formula to calculate correct answer
}
}
class multipyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
operator = 3; // for generating formula to calculate correct answer
}
}
class divideListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
operator = 4; // for generating formula to calculate correct answer
}
}
class startListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (interval == 4) {
// getting intervals from user
int doubleOfInterval1 = Integer.parseInt(interval1.getText());
int doubleOfInterval2 = Integer.parseInt(interval2.getText());
// generating numbers between intervals
num1 = (int)(Math.random() * (doubleOfInterval2 - doubleOfInterval1 + 1) + doubleOfInterval1);
num2 = (int)(Math.random() * (doubleOfInterval2 - doubleOfInterval1 + 1) + doubleOfInterval1);
// changing label text by intervals
radioButton8.setText(doubleOfInterval1 + " to " + doubleOfInterval2);
// hiding panel4 after pressing start button
panel4.setVisible(false);
}
// if the user selects no interval for calculation
if (interval != 4 && interval != 3 && interval != 2 && interval != 1) {
num1 = (int)(Math.random() * 100);
num2 = (int)(Math.random() * 100);
}
// generating formula
if (operator == 1) {
result = num1 + num2; //formula
question = ("" + num1 + "+" + num2); //question label content
} else if (operator == 2) {
result = num1 - num2; //formula
question = ("" + num1 + "-" + num2); //question label content
} else if (operator == 3) {
result = num1 * num2; //formula
question = ("" + num1 + "*" + num2); //question label content
} else if (operator == 4) {
result = num1 / num2; //formula
question = ("" + num1 + "/" + num2); //question label content
} else {
result = num1 + num2; //formula
question = ("" + num1 + "+" + num2); //question label content
}
shownLabel.setText(question); // changing question label
startTime = System.currentTimeMillis(); // starting to get time after pressing start button
// clearing counts after re-play
correctCount = 0;
wrongCount = 0;
countLabel.setText("Correct count be shown");
timeLabel.setText("Time spent will be shown");
}
}
//we must use keylistener because of reading action of enter from keyboard
class enterListener implements KeyListener {
public void keyPressed(KeyEvent e) {
// checking enter action
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
// checking answer is correct or wrong
if (doubleOfInput == result) {
correctCount++; // increasing counter
String stringcount = Integer.toString(correctCount); // getting integer to string
countLabel.setText(stringcount); // setting string to label
} else {
wrongCount+=1; // increasing wrong counter
}
input.setText(""); // clearing input after user entered answer
// re-generating numbers after user entered answer with checking level (same as before)
if (interval == 1) {
num1 = (int)(Math.random() * 6);
num2 = (int)(Math.random() * 6);
} else if (interval == 2) {
num1 = (int)(Math.random() * 7 + 3);
num2 = (int)(Math.random() * 7 + 3);
} else if (interval == 3) {
num1 = (int)(Math.random() * 21);
num2 = (int)(Math.random() * 21);
} else if (interval == 4) {
int doubleOfInterval1 = Integer.parseInt(interval1.getText());
int doubleOfInterval2 = Integer.parseInt(interval2.getText());
num1 = (int)(Math.random() * (doubleOfInterval2 - doubleOfInterval1 + 1) + doubleOfInterval1);
num2 = (int)(Math.random() * (doubleOfInterval2 - doubleOfInterval1 + 1) + doubleOfInterval1);
} else {
num1 = (int)(Math.random() * 100);
num2 = (int)(Math.random() * 100);
}
//re-generating formula
if (operator == 1) {
result = num1 + num2;
question = ("" + num1 + "+" + num2);
} else if (operator == 2) {
result = num1 - num2;
question = ("" + num1 + "-" + num2);
} else if (operator == 3) {
result = num1 * num2;
question = ("" + num1 + "*" + num2);
} else if (operator == 4) {
result = num1 / num2;
question = ("" + num1 + "/" + num2);
} else {
result = num1 + num2;
question = ("" + num1 + "+" + num2);
}
shownLabel.setText(question); //setting new question
}
}
// implementation
//other method1
public void keyReleased(KeyEvent e) {}
//other method2
public void keyTyped(KeyEvent e) {}
}
//after user press stop button
class stopListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
finishTime = System.currentTimeMillis(); // getting stopped time
timeSpent = finishTime - startTime; // calculating time spent
long realTime = timeSpent / 100; // converting to readible time (because it is miliseconds)
String stringtimeSpent = Long.toString(realTime); // converting float to string
timeLabel.setText(stringtimeSpent + " seconds"); // setting timeLabel
if (interval == 4) // if there is interval
radioButton8.setText("Select the interval"); // cleaning radio button of interval
correctAnswer.setText("" + correctCount); // showing correct for both jframes
wrongAnswer.setText("" + wrongCount); // showing correct for new jframes
input.setText(""); // clearing input after user pressed stop button
shownLabel.setText("Question will be shown :"); // clearing label after user pressed stop button
gameOver.setVisible(true); //showing gameover frame
}
}
public static void main(String[] args) {
final MathQuiz frame = new MathQuiz();
frame.pack();
frame.setTitle("Math Quiz");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}