-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
42 lines (36 loc) · 1.2 KB
/
Question.java
File metadata and controls
42 lines (36 loc) · 1.2 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
package Quiz;
import java.util.ArrayList;
import java.util.Random;
//Question class stores the question, choices, and correct choice
public class Question {
String question;
ArrayList<String> choices;
String correctChoice;
Question(String question, String correctChoice, ArrayList<String> choices) {
this.question = question;
this.correctChoice = correctChoice;
this.choices = getShuffledChoices(choices);
}
public String getQuestion() {
return question;
}
public ArrayList<String> getChoices() {
return choices;
}
public String getCorrectChoice() {
return correctChoice;
}
public ArrayList<String> getShuffledChoices(ArrayList<String> choices) {
ArrayList<String> shuffledChoices = new ArrayList<>(choices);
int index;
String temp;
Random random = new Random(); // Corrected typo
for (int i = shuffledChoices.size() - 1; i > 0; i--) {
index = random.nextInt(i + 1);
temp = shuffledChoices.get(index);
shuffledChoices.set(index, shuffledChoices.get(i));
shuffledChoices.set(i, temp);
}
return shuffledChoices;
}
}