Repository navigation
Expand file tree
/
Copy pathQuizHard.java
More file actions
313 lines (263 loc) · 10.5 KB
/
Copy pathQuizHard.java
File metadata and controls
313 lines (263 loc) · 10.5 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
package quiz.application;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class QuizHard extends JFrame implements ActionListener {
String questions[][] = new String[10][5];
String answers[][] = new String[10][2];
String useranswers[][] = new String[10][1];
JLabel qno, question;
JRadioButton opt1, opt2, opt3, opt4;
ButtonGroup groupoptions;
JButton next, submit, lifeline;
public static int timer = 15;
public static int ans_given = 0;
public static int count = 0;
public static int score = 0;
String name;
QuizHard(String name) {
this.name = name;
setBounds(50, 0, 1440, 850);
getContentPane().setBackground(Color.WHITE);
setLayout(null);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/quiz.jpg"));
JLabel image = new JLabel(i1);
image.setBounds(0, 0, 1440, 392);
add(image);
qno = new JLabel();
qno.setBounds(100, 450, 50, 30);
qno.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(qno);
question = new JLabel();
question.setBounds(150, 450, 900, 30);
question.setFont(new Font("Tahoma", Font.PLAIN, 24));
add(question);
questions[0][0] = "Iterator returned by Hashtable on key, value and entry are?";
questions[0][1] = "Fail-fast";
questions[0][2] = "Fail-safe";
questions[0][3] = "None of the above";
questions[0][4] = "All of the above";
questions[1][0] = "What is the return type of the hashCode() method in the Object class?";
questions[1][1] = "int";
questions[1][2] = "Object";
questions[1][3] = "long";
questions[1][4] = "void";
questions[2][0] = "Iterator returned by ConcurrentHashMap on key, value and entry is?";
questions[2][1] = "Fail-fast";
questions[2][2] = "Fail-safe";
questions[2][3] = "None of above";
questions[2][4] = "All of the above";
questions[3][0] = "Which of those doesn’t have an index based structure?";
questions[3][1] = "List";
questions[3][2] = "Set";
questions[3][3] = "Map";
questions[3][4] = "All of the above";
questions[4][0] = "java.util.Collections is a:";
questions[4][1] = "Class";
questions[4][2] = "Interface";
questions[4][3] = "Object";
questions[4][4] = "None of the above";
questions[5][0] = "Methods such as reverse, shuffle are offered in:";
questions[5][1] = "Object";
questions[5][2] = "Collection";
questions[5][3] = "Collections";
questions[5][4] = "Apache Commons Collections";
questions[6][0] = "Which of those allows duplicate elements?";
questions[6][1] = "Set";
questions[6][2] = "List";
questions[6][3] = "All";
questions[6][4] = "None of the Above";
questions[7][0] = "Which allows the storage of a null key and null values?";
questions[7][1] = "Hashtable";
questions[7][2] = "HashMap";
questions[7][3] = "All";
questions[7][4] = "None of the above";
questions[8][0] = "Which interface should be implemented for sorting on basis of many criteria’s?";
questions[8][1] = "Comparator";
questions[8][2] = "Comparable";
questions[8][3] = "Serializable";
questions[8][4] = "None of the above";
questions[9][0] = "Which of the following option leads to the portability and security of Java?";
questions[9][1] = "Bytecode is executed by JVM";
questions[9][2] = "The applet makes the Java code secure and portable";
questions[9][3] = "Use of exception handling";
questions[9][4] = "Dynamic binding between objects";
answers[0][1] = "Fail-fast";
answers[1][1] = "int";
answers[2][1] = "Fail-safe";
answers[3][1] = "Set";
answers[4][1] = "Class";
answers[5][1] = "Collections";
answers[6][1] = "List";
answers[7][1] = "HashMap";
answers[8][1] = "Comparator";
answers[9][1] = "Bytecode is executed by JVM";
opt1 = new JRadioButton();
opt1.setBounds(170, 520, 700, 30);
opt1.setBackground(Color.WHITE);
opt1.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt1);
opt2 = new JRadioButton();
opt2.setBounds(170, 560, 700, 30);
opt2.setBackground(Color.WHITE);
opt2.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt2);
opt3 = new JRadioButton();
opt3.setBounds(170, 600, 700, 30);
opt3.setBackground(Color.WHITE);
opt3.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt3);
opt4 = new JRadioButton();
opt4.setBounds(170, 640, 700, 30);
opt4.setBackground(Color.WHITE);
opt4.setFont(new Font("Dialog", Font.PLAIN, 20));
add(opt4);
groupoptions = new ButtonGroup();
groupoptions.add(opt1);
groupoptions.add(opt2);
groupoptions.add(opt3);
groupoptions.add(opt4);
next = new JButton("Next");
next.setBounds(1100, 550, 200, 40);
next.setFont(new Font("Tahoma", Font.PLAIN, 22));
next.setBackground(new Color(30, 144, 255));
next.setForeground(Color.WHITE);
next.addActionListener(this);
add(next);
lifeline = new JButton("50-50 Lifeline");
lifeline.setBounds(1100, 630, 200, 40);
lifeline.setFont(new Font("Tahoma", Font.PLAIN, 22));
lifeline.setBackground(new Color(30, 144, 255));
lifeline.setForeground(Color.WHITE);
lifeline.addActionListener(this);
add(lifeline);
submit = new JButton("Submit");
submit.setBounds(1100, 710, 200, 40);
submit.setFont(new Font("Tahoma", Font.PLAIN, 22));
submit.setBackground(new Color(30, 144, 255));
submit.setForeground(Color.WHITE);
submit.addActionListener(this);
submit.setEnabled(false);
add(submit);
start(count);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == next) {
repaint();
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);
ans_given = 1;
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
if (count == 8) {
next.setEnabled(false);
submit.setEnabled(true);
}
count++;
start(count);
} else if (ae.getSource() == lifeline) {
if (count == 2 || count == 4 || count == 6 || count == 8 || count == 9) {
opt2.setEnabled(false);
opt3.setEnabled(false);
} else {
opt1.setEnabled(false);
opt4.setEnabled(false);
}
lifeline.setEnabled(false);
} else if (ae.getSource() == submit) {
ans_given = 1;
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
for (int i = 0; i < useranswers.length; i++) {
if (useranswers[i][0].equals(answers[i][1])) {
score += 10;
} else {
score += 0;
}
}
setVisible(false);
new Score(name, score);
}
}
public void paint(Graphics g) {
super.paint(g);
String time = "Time left - " + timer + " seconds"; // 15
g.setColor(Color.RED);
g.setFont(new Font("Tahoma", Font.BOLD, 25));
if (timer > 0) {
g.drawString(time, 1100, 500);
} else {
g.drawString("Times up!!", 1100, 500);
}
timer--; // 14
try {
Thread.sleep(1000);
repaint();
} catch (Exception e) {
e.printStackTrace();
}
if (ans_given == 1) {
ans_given = 0;
timer = 15;
} else if (timer < 0) {
timer = 15;
opt1.setEnabled(true);
opt2.setEnabled(true);
opt3.setEnabled(true);
opt4.setEnabled(true);
if (count == 8) {
next.setEnabled(false);
submit.setEnabled(true);
}
if (count == 9) { // submit button
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
for (int i = 0; i < useranswers.length; i++) {
if (useranswers[i][0].equals(answers[i][1])) {
score += 10;
} else {
score += 0;
}
}
setVisible(false);
new Score(name, score);
} else { // next button
if (groupoptions.getSelection() == null) {
useranswers[count][0] = "";
} else {
useranswers[count][0] = groupoptions.getSelection().getActionCommand();
}
count++; // 0 // 1
start(count);
}
}
}
public void start(int count) {
qno.setText("" + (count + 1) + ". ");
question.setText(questions[count][0]);
opt1.setText(questions[count][1]);
opt1.setActionCommand(questions[count][1]);
opt2.setText(questions[count][2]);
opt2.setActionCommand(questions[count][2]);
opt3.setText(questions[count][3]);
opt3.setActionCommand(questions[count][3]);
opt4.setText(questions[count][4]);
opt4.setActionCommand(questions[count][4]);
groupoptions.clearSelection();
}
public static void main(String[] args) {
new QuizHard("User");
}
}