-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineExaminationSystem.java
149 lines (127 loc) · 4.83 KB
/
OnlineExaminationSystem.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Define OnlineExaminationSystem class first
public class OnlineExaminationSystem {
public static void main(String[] args) {
// Sample questions
MCQQuestion[] questions = new MCQQuestion[3];
questions[0] = new MCQQuestion("What is the capital of France?",
new String[]{"Paris", "Berlin", "London", "Rome"}, 0);
questions[1] = new MCQQuestion("Which planet is known as the Red Planet?",
new String[]{"Mars", "Jupiter", "Venus", "Mercury"}, 0);
questions[2] = new MCQQuestion("Who painted the Mona Lisa?",
new String[]{"Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh", "Michelangelo"}, 0);
// Start exam
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Examination exam = new Examination(questions, 3); // 3 minutes exam
exam.setVisible(true);
}
});
}
}
// Then define other classes
class User {
private String username;
private String password;
// Other profile information
public User(String username, String password) {
this.username = username;
this.password = password;
// Initialize other profile information
}
public boolean authenticate(String username, String password) {
return this.username.equals(username) && this.password.equals(password);
}
// Methods to update profile and password
}
class MCQQuestion {
private String question;
private String[] options;
private int correctOption;
public MCQQuestion(String question, String[] options, int correctOption) {
this.question = question;
this.options = options;
this.correctOption = correctOption;
}
public String getQuestion() {
return question;
}
public String[] getOptions() {
return options;
}
public int getCorrectOption() {
return correctOption;
}
}
class Examination extends JFrame {
private MCQQuestion[] questions;
private int totalTimeInMinutes;
private int currentQuestionIndex = 0;
private int score = 0;
private JLabel questionLabel;
private JRadioButton[] optionButtons;
private ButtonGroup buttonGroup;
private JButton nextButton;
public Examination(MCQQuestion[] questions, int totalTimeInMinutes) {
this.questions = questions;
this.totalTimeInMinutes = totalTimeInMinutes;
setTitle("Online Examination System");
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel questionPanel = new JPanel(new GridLayout(5, 1));
questionLabel = new JLabel();
questionPanel.add(questionLabel);
optionButtons = new JRadioButton[4];
buttonGroup = new ButtonGroup();
for (int i = 0; i < 4; i++) {
optionButtons[i] = new JRadioButton();
buttonGroup.add(optionButtons[i]);
questionPanel.add(optionButtons[i]);
}
add(questionPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
processAnswer();
showNextQuestion();
}
});
buttonPanel.add(nextButton);
add(buttonPanel, BorderLayout.SOUTH);
showNextQuestion();
}
private void showNextQuestion() {
if (currentQuestionIndex < questions.length) {
MCQQuestion currentQuestion = questions[currentQuestionIndex];
questionLabel.setText(currentQuestion.getQuestion());
String[] options = currentQuestion.getOptions();
for (int i = 0; i < options.length; i++) {
optionButtons[i].setText(options[i]);
optionButtons[i].setSelected(false);
}
currentQuestionIndex++;
} else {
// Display score
JOptionPane.showMessageDialog(this, "Exam Completed! Your Score: " + score);
dispose();
}
}
private void processAnswer() {
MCQQuestion currentQuestion = questions[currentQuestionIndex - 1];
for (int i = 0; i < optionButtons.length; i++) {
if (optionButtons[i].isSelected()) {
if (i == currentQuestion.getCorrectOption()) {
score++;
}
break;
}
}
}
}