-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuestionPaper.java
More file actions
171 lines (138 loc) · 4.49 KB
/
Copy pathQuestionPaper.java
File metadata and controls
171 lines (138 loc) · 4.49 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
import java.io.Serializable;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
public class QuestionPaper extends Question implements Serializable {
private boolean isActive = true;
private StringBuffer paperName;
private Vector<Question> setOfQuestions = new Vector<Question>();
// Default Constructor
public QuestionPaper() {
}
// set Paper using Interactive way
public QuestionPaper(String paperName) {
this.paperName = new StringBuffer(paperName);
setQuestion();
}
// Parameterized Constructor accepting array of Questions
public QuestionPaper(String paperName, Question[] questions) {
this.paperName = new StringBuffer(paperName);
for (Question question : questions) {
setOfQuestions.add(question);
}
}
// Prints Question Paper Directly
@Override
public String toString() {
StringBuffer str = new StringBuffer(paperName + "\n\n");
for (int i = 0; i < setOfQuestions.size(); i++) {
str.append("Que " + (i + 1) + ". " + setOfQuestions.get(i) + "\n");
}
return str.toString();
}
// gets Question Paper Name
public String getQuestionPaperName() {
return paperName.toString();
}
// is Paper to be shown or not
public boolean isActive() {
return isActive;
}
// gets size of Paper in form of total Questions
public int getQuestionPaperSize() {
return setOfQuestions.size();
}
// Sets Question manually with method
void setQuestion() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of questions you want to add: ");
int size = sc.nextInt();
for (int i = 0; i < size; i++) {
System.out.println("Question: " + (i + 1));
addQuestion();
}
}
// gets particular Question of Paper
Question getQuestion(int index) {
return setOfQuestions.get(index);
}
// Adds Question by array of question
void addQuestions(Question[] questions) {
for (Question question : questions)
setOfQuestions.add(question);
}
// Adds Question individually
void addQuestion(Question question) {
setOfQuestions.add(question);
}
// Adds Question interactively
void addQuestion() {
Scanner sc = new Scanner(System.in);
String question;
int sizeOfOptions;
String[] options;
int correctChoice;
System.out.println("Enter question: ");
question = new String(sc.nextLine());
System.out.println("Enter number of options for question: ");
sizeOfOptions = sc.nextInt();
options = new String[sizeOfOptions];
System.out.println("Enter options: ");
sc.skip("[\\r\\n]+");
for (int j = 0; j < sizeOfOptions; j++) {
options[j] = new String(sc.nextLine());
}
System.out.println("Enter which option is correct: ");
correctChoice = sc.nextInt();
addQuestion(new Question(question, options, correctChoice));
}
// Changes question by user requirement
// Under Construction
void changeQuestion(Question question) {
String ques = null;
String[] options = null;
int correctChoice = 0;
question.modifyQuestion(ques, options, correctChoice);
}
// Removes Question by index
// Under Construction
void removeQuestion(int index) {
setOfQuestions.remove(index - 1);
}
// shuffle Questions of Paper
void shufflePaper() {
Random rand = new Random();
Vector<Integer> questionVector = new Vector<Integer>();
QuestionPaper questionPaper = new QuestionPaper();
do {
int paperIndex = rand.nextInt(setOfQuestions.size());
if (!questionVector.contains(paperIndex)) {
questionVector.add(paperIndex);
}
} while (questionVector.size() < setOfQuestions.size());
for (int questionIndex : questionVector) {
questionPaper.addQuestion(setOfQuestions.get(questionIndex));
}
this.setOfQuestions = questionPaper.setOfQuestions;
}
// to take and verify the answers
int checkPaper(Vector<Integer> answers) {
int marks = 0;
for (int i = 0; i < setOfQuestions.size(); i++) {
if (setOfQuestions.get(i).getCorrectChoice() == answers.get(i))
marks++;
}
return marks;
}
public static void main(String[] args) {
// String[] options = { "Charles Babbage", "Dennis Ritchie", "James Gosling" };
//
// Question[] obj = new Question[2];
// obj[0] = new Question("Who was the founder of JAVA1 language ?", options);
// obj[1] = new Question("Who was the founder of JAVA2 language ?", options);
//
// QuestionPaper exm = new QuestionPaper(obj);
QuestionPaper exm = new QuestionPaper("General Questions!");
System.out.println(exm);
}
}