-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOnlinetest.java
More file actions
166 lines (141 loc) · 6.24 KB
/
Onlinetest.java
File metadata and controls
166 lines (141 loc) · 6.24 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class OnlineJavaTest extends JFrame {
private JLabel questionLabel;
private JRadioButton[] options;
private JButton nextButton, bookmarkButton, resultButton;
private ButtonGroup optionGroup;
private int currentQuestion = 0;
private int score = 0;
private List<Integer> bookmarkedQuestions = new ArrayList<>();
// Question data
private final String[] questions = {
"Which one among these is not a primitive datatype?",
"Which class is available to all the class automatically?",
"Which package is directly available to our class without importing it?",
"String class is defined in which package?",
"Which institute is best for java coaching?",
"Which one among these is not a keyword?",
"Which one among these is not a class?",
"Which one among these is not a function of Object class?",
"Which function is not present in Applet class?",
"Which one among these is not a valid component?"
};
private final String[][] choices = {
{"int", "Float", "boolean", "char"},
{"Swing", "Applet", "Object", "ActionEvent"},
{"swing", "applet", "net", "lang"},
{"lang", "Swing", "Applet", "awt"},
{"Dheeraj Notes", "Aptech", "SSS IT", "jtek"},
{"class", "int", "get", "if"},
{"Swing", "Actionperformed", "ActionEvent", "Button"},
{"toString", "finalize", "equals", "getDocumentBase"},
{"init", "main", "start", "destroy"},
{"JButton", "JList", "JButtonGroup", "JTextArea"}
};
private final int[] correctAnswers = {1, 2, 3, 0, 2, 2, 1, 3, 1, 2};
public OnlineJavaTest() {
super("Online Java Test");
initializeUI();
setupQuestion();
}
private void initializeUI() {
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
// Question panel
JPanel questionPanel = new JPanel(new BorderLayout());
questionLabel = new JLabel();
questionLabel.setFont(new Font("Arial", Font.BOLD, 14));
questionPanel.add(questionLabel, BorderLayout.NORTH);
questionPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
// Options panel
JPanel optionsPanel = new JPanel(new GridLayout(4, 1));
optionGroup = new ButtonGroup();
options = new JRadioButton[4];
for (int i = 0; i < options.length; i++) {
options[i] = new JRadioButton();
options[i].setFont(new Font("Arial", Font.PLAIN, 13));
optionGroup.add(options[i]);
optionsPanel.add(options[i]);
}
questionPanel.add(optionsPanel, BorderLayout.CENTER);
// Button panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
nextButton = new JButton("Next");
bookmarkButton = new JButton("Bookmark");
resultButton = new JButton("View Result");
nextButton.addActionListener(this::handleNext);
bookmarkButton.addActionListener(this::handleBookmark);
resultButton.addActionListener(this::showResult);
buttonPanel.add(bookmarkButton);
buttonPanel.add(nextButton);
buttonPanel.add(resultButton);
add(questionPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
private void setupQuestion() {
if (currentQuestion < questions.length) {
questionLabel.setText("Question " + (currentQuestion + 1) + ": " + questions[currentQuestion]);
for (int i = 0; i < options.length; i++) {
options[i].setText(choices[currentQuestion][i]);
options[i].setSelected(false);
}
// Update button states
nextButton.setEnabled(currentQuestion < questions.length - 1);
resultButton.setEnabled(currentQuestion == questions.length - 1);
}
}
private void handleNext(ActionEvent e) {
checkAnswer();
currentQuestion++;
setupQuestion();
}
private void handleBookmark(ActionEvent e) {
if (!bookmarkedQuestions.contains(currentQuestion)) {
bookmarkedQuestions.add(currentQuestion);
JOptionPane.showMessageDialog(this, "Question " + (currentQuestion + 1) + " bookmarked!");
} else {
JOptionPane.showMessageDialog(this, "Question already bookmarked!");
}
}
private void showResult(ActionEvent e) {
checkAnswer();
String result = "Test Completed!\n\n";
result += "Your Score: " + score + " out of " + questions.length + "\n";
result += "Percentage: " + (score * 100 / questions.length) + "%\n\n";
if (score == questions.length) {
result += "Perfect! You got all questions right!";
} else if (score >= questions.length * 0.7) {
result += "Good job! You passed with a good score.";
} else if (score >= questions.length * 0.5) {
result += "You passed, but consider reviewing Java concepts.";
} else {
result += "You need more practice with Java fundamentals.";
}
JOptionPane.showMessageDialog(this, result);
System.exit(0);
}
private void checkAnswer() {
for (int i = 0; i < options.length; i++) {
if (options[i].isSelected() && i == correctAnswers[currentQuestion]) {
score++;
break;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new OnlineJavaTest().setVisible(true);
});
}
}