-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
275 lines (244 loc) · 9.01 KB
/
Copy pathQuestion.java
File metadata and controls
275 lines (244 loc) · 9.01 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
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
import java.io.*;
import java.util.*;
class Variable{
// general info
public String name;
public String varType;
// generated numbers uses these vars range = [floor,numCeil] \ noNos
public String floor; // least val allowed
public String ceil; // max val allowed
public String[] noNos; // numbers that break stuff 1/x -> x != 0
// equation
public String equation;
// given string of info
Variable(String info){
final String VARIABLE_INFO_SEPERATOR = "~";
String[] infoBits = info.split(VARIABLE_INFO_SEPERATOR);
this.name = infoBits[0];
this.varType = infoBits[1];
// dealing with different types of variables
switch(this.varType){
case "int":
case "dep":
this.setupNum(infoBits);
break;
case "equation":
this.setupEquation(infoBits);
break;
}
}
// given the stored var info
// sets up the number part of the variable
public void setupNum(String[] infoBits){
// getting the bounds
String[] bounds = infoBits[2].split("<");
this.floor = bounds[0];
this.ceil = bounds[1];
// getting the no no numbers
if(infoBits.length > 3){
String[] noNosInfo = infoBits[3].split(",");
int numNoNos = noNosInfo.length;
String[] noNos = new String[numNoNos];
for(int index = 0; index < numNoNos; index++){
noNos[index] = noNosInfo[index];
}
this.noNos = noNos;
}
}
// given the stored var info
// sets up the equation part of the variable
public void setupEquation(String[] infoBits){
// fixes all the equation shorthand
String equation = infoBits[2];
equation = equation.replace("m.", "Math.");
this.equation = equation;
}
@Override
public String toString(){
StringBuilder output = new StringBuilder();
output.append(name + " " + varType + " ");
switch(varType){
case "int":
case "dep":
output.append(floor + " ");
output.append(ceil + " ");
if(noNos != null){
for(String valueNoNo : noNos){
output.append(valueNoNo + " ");
}
}
break;
case "equation":
output.append(equation);
}
return output.toString().trim();
}
// given line of variables
// returns a variable array
public static Variable[] parseVariables(String info, String INFO_SEPERATOR){
// setting up variables
String[] variablesInfo = info.split(INFO_SEPERATOR);
int VarDepiables = variablesInfo.length;
Variable[] variables = new Variable[VarDepiables];
// setting up all the variables and adding them
for(int index = 0; index < VarDepiables; index++){
String variableInfo = variablesInfo[index];
Variable variable = new Variable(variableInfo);
variables[index] = variable;
}
return variables;
}
}
public class Question{
// general info
public String title;
public String type;
// question type equation
public String question;
public String hint;
public String reasoning;
public int precision;
public Variable[] variables;
public String[] equations;
// question type multyiple choice
public String answer;
public String[] choices;
// creates the reader for the a file
public static BufferedReader getReader(String folder, String fileName) throws FileNotFoundException{
final String MAIN_PATH = System.getProperty("user.dir");
File topicFile = new File(MAIN_PATH + folder + fileName + ".txt");
BufferedReader reader = new BufferedReader(new FileReader(topicFile));
return reader;
}
// returns a string array with all the choices
public static String[] getChoices(String choicesString) throws IOException{
// getting the file
final String CHOICES_FOLDER = "/choices/";
BufferedReader reader = getReader(CHOICES_FOLDER, choicesString);
// reading the file for the choices
ArrayList<String> choices = new ArrayList<String>();
String line;
while((line = reader.readLine()) != null){
choices.add(line.trim());
}
reader.close();
return choices.toArray(new String[0]);
}
// given the field it fills info is formatted and put into its respective field
public void addInfo(String field, String info) throws IOException{
switch(field){
//basic parts
case "title":
this.title = info;
break;
case "type":
this.type = info;
break;
case "question":
this.question = info;
break;
//equation parts
case "hint":
this.hint = info;
break;
case "reasoning":
this.reasoning = info;
break;
case "precision":
this.precision = Integer.parseInt(info);
break;
case "variables":
final String INFO_SEPERATOR = " ";
this.variables = Variable.parseVariables(info, INFO_SEPERATOR);
break;
//multiple choice
case "answer":
this.answer = info;
break;
case "choices":
this.choices = getChoices(info);
break;
}
}
public static Question[] readFile(BufferedReader reader) throws IOException{
ArrayList<Question> questions = new ArrayList<Question>();
String line;
Question currentQuestion = null;
while((line = reader.readLine()) != null){
// skipping empty lines
if(line.trim().isEmpty()){
continue;
}
// formatting the line
final String FIELD_SEPERATOR = ":";
String[] split = line.split(FIELD_SEPERATOR,2);
String field = split[0].toLowerCase();
if(field.equals("title")){// title has be first for this to work
currentQuestion = new Question();
questions.add(currentQuestion);
}
String info = "";
if(split.length == 2){
info = split[1].trim();
}
// adding the info to the current question
currentQuestion.addInfo(field, info);
}
return questions.toArray(new Question[0]);
}
// gets all the questions in a topic
public static Question[] getTopicQuestions(String topic) throws IOException{
// getting the doc
final String TOPIC_FOLDER = "/topics/";
BufferedReader reader = getReader(TOPIC_FOLDER, topic);
// reading from the file and setting up all the questions
Question[] questions = readFile(reader);
reader.close();
return questions;
}
public static void testAllTopics() throws IOException{
// getting the path
String mainPath = System.getProperty("user.dir");
final String FOLDER_STRING = "/topics/";
File[] topics = new File(mainPath + FOLDER_STRING).listFiles();
for(File topic: topics){
String topicName = topic.getName().replace(".txt", "");
System.out.println("\n\nTopic: " + topicName + "\n");
Question[] questions = getTopicQuestions(topicName);
for(Question question: questions)
System.out.println(question);
}
}
@Override
public String toString(){
StringBuilder output = new StringBuilder();
output.append("Title: " + this.title + "\n");
output.append("Question: " + this.question + "\n");
// printing important info based on the type of question
switch(this.type){
case "equation":
output.append("Hint: " + this.hint + "\n");
output.append("Reasoning: " + this.reasoning + "\n");
output.append("Precision: " + this.precision + "\n");
if(this.variables != null){
output.append("Variables: " + "\n");
for(Variable var: this.variables){
output.append(" " + var.toString() + "\n");
}
}
break;
case "multiple choice":
output.append("Answer: " + this.answer + "\n");
if(this.choices != null){
output.append("Choices: " + "\n");
for(String choice: this.choices){
output.append(" " + choice + "\n");
}
}
}
return output.toString();
}
public static void main(String[] args) throws IOException{
testAllTopics();
}
}