-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineTest.java
205 lines (194 loc) · 6.1 KB
/
OnlineTest.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
package onlineJavaTest;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class OnlineTest extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel label;
JRadioButton radioButton[] = new JRadioButton[5];
JButton btnNext, btnBookmark;
ButtonGroup bg;
int count = 0, current = 0, x = 1, y = 1, now = 0;
int m[] = new int[10];
// create jFrame with radioButton and JButton
OnlineTest(String s) {
super(s);
label = new JLabel();
add(label);
bg = new ButtonGroup();
for (int i = 0; i < 5; i++) {
radioButton[i] = new JRadioButton();
add(radioButton[i]);
bg.add(radioButton[i]);
}
btnNext = new JButton("Next");
btnBookmark = new JButton("Bookmark");
btnNext.addActionListener(this);
btnBookmark.addActionListener(this);
add(btnNext);
add(btnBookmark);
set();
label.setBounds(30, 40, 450, 20);
//radioButton[0].setBounds(50, 80, 200, 20);
radioButton[0].setBounds(50, 80, 450, 20);
radioButton[1].setBounds(50, 110, 200, 20);
radioButton[2].setBounds(50, 140, 200, 20);
radioButton[3].setBounds(50, 170, 200, 20);
btnNext.setBounds(100, 240, 100, 30);
btnBookmark.setBounds(270, 240, 100, 30);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setLocation(250, 100);
setVisible(true);
setSize(600, 350);
}
// handle all actions based on event
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnNext) {
if (check())
count = count + 1;
current++;
set();
if (current == 9) {
btnNext.setEnabled(false);
btnBookmark.setText("Result");
}
}
if (e.getActionCommand().equals("Bookmark")) {
JButton bk = new JButton("Bookmark" + x);
bk.setBounds(480, 20 + 30 * x, 100, 30);
add(bk);
bk.addActionListener(this);
m[x] = current;
x++;
current++;
set();
if (current == 9)
btnBookmark.setText("Result");
setVisible(false);
setVisible(true);
}
for (int i = 0, y = 1; i < x; i++, y++) {
if (e.getActionCommand().equals("Bookmark" + y)) {
if (check())
count = count + 1;
now = current;
current = m[y];
set();
((JButton) e.getSource()).setEnabled(false);
current = now;
}
}
if (e.getActionCommand().equals("Result")) {
if (check())
count = count + 1;
current++;
JOptionPane.showMessageDialog(this, "correct answers= " + count);
System.exit(0);
}
}
// SET Questions with options
void set() {
radioButton[4].setSelected(true);
if (current == 0) {
label.setText("Que1: Which of the following is not introduced with Java 8?");
radioButton[0].setText("Stream API");
radioButton[1].setText("Serialization");
radioButton[2].setText("Spliterator");
radioButton[3].setText("Lambda Expression");
}
if (current == 1) {
label.setText("Que2: Which feature of java 7 allows to not explicitly close IO resource?");
radioButton[0].setText("try catch finally");
radioButton[1].setText("IOException");
radioButton[2].setText("AutoCloseable");
radioButton[3].setText("Streams");
}
if (current == 2) {
label.setText("Que3: SessionFactory is a thread-safe object.");
radioButton[0].setText("true");
radioButton[1].setText("false");
radioButton[2].setText("don't know");
radioButton[3].setText("false");
}
if (current == 3) {
label.setText("Que4: Which is the new method introduced in java 8 to iterate over a collection?");
radioButton[0].setText("for (String i : StringList)");
radioButton[1].setText("foreach (String i : StringList)");
radioButton[2].setText("StringList.forEach()");
radioButton[3].setText("List.for()");
}
if (current == 4) {
label.setText("Que5: What is the substitute of Rhino javascript engine in Java 8?");
radioButton[0].setText(" Nashorn");
radioButton[1].setText("V8");
radioButton[2].setText("Inscript");
radioButton[3].setText("Narcissus");
}
if (current == 5) {
label.setText("Que6: How to read entire file in one line using java 8?");
radioButton[0].setText("Files.readAllLines()");
radioButton[1].setText("Files.read()");
radioButton[2].setText("Files.readFile()");
radioButton[3].setText("Files.lines()");
}
if (current == 6) {
label.setText("Que7: Which feature of java 7 allows to not explicitly close IO resource?");
radioButton[0].setText("try catch finally");
radioButton[1].setText("IOException");
radioButton[2].setText("AutoCloseable");
radioButton[3].setText("Streams");
}
if (current == 7) {
label.setText("Que8: Which of the following is not a core interface of Hibernate?");
radioButton[0].setText("Configuration");
radioButton[1].setText("Criteria");
radioButton[2].setText("SessionManagement");
radioButton[3].setText("Session");
}
if (current == 8) {
label.setText("Que9: SessionFactory is a thread-safe object.");
radioButton[0].setText("true");
radioButton[1].setText("false");
radioButton[2].setText("don't know");
radioButton[3].setText("false");
}
if (current == 9) {
label.setText("Que10: Which of the following is not a state of object in Hibernate?");
radioButton[0].setText("Attached()");
radioButton[1].setText("Detached()");
radioButton[2].setText("Persistent()");
radioButton[3].setText("Transient()");
}
label.setBounds(30, 40, 450, 20);
for (int i = 0, j = 0; i <= 90; i += 30, j++)
radioButton[j].setBounds(50, 80 + i, 200, 20);
}
// declare right answers.
boolean check() {
if (current == 0)
return (radioButton[1].isSelected());
if (current == 1)
return (radioButton[1].isSelected());
if (current == 2)
return (radioButton[0].isSelected());
if (current == 3)
return (radioButton[2].isSelected());
if (current == 4)
return (radioButton[0].isSelected());
if (current == 5)
return (radioButton[0].isSelected());
if (current == 6)
return (radioButton[1].isSelected());
if (current == 7)
return (radioButton[2].isSelected());
if (current == 8)
return (radioButton[0].isSelected());
if (current == 9)
return (radioButton[0].isSelected());
return false;
}
public static void main(String s[]) {
new OnlineTest("Online Test App");
}
}