-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionDialog.java
More file actions
264 lines (242 loc) · 9.35 KB
/
QuestionDialog.java
File metadata and controls
264 lines (242 loc) · 9.35 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
package chess;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QuestionDialog extends JDialog {
//private static final int width = 400;
private static final int width = 500;
private static final int height = 125;
// UI
private static JFrame frame;
private static JComboBox levelBox;
private static JButton newQuestion;
private static JLabel score;
private static JButton reset;
private static int correctCount, totalCount;
// QuestionDialog()
private static int level = 0;
private static JLabel instructions, problem, timeRemaining;
private static JTextField answerField;
private static JButton giveUp;
private static boolean gaveUp;
private static int seconds;
private static int correctAnswer, userAnswer;
private static CountdownTimer timer;
public static void main(String[]args) {
// Create UI
frame = new JFrame("Math Questions Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 200, width, height);
frame.setResizable(false);
levelBox = new JComboBox(new String[] {"Random level",
"Level 1", "Level 2", "Level 3", "Level 4", "Level 5"});
levelBox.setSelectedIndex(level);
levelBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
level = levelBox.getSelectedIndex();
}
});
frame.add(levelBox, BorderLayout.NORTH);
newQuestion = new JButton("New Question");
newQuestion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
QuestionDialog dialog = new QuestionDialog(frame, level);
if(dialog.answerIsCorrect()) {
correctCount++;
}
totalCount++;
score.setText("Correct answers: " + correctCount + "/" + totalCount
+ " (" + (int)((double)correctCount/totalCount*100) + "%)");
}
});
frame.add(newQuestion, BorderLayout.CENTER);
score = new JLabel();
frame.add(score, BorderLayout.SOUTH);
reset();
reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
frame.add(reset, BorderLayout.EAST);
frame.setVisible(true);
}
public static void reset() {
correctCount = 0;
totalCount = 0;
score.setText("Press \"New Question\" to begin");
}
public QuestionDialog(Frame owner, int level) {
super(owner, "Math Question: " + level, true);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setResizable(false);
setSize(width, height);
if (owner != null) {
setLocation(owner.getX() + (owner.getWidth()-width)/2,
owner.getY() + (owner.getHeight()-height)/2);
} else {
setLocation(400, 400);
}
setResizable(false);
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
instructions = new JLabel("Do the following problem from left to right. Leave off any decimals.");
add(instructions);
problem = new JLabel("");
add(problem);
timeRemaining = new JLabel("");
add(timeRemaining);
answerField = new JTextField(12);
answerField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
String answer = answerField.getText();
if (answer.length() == 0) {
gaveUp = true;
} else {
userAnswer = 0;
boolean negative = false;
int power = 0;
for (int i = answer.length() - 1; i >= 0; i--) {
char c = answer.charAt(i);
if (c == '-') {
negative = true;
} else if ('0' <= c && c <= '9') {
userAnswer += (int)(c-48)*(Math.pow(10, power));
power++;
}
}
if (negative) {
userAnswer *= -1;
}
}
showResult();
}
});
answerField.addKeyListener(new KeyAdapter() {
// Makes it so only numbers and '-' can be typed
public void keyReleased(KeyEvent e) {
String text = answerField.getText();
String filtered = "";
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '-' || ('0' <= c && c <= '9')) {
filtered += c;
}
}
answerField.setText(filtered);
}
});
add(answerField);
giveUp = new JButton("Give Up");
giveUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gaveUp = true;
showResult();
}
});
add(giveUp);
// Initialize variables
gaveUp = false;
seconds = 0;
correctAnswer = userAnswer = 0;
// Create arrays of random operations and operands, add time based on operations
int currentLevel = level;
if (currentLevel == 0) { // It has been chosen as "random"
currentLevel = (int)(Math.random()*5 + 1); // 1-5
setTitle("Math Question: " + currentLevel);
}
int difficulty = 3*currentLevel;
seconds = difficulty;
int[] numbers = new int[difficulty];
char[] operations = new char[difficulty];
for (int i = 0; i < operations.length; i++) {
double random1 = Math.random();
double random2 = Math.random();
if (random1 < 0.5) {
if (random2 < 0.5) {
operations[i] = '+';
} else {
operations[i] = '-';
}
numbers[i] = (int)(Math.random()*20) + 1; // 1-20
seconds += 2;
} else {
if (random2 < 0.5) {
operations[i] = 'x';
} else {
operations[i] = 247; // 247 = division symbol
}
numbers[i] = (int)(Math.random()*4) + 2; // 2-5
seconds += 3;
}
}
// Find correct answer
double answer = (int)(Math.random()*10) + 1; // 1-10
String question = "" + (int)answer;
for (int i = 0; i < operations.length; i++) {
char operation = operations[i];
int number = numbers[i];
question += " " + operation + " " + number;
switch (operation) {
case '+': answer += number; break;
case '-': answer -= number; break;
case 'x': answer *= number; break;
case 247: answer /= number; break; // 247 = division symbol
default:
System.out.println("Error in QuestionDialog()");
}
}
correctAnswer = (int)answer;
question += " = ";
// Set text
problem.setText(question);
// Create timer
timer = new CountdownTimer(10, "You have: ", " seconds remaining.",
0, 0, seconds, 0.0,
new Runnable() { // Change Time
public void run() {
timer.changeTime(0, 0, 0, -CountdownTimer.oneCentisecond);
}
}, new Runnable() { // Update Time
public void run() {
if (timer != null) {
timeRemaining.setText(timer.getTime(true, true, true, true));
}
}
}, new Runnable() {
public void run() { // Perform on close
showResult();
}
}
);
timer.updateTime();
timer.start();
setVisible(true);
}
private void showResult() {
timer.stop();
QuestionDialog.this.setVisible(false);
String message = "";
if (gaveUp) {
message = "You gave up. Try harder next time!";
} else if (timer.isOutOfTime()) {
message = "You ran out of time. Work faster next time!";
} else if (userAnswer == correctAnswer) {
message = "Correct! You had " + timer.getSeconds() + "." + timer.getCentiseconds() + " seconds remaining.";
} else {
message = "Incorrect. Your answer was " + userAnswer + ".";
}
message += "\n" + problem.getText() + correctAnswer;
JOptionPane.showMessageDialog(frame, message);
}
public boolean answerIsCorrect() {
if (gaveUp || timer.isOutOfTime()) {
return false;
} else if (userAnswer == correctAnswer) {
return true;
} else {
return false;
}
}
}