-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionBank.java
292 lines (189 loc) · 6.55 KB
/
QuestionBank.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
/**
*
* @author shammaaas
*/
import java.io.*;
public class QuestionBank{
private String courseName;
private int nOfQ;
private Question[] qList;
public QuestionBank(String courseName){
this.courseName=courseName;
nOfQ=0;
qList =new Question[100];
}
public String getCourseName(){
return courseName;
}
public void setCourseName(String courseName){
this.courseName=courseName;}
public Question[] getqList(){
return qList;
}
public void setqList(Question[] qList){
this.qList=qList;
}
public int getnOfQ(){
return nOfQ;
}
public void setnOfQ(int nOfQ){
this.nOfQ=nOfQ;}
public int findQuestion(Question q){// satrt of findQuestion*
int i=0;
for(i=0;i<nOfQ;i++){
if(qList[i].getText().equals(q.getText())){// add to lower case
return i;}
if(qList[i].getqID().equals(q.getqID())){
return i;
}
}
return -1;
}// end of findQuestion
public boolean addQuestion(Question q){// start of addQuestion
if(nOfQ==qList.length){// added
return false;
}
if(findQuestion(q) == -1){
if( q instanceof FillBlankQ)
qList[nOfQ++]=new FillBlankQ((FillBlankQ)q);
if(q instanceof MCQ)
qList[nOfQ++]=new MCQ((MCQ)q);
if(q instanceof TrueFalseQ)
qList[nOfQ++]=new TrueFalseQ((TrueFalseQ)q);
return true;}
return false;
}// end of addQuestion
public Question[] getRandomQuestions(int n) {
if (n <= 0 || n > nOfQ) {
throw new IllegalArgumentException("The required number of questions is invalid ");
}
Question[] randomQuestions = new Question[n];
/*
1- loop n times
2- generate random index in the correct range (0 to nOfQ-1) --> no duplicates
3- pick up the question qList[randomIndex] and store it in the randomQuestion array
*/
int min = 0;
int max = nOfQ - 1;
int[] indices = new int[n];
int counter = 0;
boolean repeate = true;
for (int i = 0; i < n; i++) {
int randomIndex = 0;//dummy value
while (repeate) {
randomIndex = min + (int) (Math.random() * (max - min + 1));
boolean isFound = false;
for (int j = 0; j < counter; j++) {
if (indices[j] == randomIndex) {
isFound = true;
break;
}
}//end for
if (!isFound) {
indices[counter++] = randomIndex;
repeate = false;
}
}
repeate = true;//reset
/* randomQuestions[i] = qList[randomIndex]; //--> aggregation */
if (qList[randomIndex] instanceof FillBlankQ) {
randomQuestions[i] = new FillBlankQ(qList[randomIndex]);
} else if (qList[randomIndex] instanceof MCQ) {
randomQuestions[i] = new MCQ(qList[randomIndex]);
} else {
randomQuestions[i] = new TrueFalseQ(qList[randomIndex]);
}
}//end for
return randomQuestions;
}//end getRandomQuestions
public int countChapterQuestions(String cName){//start of countChapterQuestion**
cName = cName.toLowerCase();
int num = 0;
for (int i = 0; i < nOfQ; i++) {
String ID = qList[i].getqID();
String ch = ID.substring(0, ID.indexOf('_')).toLowerCase();
if (ch.equals(cName)) {
num++;
}
}
return num;
}
public void removeAllQuestion(String ids[]){// start of removeAllQuestion
for (String id : ids) {
for (int i = 0; i < nOfQ; i++) {
if (qList[i].qID.toLowerCase().equals(id.toLowerCase())) {
for (int j = i; j < nOfQ - 1; j++) {
qList[j] = qList[j + 1];
}
qList[--nOfQ] = null;
break;
}
}
}
} //end of removeAllQuestion
/*for(int i=0;i<nOfQ;i++)
if(ids[i].equals(qList[i].getqID()))
ids[i]=ids[i+1];
ids[--nOfQ]=null;*/
//
public int loadQuestions(String fname)throws IOException , FileNotFoundException, ClassNotFoundException{// start of loadQuestions
if (!(fname.substring(0, fname.indexOf('.')).toLowerCase().equals(courseName.toLowerCase()))) {
throw new IllegalArgumentException("File Name does't match the course !");
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname));
int num = 0;
while (true) {
try {
addQuestion((Question) ois.readObject());
num++;
} catch (EOFException ex) {
System.out.println(num + " questions have been loadded successfully !");
break;
}
}
ois.close();
return num;
}// end of loadQuestions
public void saveQuestions() throws IOException, FileNotFoundException {// start of saveQuestions
ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream(courseName.toLowerCase() + ".txt"));
try {
for (int i=0;i<nOfQ;i++)
ostream.writeObject(qList[i].formattedQwithA());
}finally{
ostream.close();
}
System.out.println(nOfQ+" questions saved successfully :)!");//
} // end of saveQuestions
public Question[] getChapterQuestions(int n, String cName) {//start of getChapterQuestions
int counter = countChapterQuestions(cName);// or
/* for (int i = 0; i < nOfQ; i++) {
if (qList[i].qID.substring(0, qList[i].qID.indexOf('_')).toLowerCase().equals(cName.toLowerCase())) {
count++;
}
}*/
int size = 0;
if(counter < n)
size=counter;
else
size=n;
Question[] arrayQuestions = new Question[size];
for (int i = 0, j = 0; i < nOfQ && j<size ; i++) {
if (qList[i].qID.substring(0, qList[i].qID.indexOf('_')).toLowerCase().equals(cName.toLowerCase())) {
if (qList[i] instanceof FillBlankQ) {
arrayQuestions[j++] = new FillBlankQ(qList[i]);}
else if (qList[i] instanceof MCQ) {
arrayQuestions[j++] = new MCQ(qList[i]);}
else {
arrayQuestions[j++] = new TrueFalseQ(qList[i]);
}
}
}
return arrayQuestions;
}//end of getChapterQuestions
}