-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathCheckboxQuestion.java
More file actions
41 lines (34 loc) · 1.42 KB
/
CheckboxQuestion.java
File metadata and controls
41 lines (34 loc) · 1.42 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
package org.launchcode;
import java.util.List;
class CheckboxQuestion extends Question {
private List<String> choices;
private List<String> correctAnswers;
public CheckboxQuestion(String questionText, List<String> choices, List<String> correctAnswers) {
super(questionText, correctAnswers); // Pass correctAnswers to the superclass constructor
this.choices = choices;
this.correctAnswers = correctAnswers;
}
@Override
public void displayQuestion() {
System.out.println("Question: " + getQuestionText());
System.out.println("Choices:");
for (int i = 0; i < choices.size(); i++) {
System.out.println((char)('A' + i) + ") " + choices.get(i)); // Use letters (A, B, C, ...) for choices
}
System.out.println("Select multiple choices separated by commas (e.g., A, C): ");
}
@Override
public boolean isCorrect(Object answer) {
if (answer instanceof String) {
String[] selectedChoices = ((String) answer).split(",");
for (String choice : selectedChoices) {
choice = choice.trim(); // Remove leading/trailing whitespace
if (!correctAnswers.contains(choice)) {
return false; // If any choice is not correct, the answer is incorrect
}
}
return true;
}
return false; // Incorrect format for answer
}
}