-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPhysicsQuiz.java
More file actions
402 lines (396 loc) · 13.2 KB
/
PhysicsQuiz.java
File metadata and controls
402 lines (396 loc) · 13.2 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
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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import java.io.*;
public class PhysicsQuiz extends Frame implements WindowListener, ActionListener{
private Label errorlabel;
public Connection getConnection() throws SQLException{ //This method connects the program to the database
String user = "root";
String pass = "usbw";
Connection c = null;
c = DriverManager.getConnection("jdbc:mysql://localhost:3307/pastpapers", user, pass);
System.out.println("Connected to database");
return c;
}
private Random rand = new Random();
private Label menuheader;
private Choice topiclist;
private Button test;
private Button scores;
private Button quit;
private Button submit;
private Label questionheader;
private Choice multiquestion;
private JTextField calculation;
private JTextField equation;
private JTextField name;
private Label incorrectQuestion;
private Button finish;
private boolean answered = false;
String topic;
List<Integer> primaryKeys = new LinkedList<Integer>();
int questionNo = 0;
int score = 0;
int random;
int multirandom;
String username;
question[] questions = new question[10];
String[] answers = new String[10];
String[] multianswers = new String[10];
String[] calcanswers = new String[10];
String[] eqanswers = new String[10];
multiAnswer[] answerOptions = new multiAnswer[10];
List<String> incorrectAnswers = new LinkedList<String>();
public PhysicsQuiz(){
mainMenu();
}
public void mainMenu(){ //What the user will see first, where they choose which topic to test on
this.removeAll();
repaint();
setLayout(new GridLayout(10,1));
menuheader = new Label("Choose the topic you wish to revise.");
add(menuheader);
topiclist = new Choice();
topiclist.add("Mechanics");
topiclist.add("Materials");
topiclist.add("Waves");
topiclist.add("DC Electricity");
topiclist.add("Nature of Light");
add(topiclist);
test = new Button("Test!");
add(test);
test.addActionListener(this);
scores = new Button("View previous scores on this topic");
add(scores);
scores.addActionListener(this);
quit = new Button("Quit");
add(quit);
quit.addActionListener(this);
addWindowListener(this);
setTitle("Physics Quiz");
setSize(1600, 600);
setVisible(true);
repaint();
}
public void getTopicQuestions() throws SQLException{ //This gets the questionIDs from the database and stores them in an array. A list of primary keys is used to randomly select 10 questions.
try{
Connection c = getConnection();
Statement st = c.createStatement();
String sqlcontent = "SELECT QuestionID FROM question WHERE QuestionTopic = '" + topic + "'";
ResultSet rs0 = st.executeQuery(sqlcontent);
while(rs0.next()){
System.out.println(rs0.getInt(1));
primaryKeys.add(rs0.getInt(1));
}
c.close();
}catch(SQLException ei){
errorlabel = new Label(ei.toString());
add(errorlabel);
setVisible(true);
}
for(int i = 0;i<questions.length;i++){
try{
random = rand.nextInt(primaryKeys.size());
System.out.println(primaryKeys.get(i));
questions[i] = new question();
questions[i].id = primaryKeys.get(random);
primaryKeys.remove(random);
}catch(NullPointerException er){System.out.println(er + " " + i);}
}
}
public void getTopicAnswers() throws SQLException{ //This gets the answers for each question and stores them in an array.
for(int f=0;f<questions.length;f++){
try{
Connection c = getConnection();
Statement st = c.createStatement();
String sqlcontent = "SELECT AnswerContent FROM answer WHERE AnswerID = '" + questions[f].id + "'";
ResultSet rsA = st.executeQuery(sqlcontent);
try{
while (rsA.next()){
answers[f] = rsA.getString(1);
}
} finally {
rsA.close();
}
c.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
}
public void displayNextQuestion(){ //This gets the next question from the array, then displays it appropriately, depending on whether it is a multiple choice or calculation question.
for(int y=0;y<answerOptions.length;y++){
answerOptions[y] = new multiAnswer();
}
if(questions[questionNo].type.equals("Multiple Choice")){
for(int f=0;f<questions.length;f++){
for(int o=1;o<4;o++){
try{
Connection c = getConnection();
Statement st = c.createStatement();
String sqlcontent2 = "SELECT AnswerOption" + o + " FROM answer WHERE AnswerID = '" +questions[f].id + "'";
ResultSet rsB = st.executeQuery(sqlcontent2);
try{
while(rsB.next()){
if(o == 1){
answerOptions[f].answer1 = rsB.getString(1);
}
else if(o == 2){
answerOptions[f].answer2 = rsB.getString(1);
}
else if(o == 3){
answerOptions[f].answer3 = rsB.getString(1);
}
}
} finally {
rsB.close();
}
c.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
}
questionheader = new Label("Question " + questionNo + " : " + questions[questionNo].content);
add(questionheader);
multiquestion = new Choice();
add(multiquestion);
multirandom = rand.nextInt(3);
if(multirandom == 0){
multiquestion.add(answers[questionNo]);
multiquestion.add(answerOptions[questionNo].answer1);
multiquestion.add(answerOptions[questionNo].answer2);
multiquestion.add(answerOptions[questionNo].answer3);
}
else if(multirandom == 1){
multiquestion.add(answerOptions[questionNo].answer3);
multiquestion.add(answers[questionNo]);
multiquestion.add(answerOptions[questionNo].answer1);
multiquestion.add(answerOptions[questionNo].answer2);
}
else if(multirandom == 2){
multiquestion.add(answerOptions[questionNo].answer1);
multiquestion.add(answerOptions[questionNo].answer2);
multiquestion.add(answers[questionNo]);
multiquestion.add(answerOptions[questionNo].answer3);
}
else if(multirandom == 3){
multiquestion.add(answerOptions[questionNo].answer1);
multiquestion.add(answerOptions[questionNo].answer2);
multiquestion.add(answerOptions[questionNo].answer3);
multiquestion.add(answers[questionNo]);
}
submit = new Button("Submit");
add(submit);
submit.addActionListener(this);
finish = new Button("Back");
add(finish);
finish.addActionListener(this);
setVisible(true);
repaint();
}
else if(questions[questionNo].type.equals("Calculation")){
questionheader = new Label("Question " + questionNo + " : " + questions[questionNo].content);
add(questionheader);
calculation = new JTextField("Enter your final answer. (The number you get)");
add(calculation);
equation = new JTextField("Enter the final equation you used. (All values except constants should be full words, with capital letters and separated by a space e.g. Force = Mass x Acceleration. Constants using standard form write as _x10^_)");
add(equation);
//add JTextField for Units.
submit = new Button("Submit");
add(submit);
submit.addActionListener(this);
finish = new Button("Back");
add(finish);
finish.addActionListener(this);
setVisible(true);
repaint();
}
else{
System.out.println("Error. Cannot get results from query");
System.out.println(questions[questionNo].content);
}
}
public void Answers(){ //This reads the user's answer and checks which they got right and counts their score. Incorrect questions are stored in an array which will be used to feedback to them.
if(questions[questionNo].type.equals("Multiple Choice")){
multianswers[questionNo] = multiquestion.getSelectedItem();
if(multianswers[questionNo].equals(answers[questionNo])){
score++;
}
else {
incorrectAnswers.add(questions[questionNo].content);
}
}
else if(questions[questionNo].type.equals("Calculation")){
calcanswers[questionNo] = calculation.getText();
eqanswers[questionNo] = equation.getText();
if(calcanswers[questionNo].equals(answers[questionNo]) && eqanswers[questionNo].equals(answers[questionNo])){
score++;
score++;
}
else if(eqanswers[questionNo].equals(answers[questionNo]) && !calcanswers[questionNo].equals(answers[questionNo])){
score++;
}
else if(calcanswers[questionNo].equals(answers[questionNo]) && !eqanswers[questionNo].equals(answers[questionNo])){
score++;
}
else{
incorrectAnswers.add(questions[questionNo].content);
}
}
else{
System.out.println("Error. Cannot get results from query");
System.out.println(questions[questionNo].content);
}
}
public void Results(){ //This displays the results of the test. Each question that was answered incorrectly is displayed. The user enters their name to be stored in the scores file.
questionheader = new Label("Your final score is: " + score + ". Here are the questions that you got wrong:");
add(questionheader);
setVisible(true);
for(int d=0;d < incorrectAnswers.size();d++){
incorrectQuestion = new Label(incorrectAnswers.get(d));
add(incorrectQuestion);
setVisible(true);
repaint();
}
finish = new Button("Back to menu");
add(finish);
finish.addActionListener(this);
name = new JTextField("Enter your name");
add(name);
setVisible(true);
repaint();
}
public void Test() throws SQLException{ //This is where the question content is fetched from the database and stored in an array.
remove(topiclist);
remove(test);
remove(scores);
remove(quit);
remove(menuheader);
for(int j=0;j<questions.length;j++){
try{
Connection c = getConnection();
Statement st = c.createStatement();
String sqlcontent = "SELECT QuestionContent FROM question WHERE QuestionID = " + questions[j].id;
ResultSet rs1 = st.executeQuery(sqlcontent);
try{
while (rs1.next()){
questions[j].content = rs1.getString(1);
}
} finally {
rs1.close();
}
String sqltype = "SELECT QuestionType FROM question WHERE QuestionID = " + questions[j].id;
ResultSet rs2 = st.executeQuery(sqltype);
try{
while (rs2.next()){
questions[j].type = rs2.getString(1);
}
} finally {
rs2.close();
}
st.close();
c.close();
}catch(SQLException ex){
System.out.println(ex);
}
}
displayNextQuestion();
}
public static void main(String[] args){
PhysicsQuiz app = new PhysicsQuiz();
}
public void actionPerformed(ActionEvent e){
String action = e.getActionCommand();
if(action.equals("Test!")){ //When the test button is clicked, gets the topic and then runs the methods to get the questions and answers and run the test.
questionNo = 0;
topic = topiclist.getSelectedItem();
try{
getTopicQuestions();
getTopicAnswers();
Test();
}
catch(SQLException ex){
System.out.println(ex);
}
}
if(action.equals("View previous scores on this topic")){ //Just a link to tell the user where they can find their scores.
this.removeAll();
questionheader = new Label("To find your previous scores, open the text file located in this folder with the title '*topic* scores'.");
add(questionheader);
finish = new Button("Back");
add(finish);
finish.addActionListener(this);
setVisible(true);
repaint();
}
if(action.equals("Submit")){ //Runs the method that shows the answers. Also displays all the incorrect questions.
Answers();
this.removeAll();
answered = true;
questionNo++;
if(questionNo < questions.length){
displayNextQuestion();
}
else{
Results();
}
}
if(action.equals("Quit")){
System.exit(0);
}
if(action.equals("Back")){
mainMenu();
}
if(action.equals("Back to menu")){ //After the feedback, when back is clicked this will store the name of the user and their score in the appropriate text file then return to the menu.
username = name.getText();
if(username.equals("")){
username = "Student";
}
else{
username = username;
}
try{
if(topic == "Mechanics"){
BufferedWriter out = new BufferedWriter(new FileWriter("Mechanics Scores.txt", true));
out.newLine();
out.write(username + " got " + score + ".");
out.close();
}
else if(topic == "DC Electricity"){
BufferedWriter out = new BufferedWriter(new FileWriter("DC Electricity Scores.txt", true));
out.write(username + " got " + score + ".");
out.close();
}
else if(topic == "Materials"){
BufferedWriter out = new BufferedWriter(new FileWriter("Materials Scores.txt", true));
out.write(username + " got " + score + ".");
out.close();
}
else if(topic == "Waves"){
BufferedWriter out = new BufferedWriter(new FileWriter("Waves Scores.txt", true));
out.write(username + " got " + score + ".");
out.close();
}
else if(topic == "Nature of Light"){
BufferedWriter out = new BufferedWriter(new FileWriter("Nature of Light Scores.txt", true));
out.write(username + " got " + score + ".");
out.close();
}
} catch (IOException ioe){System.out.println(ioe);}
mainMenu();
}
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowOpened(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
}