Repository navigation
Expand file tree
/
Copy pathLab07.java
More file actions
180 lines (147 loc) · 5.1 KB
/
Copy pathLab07.java
File metadata and controls
180 lines (147 loc) · 5.1 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
import java.util.*;
public class Lab07 {
public static void main(String args[]) {
QuizManager quiz = new QuizManager(5);
quiz.addQuestion(new TrueFalseQuestion(1, "Java is platform-independent?", true, 5));
quiz.addQuestion(new MultipleChoiceQuestion(2, "Which one is not a Java keyword?",
new String[] { "class", "interface", "goto", "define" }, 4, 10));
quiz.addQuestion(new ShortAnswerQuestion(3, "What does JVM stand for?", "Java Virtual Machine", 10));
quiz.displayAllQuestion();
quiz.answerQuestion(1, "true");
quiz.answerQuestion(2, "4");
quiz.answerQuestion(3, "java virtual machine");
quiz.showScore();
}
}
interface Displayable {
void displayQuestion();
}
interface Answerable {
boolean checkAnswer(String userAnswer);
}
abstract class Question<T> implements Displayable, Answerable {
protected int id;
protected String questionText;
protected T correctAnswer;
protected int points;
public Question(int id, String questionText, T correctAnswer, int points) {
this.id = id;
this.questionText = questionText;
this.correctAnswer = correctAnswer;
this.points = points;
}
public int getId() {
return id;
}
public int getPoints() {
return points;
}
protected abstract T parseAnswer(String userAnswer);
@Override
public boolean checkAnswer(String userAnswer) {
T parsed = parseAnswer(userAnswer);
return correctAnswer.equals(parsed);
}
}
class TrueFalseQuestion extends Question<Boolean> {
public TrueFalseQuestion(int id, String text, Boolean correctAnswer, int points) {
super(id, text, correctAnswer, points);
}
@Override
protected Boolean parseAnswer(String userAnswer) {
return Boolean.parseBoolean(userAnswer.trim().toLowerCase());
}
@Override
public void displayQuestion() {
System.out.println("[" + id + "] (" + points + " pts) " + questionText + " (true/false)");
}
}
class MultipleChoiceQuestion extends Question<Integer> {
private String[] options;
public MultipleChoiceQuestion(int id, String text, String[] options, int correctOptionNumber, int points) {
super(id, text, correctOptionNumber, points);
this.options = options;
}
@Override
public Integer parseAnswer(String userAnswer) {
return Integer.parseInt(userAnswer.trim());
}
@Override
public void displayQuestion() {
System.out.println("[" + id + "] (" + points + " pts) " + questionText);
for (int i = 0; i < options.length; i++) {
System.out.println(" " + (i + 1) + " )" + options[i]);
}
}
}
class ShortAnswerQuestion extends Question<String> {
public ShortAnswerQuestion(int id, String text, String correctAnswer, int points) {
super(id, text, correctAnswer.trim().toLowerCase(), points);
}
@Override
protected String parseAnswer(String userAnswer) {
return userAnswer.trim().toLowerCase();
}
@Override
public void displayQuestion() {
System.out.println("[" + id + "] (" + points + " pts) " + questionText + " (short answer)");
}
}
class QuizManager {
private Question<?>[] questions;
private int count;
private int totalScore;
public QuizManager(int capacity) {
questions = new Question<?>[capacity];
count = 0;
totalScore = 0;
}
public void addQuestion(Question<?> q) {
if (count < questions.length) {
questions[count++] = q;
} else {
System.out.println("Quiz is full! Cannot add more questions.");
}
}
public void removeQuestionById(int id) {
for (int i = 0; i < count; i++) {
if (questions[i].getId() == id) {
for (int j = i; j < count - 1; j++) {
questions[j] = questions[j + 1];
}
questions[--count] = null;
System.out.println("Question removed.");
return;
}
}
System.out.println("Question not found.");
}
public void displayAllQuestion() {
for (int i = 0; i < count; i++) {
questions[i].displayQuestion();
System.out.println();
}
}
public void answerQuestion(int id, String userAnswer) {
for (int i = 0; i < count; i++) {
if (questions[i].getId() == id) {
boolean correct = questions[i].checkAnswer(userAnswer);
if (correct) {
totalScore += questions[i].getPoints();
System.out.println("Correct! +" + questions[i].getPoints() + " points.");
} else {
System.out.println("Incorrect.");
}
return;
}
}
System.out.println("Question not found.");
}
public void showScore() {
int maxScore = 0;
for (int i = 0; i < count; i++) {
maxScore += questions[i].getPoints();
}
System.out.println("Score: " + totalScore + " / " + maxScore);
}
}