-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz.java
More file actions
150 lines (131 loc) · 6.69 KB
/
Quiz.java
File metadata and controls
150 lines (131 loc) · 6.69 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
import javax.swing.*;
import java.awt.*;
public class Quiz {
private String[] subjects = {"Java", "C", "Python"};
private String selectedSubject;
private String[] questions;
private String[][] options;
private int[] answers;
private int score = 0;
private int currentQuestionIndex = 0;
public void start() {
selectedSubject = (String) JOptionPane.showInputDialog(
null,
"Choose a subject:",
"Subject Selection",
JOptionPane.QUESTION_MESSAGE,
null,
subjects,
subjects[0]
);
if (selectedSubject != null) {
loadQuestions(selectedSubject);
showQuizFrame();
}
}
private void loadQuestions(String subject) {
switch (subject) {
case "Java":
questions = new String[]{
"What is Java?",
"What is inheritance in Java?",
"What is an interface?",
"What does JVM stand for?",
"Who developed Java?"
};
options = new String[][]{
{"A scripting language", "An object-oriented programming language widely used", "A markup language for websites", "A type of coffee"},
{"A method to inherit data", "A mechanism where one class acquires the properties and behaviors of another class completely", "A function call style", "A kind of error in Java"},
{"A user interface toolkit", "A reference type in Java that can contain only abstract methods and static fields used effectively", "A variable inside a class", "A method inside a loop"},
{"Java Visual Machine", "Java Virtual Machine", "Just Verified Module", "Java Verification Mechanism"},
{"Bill Gates", "James Gosling", "Mark Zuckerberg", "Guido van Rossum"}
};
answers = new int[]{1, 1, 1, 1, 1};
break;
case "C":
questions = new String[]{
"Who is the creator of C?",
"What is a pointer in C?",
"What is a segmentation fault?",
"What is a header file?",
"What does the 'main' function do?"
};
options = new String[][]{
{"James Gosling", "Dennis Ritchie who created C at Bell Labs", "Ken Thompson", "Bjarne Stroustrup"},
{"A kind of variable", "A variable that stores the memory address of another variable accurately", "An error handler", "A class name"},
{"A syntax error", "A runtime error caused due to illegal memory access during execution", "A memory leak", "An input error"},
{"A comment in the code", "A file with .h extension that contains function declarations and macro definitions properly", "A main file", "A binary executable"},
{"Stores user inputs", "Marks the beginning of program execution exactly", "Declares variables", "Performs calculation"}
};
answers = new int[]{1, 1, 1, 1, 1};
break;
case "Python":
questions = new String[]{
"What type of language is Python?",
"What is indentation in Python?",
"What is a list in Python?",
"What is a function in Python?",
"Who created Python?"
};
options = new String[][]{
{"Compiled low-level", "An interpreted, high-level programming language that is easy to read", "Only object-oriented", "Scripting only"},
{"A space issue", "A whitespace used to define code blocks clearly in Python", "A comment marker", "A memory unit"},
{"A collection", "A collection which is ordered and changeable with duplicate items supported", "A constant", "A mathematical set"},
{"An operator", "A block of organized, reusable code used to perform a single action precisely", "A loop", "A variable"},
{"James Gosling", "Guido van Rossum who developed Python in 1989", "Dennis Ritchie", "Linus Torvalds"}
};
answers = new int[]{1, 1, 1, 1, 1};
break;
}
}
private void showQuizFrame() {
JFrame quizFrame = new JFrame(selectedSubject + " Quiz");
quizFrame.setSize(800, 700);
quizFrame.setLayout(new BorderLayout());
quizFrame.getContentPane().setBackground(Color.decode("#424242"));
JTextArea questionArea = new JTextArea();
questionArea.setEditable(false);
questionArea.setFont(new Font("Arial", Font.BOLD, 22));
questionArea.setForeground(Color.BLACK);
questionArea.setBackground(Color.decode("#E0FFFF"));
JPanel answerPanel = new JPanel(new GridLayout(4, 1, 10, 10));
answerPanel.setBackground(Color.decode("#424242"));
updateQuestionAndOptions(quizFrame, questionArea, answerPanel);
quizFrame.add(questionArea, BorderLayout.NORTH);
quizFrame.add(answerPanel, BorderLayout.CENTER);
quizFrame.setVisible(true);
}
private void updateQuestionAndOptions(JFrame quizFrame, JTextArea questionArea, JPanel answerPanel) {
if (currentQuestionIndex >= questions.length) {
JOptionPane.showMessageDialog(quizFrame, "Your score: " + score + "/" + questions.length);
quizFrame.dispose();
return;
}
questionArea.setText(questions[currentQuestionIndex]);
answerPanel.removeAll();
for (int i = 0; i < options[currentQuestionIndex].length; i++) {
JButton answerButton = new JButton(options[currentQuestionIndex][i]);
answerButton.setFont(new Font("Arial", Font.BOLD, 20));
answerButton.setBackground(Color.WHITE);
answerButton.setForeground(Color.BLACK);
answerButton.setOpaque(true);
answerButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
int answerIndex = i;
answerButton.addActionListener(e -> {
if (answerIndex == answers[currentQuestionIndex]) {
score++;
}
currentQuestionIndex++;
updateQuestionAndOptions(quizFrame, questionArea, answerPanel);
quizFrame.revalidate();
quizFrame.repaint();
});
answerPanel.add(answerButton);
}
quizFrame.revalidate();
quizFrame.repaint();
}
public static void main(String[] args) {
new Quiz().start();
}
}