-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateTest.java
254 lines (211 loc) · 10 KB
/
GenerateTest.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
/*
* 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.
*/
/**
*
* @author abhiram
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class GenerateTest extends java.awt.Frame {
/**
* Creates new form GenerateTest
*/
public GenerateTest() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnGenerate = new java.awt.Button();
lblNumberOfQuest = new java.awt.Label();
txtNumberOfQuest = new java.awt.TextField();
txtQuestions = new java.awt.TextArea();
btnBack = new java.awt.Button();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
setLayout(new FlowLayout());
setSize(1000,1000);
btnGenerate.setLabel("Generate Test");
add(btnGenerate);
btnGenerate.setBounds(350, 80, 100, 30);
btnGenerate.addActionListener(new ButtonClickListener());
btnBack.setLabel("back");
btnBack.setActionCommand("back");
btnBack.setBounds(60,400,60,30);
add(btnBack);
btnBack.addActionListener(new ButtonClickListener());
lblNumberOfQuest.setText("No. of Questions");
add(lblNumberOfQuest);
lblNumberOfQuest.setBounds(50, 80, 100, 30);
txtNumberOfQuest.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNumberOfQuestActionPerformed(evt);
}
});
add(txtNumberOfQuest);
txtNumberOfQuest.setBounds(200, 80, 100, 30);
add(txtQuestions);
txtQuestions.setBounds(30, 180, 310, 200);
setLayout(null);
}// </editor-fold>
/**
* Exit the Application
*/
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
private void txtNumberOfQuestActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GenerateTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Button btnGenerate;
private java.awt.Label lblNumberOfQuest;
private java.awt.TextField txtNumberOfQuest;
private java.awt.TextArea txtQuestions;
private java.awt.Button btnBack;
String filenameQuest ;
String filenameAns ;
String fileNameQuestGen;
String fileNameAnsGen;
// End of variables declaration
public class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if(command.equals("back")){
btnBack.getParent().setVisible(false);
new PhysicsHomePage().setVisible(true);
}
else{
if(Subject.Subject.equals("phy")){
filenameQuest = ("PhysicsQuestions.txt");
filenameAns = ("PhysicsAnswers.txt");
}
if(Subject.Subject.equals("chem")){
filenameQuest = ("ChemistryQuestions.txt");
filenameAns = ("ChemAnswers.txt");
}
try{
String strQuestforArray;
String strAnsforArray;
String newQuestion;
String newAnswer;
int i = 0;
FileReader fileReaderQuest = new FileReader(filenameQuest);
BufferedReader bufferedReaderQuest = new BufferedReader(fileReaderQuest);
FileReader fileReaderAns = new FileReader(filenameAns);
BufferedReader bufferedReaderAns = new BufferedReader(fileReaderAns);
ArrayList<String> StringOfQuestions = new ArrayList<String>();
ArrayList<String> StringOfAnswers = new ArrayList<String>();
ArrayList<String> listAllQuestion = new ArrayList<String>();
ArrayList<String> listAllAnswer = new ArrayList<String>();
ArrayList<String> listGeneratedQuestions = new ArrayList<String>();
ArrayList<String> listIndex = new ArrayList<String>();
while((strQuestforArray = bufferedReaderQuest.readLine())!=null){
StringOfQuestions.add(strQuestforArray);
//System.out.println(strQuestforArray);
}
while((strAnsforArray=bufferedReaderAns.readLine())!=null){
StringOfAnswers.add(strAnsforArray);
//System.out.println(strAnsforArray);
}
fileNameQuestGen = ("QuestionGenerated.txt");
fileNameAnsGen = ("AnswersGenerated.txt");
FileWriter fileWriterQuestGen = new FileWriter(fileNameQuestGen,false);
BufferedWriter bufferedWriterQuestGen = new BufferedWriter(fileWriterQuestGen);
FileWriter fileWriterAnsGen = new FileWriter(fileNameAnsGen,false);
BufferedWriter bufferedWriterAnsGen = new BufferedWriter(fileWriterAnsGen);
for(String strQuest : StringOfQuestions){
String[] parts = strQuest.split("\\.",2);
newQuestion = parts[1];
int lastQuestionNumber = Integer.parseInt(parts[0]);
while(i!=lastQuestionNumber){
listAllQuestion.add(i,newQuestion);
listIndex.add(i,parts[0]);
//System.out.println(newQuestion);
i++;
}
}
i=0;
for(String strAns : StringOfAnswers){
String[] parts = strAns.split("\\.",2);
newAnswer = parts[1];
int lastQuestionNumber = Integer.parseInt(parts[0]);
while(i!=lastQuestionNumber){
listAllAnswer.add(i,newAnswer);
//System.out.println(newQuestion);
i++;
}
}
//Collections.shuffle(listAllQuestion);
Collections.shuffle(listIndex);
int j = 0;
int numOfQuestions = Integer.parseInt(txtNumberOfQuest.getText().toString());
while(j!=numOfQuestions){
String strshuffledIndex = listIndex.get(j);
int intShuffledIndex = Integer.parseInt(strshuffledIndex);
System.out.println(listAllQuestion.get(intShuffledIndex-1));
txtQuestions.append(listAllQuestion.get(intShuffledIndex-1) + "\n");
bufferedWriterQuestGen.write(listAllQuestion.get(intShuffledIndex-1));
bufferedWriterQuestGen.newLine();
bufferedWriterQuestGen.flush();
/*System.out.println(listAllQuestion.get(j));
txtQuestions.append(listAllQuestion.get(j) + "\n");
bufferedWriterQuestGen.write(listAllQuestion.get(j));*/
//bufferedWriterQuestGen.newLine();
//bufferedWriterQuestGen.flush();
j++;
}
j = 0;
int numOfAnswers = Integer.parseInt(txtNumberOfQuest.getText().toString());
while(j!=numOfAnswers){
String strshuffledIndex = listIndex.get(j);
int intShuffledIndex = Integer.parseInt(strshuffledIndex);
System.out.println(listAllAnswer.get(intShuffledIndex-1));
//txtQuestions.append(listAllAnswer.get(intShuffledIndex-1) + "\n");
bufferedWriterAnsGen.write(listAllAnswer.get(intShuffledIndex-1));
bufferedWriterAnsGen.newLine();
bufferedWriterAnsGen.flush();
/*System.out.println(listAllQuestion.get(j));
txtQuestions.append(listAllQuestion.get(j) + "\n");
bufferedWriterQuestGen.write(listAllQuestion.get(j));*/
//bufferedWriterQuestGen.newLine();
//bufferedWriterQuestGen.flush();
j++;
}
bufferedWriterQuestGen.close();
}
catch(IndexOutOfBoundsException iobe){
System.out.println("Question list is empty");
}
catch(NumberFormatException nfe){
System.out.println("number format exception");
}
catch(IOException ioe){
System.out.println("IO Exception");
}
}
}
}
}