-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuiz.java
More file actions
229 lines (205 loc) · 7.89 KB
/
Copy pathQuiz.java
File metadata and controls
229 lines (205 loc) · 7.89 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
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
import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
/** Quiz class for creating quizzes
*
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/5/2022
*/
public class Quiz {
private int quizID; // Quiz number
private int numberQuestions; // Number of questions in this quiz
private LocalDateTime deadline; // Deadline for the quiz
private int duration; // How many minutes the quiz should take
private int totalPoints; // How many points the quiz is worth
private int[] questionPoints; // a record of what each question is worth
private int[] questionsIndex; // An array for question pool
Tools tools = new Tools();
/** Quiz constructors
* CS1800 Spring 2022, Project 4
* @author Taylor Graham, Alex lee
* @version 4/11/2022
*/
public Quiz( int numberQuestions, LocalDateTime deadline, int duration, int totalPoints,
int[] questionsIndex, int[] questionPoints) {
this.quizID = retrieveLargestID() + 1; // The new quiz ID will be one greater than the largest one
this.numberQuestions = numberQuestions;
this.deadline = deadline;
this.duration = duration;
this.totalPoints = totalPoints;
this.questionsIndex = questionsIndex;
this.questionPoints = questionPoints;
}
public Quiz( int quizID, int numberQuestions, LocalDateTime deadline, int duration, int totalPoints,
int[] questionsIndex, int[] questionPoints) {
this.quizID = quizID;
this.numberQuestions = numberQuestions;
this.deadline = deadline;
this.duration = duration;
this.totalPoints = totalPoints;
this.questionsIndex = questionsIndex;
this.questionPoints = questionPoints;
}
public int getQuizID() {
return this.quizID;
}
public int[] getQuestionsIndex() {
return this.questionsIndex;
}
public int[] getQuestionPoints() {
return this.questionPoints;
}
public int getNumberQuestions() {
return this.numberQuestions;
}
public int getDuration() {
return this.duration;
}
public int getTotalPoints() {
return this.totalPoints;
}
public void setDuration(int duration) {
this.duration = duration;
}
public void setQuestionPoints(int[] questionPoints) {
this.questionPoints = questionPoints;
}
public void setQuestionsIndex(int[] questionsIndex) {
this.questionsIndex = questionsIndex;
}
public String getDeadline() {
return String.format("%d-%d-%d %d:%d",
this.deadline.getYear(), this.deadline.getMonthValue(),
this.deadline.getDayOfMonth(), this.deadline.getHour(),
this.deadline.getMinute());
}
public void setDeadline(LocalDateTime deadline) {
this.deadline = deadline;
}
/** Retrieve the largest quiz ID value
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/11/2022
*/
public int retrieveLargestID() {
String quizFile = tools.loadTextFile("Quiz.txt");
String[] lines = new String[0];
if (!quizFile.equals("")) {
lines = quizFile.split("/");
}
int largest = 0;
if (lines.length > 0) {
largest = Integer.parseInt(lines[lines.length - 1].split(":")[0]);
}
return largest;
}
/** Retrieve the questions from the text file and save it into a Question array
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/9/2022
*/
public Question[] retrieveQuestions() {
String questionFile = tools.loadTextFile("Questions.txt");
String[] lines = questionFile.split("/");
Question[] questions = new Question[lines.length];
for (int i = 0; i < lines.length; i++) {
String[] elements = lines[i].split(",");
String prompt = elements[0];
String answer = elements[1];
String questionType = elements[2];
Question question = new Question(prompt, answer, questionType);
questions[i] = question;
}
return questions;
}
/** Retrieve the questions for this specific quiz
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/10/2022
*/
public Question[] loadQuizQuestions() {
Question[] questionPool = retrieveQuestions();
int[] questionIndex = getQuestionsIndex();
int[] questionPoints = getQuestionPoints();
int numberQuestions = getNumberQuestions();
Question[] quizQuestions = new Question[numberQuestions];
for ( int i = 0 ; i < numberQuestions ; i++ ) {
quizQuestions[i] = questionPool[questionIndex[i]];
quizQuestions[i].setPointValue(questionPoints[i]);
}
return quizQuestions;
}
/** append this quiz to quiz text file
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/9/2022
*/
public void writeNewQuiz() {
try {
PrintWriter out = new PrintWriter(new FileOutputStream("Quiz.txt", true));
out.println(this);
out.close();
System.out.println("Quiz recorded!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("ERROR RECORDING QUIZ");
}
}
/** grade the quiz and save it out to grades text file
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/9/2022
*/
public float gradeQuiz( String[] responses ) {
Question[] questions = loadQuizQuestions();
int totalPointsEarned = 0;
for ( int i = 0 ; i < this.numberQuestions ; i++ ) {
int questionGrade = questions[i].gradeQuestion(responses[i]);
totalPointsEarned = totalPointsEarned + questionGrade;
}
float grade = ((float) totalPointsEarned / this.totalPoints) * 100;
System.out.printf("GRADE : %d/%d = %.2f\n", totalPointsEarned, this.totalPoints, grade);
return grade;
}
/** toString method
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/9/2022
*/
public String toString() {
String questionsIndexString = "";
String monthString;
String dayString;
if ( this.deadline.getMonthValue() < 10 ) {
monthString = String.format("0%d", this.deadline.getMonthValue());
} else {
monthString = String.valueOf(this.deadline.getMonthValue());
}
if ( this.deadline.getDayOfMonth() < 10 ) {
dayString = String.format("0%d", this.deadline.getDayOfMonth());
} else {
dayString = String.valueOf(this.deadline.getDayOfMonth());
}
for (int i = 0 ; i < this.numberQuestions - 1 ; i++ ) {
questionsIndexString = questionsIndexString + this.questionsIndex[i] + ",";
}
questionsIndexString = questionsIndexString + this.questionsIndex[this.numberQuestions - 1];
String hourString = "";
if (this.deadline.getHour() <10) {
hourString = "0" + this.deadline.getHour();
} else {
hourString = String.valueOf(this.deadline.getHour());
}
String deadlineString = String.format("%d-%s-%s-%s-%d", this.deadline.getYear(),monthString,
dayString, hourString, this.deadline.getMinute());
String questionPointsString = "";
for ( int i = 0 ; i < this.numberQuestions - 1 ; i++ ) {
questionPointsString = questionPointsString + this.questionPoints[i] + "," ;
}
questionPointsString = questionPointsString + this.questionPoints[this.numberQuestions - 1];
return String.format("%d:%d:%s:%s:%d:%d:%s/", this.quizID, this.numberQuestions, questionPointsString,
deadlineString, this.duration,
this.totalPoints, questionsIndexString);
}
}