-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz.java
248 lines (228 loc) · 7.04 KB
/
Quiz.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
/**
* Represents a Quiz object.
* Quizzes can be created manually by a teacher,
* where the quiz can be created by teachers and accessed by students.
*
* @author Cole Priser
* @version December 11 2021
*/
public class Quiz implements Serializable {
private String quizName;
private String questions;
private String answers;
private int numQuestions;
private int numAnswers;
private boolean isRandom;
private String teacherEmail;
private String randomQuestion;
private String randomAnswer;
/**
* Constructor to create a quiz object using a file that contains the quiz
*
* @param quizName name of the quiz
* @param questions the questions for the quiz
* @param answers the answers of the questions
* @param numQuestions number of questions in quiz
* @param numAnswers number of answer choices for each question in the quiz
* @param isRandom decides if quiz questions and answer choices will be put in random order
* @param teacherEmail email of the teacher that created the quiz
*/
public Quiz(String quizName, String questions, String answers, int numQuestions,
int numAnswers, boolean isRandom, String teacherEmail) {
this.quizName = quizName;
this.questions = questions;
this.answers = answers;
this.numQuestions = numQuestions;
this.numAnswers = numAnswers;
this.isRandom = isRandom;
this.teacherEmail = teacherEmail;
}
/**
* Method to get name of quiz
*
* @return String
*/
public String getQuizName() {
return quizName;
}
/**
* Method to get the of questions in quiz
*
* @return String
*/
public String getQuestions() {
return questions;
}
/**
* Method to set the questions of a quiz
*
* @param questions new string of questions for quiz
*/
public void setQuestions(String questions) {
this.questions = questions;
}
/**
* Method to get answer choices for a quiz
*
* @return String
*/
public String getAnswers() {
return answers;
}
/**
* Method to set the answer choices of a quiz
*
* @param answers new string of a answers for quiz
*/
public void setAnswers(String answers) {
this.answers = answers;
}
/**
* Method to get the number of questions in a quiz
*
* @return int
*/
public int getNumQuestions() {
return numQuestions;
}
/**
* Method to set the number of questions in a quiz
*
* @param numQuestions new number of questions for a quiz
*/
public void setNumQuestions(int numQuestions) {
this.numQuestions = numQuestions;
}
/**
* Method to get the number of answer choices in a quiz
*
* @return int
*/
public int getNumAnswers() {
return numAnswers;
}
/**
* Method to set the number of answer choices for each question in a quiz
*
* @param numAnswers new number of answer choices for quiz
*/
public void setNumAnswers(int numAnswers) {
this.numAnswers = numAnswers;
}
/**
* Method to see if quiz will be randomized
*
* @return boolean
*/
public boolean isRandom() {
return isRandom;
}
/**
* Method to set the value of random for a quiz to see if it should be randomized
*
* @param random new value of random to see if the quiz should be randomized
*/
public void setRandom(boolean random) {
isRandom = random;
}
/**
* Method to get the email of teacher that created quiz
*
* @return String
*/
public String getTeacherEmail() {
return teacherEmail;
}
/**
* Method to get the random order of questions
*
* @return String
*/
public String getRandomQuestion() {
return randomQuestion;
}
/**
* Method to set the questions of a quiz to the new random order
*
* @param randomQuestion new string of random order of questions for quiz
*/
public void setRandomQuestion(String randomQuestion) {
this.randomQuestion = randomQuestion;
}
/**
* Method to get the random order of answer choices
*
* @return String
*/
public String getRandomAnswer() {
return randomAnswer;
}
/**
* Method to set the answer choices of a quiz to the new random order
*
* @param randomAnswer new string of random order of answer choices for questions in a quiz
*/
public void setRandomAnswer(String randomAnswer) {
this.randomAnswer = randomAnswer;
}
/**
* Randomize the order of the questions and their corresponding answer choices
*/
public void randomize() {
ArrayList<Question> questionAndAnswer = new ArrayList<>();
ArrayList<String> questionList = new ArrayList<>();
int indexQuestion = 0;
int counterQuestion = 0;
for (int x = 0; x < questions.length(); x++) {
if (questions.startsWith("**", x)) {
questionList.add(questions.substring(indexQuestion, x));
counterQuestion++;
indexQuestion = x + 2;
}
if (counterQuestion == numQuestions) {
break;
}
}
int numAnswersUsed = 0;
int answerIndex = 0;
int tempIndex = 0;
while (numAnswersUsed != numQuestions) {
int numAnswerChoicesUsed = 0;
ArrayList<String> answerList = new ArrayList<>();
while (tempIndex + 1 < answers.length()) {
if (answers.startsWith("**", tempIndex)) {
answerList.add(answers.substring(answerIndex, tempIndex));
numAnswerChoicesUsed++;
answerIndex = tempIndex + 2;
}
tempIndex++;
if (numAnswerChoicesUsed == numAnswers) {
questionAndAnswer.add(new Question(questionList.get(numAnswersUsed), answerList));
tempIndex += 1;
answerIndex = tempIndex;
break;
}
}
numAnswersUsed++;
}
Collections.shuffle(questionAndAnswer);
for (int x = 0; x < numQuestions; x++) {
Collections.shuffle(questionAndAnswer.get(x).getAnswerChoices());
}
String newOrderQuestion = "";
String newOrderAnswer = "";
for (int x = 0; x < numQuestions; x++) {
newOrderQuestion += questionAndAnswer.get(x).getQuestion() + "**";
}
for (int x = 0; x < numQuestions; x++) {
for (int y = 0; y < questionAndAnswer.get(x).getAnswerChoices().size(); y++) {
newOrderAnswer += questionAndAnswer.get(x).getAnswerChoices().get(y) + ("**");
}
}
setRandomQuestion(newOrderQuestion);
setRandomAnswer(newOrderAnswer);
}
}