-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExam.java
52 lines (51 loc) · 1.52 KB
/
Exam.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
import java.util.*;
import java.io.*;
import java.lang.*;
package Exam;
class Exam{
int duration, totalmarks;
boolean tofinish;
HashMap<String,String> choices;
public Exam(int d, int tot){
duration = d;
totalmarks = tot;
choices = new HashMap<String,String>();
tofinish = false;
}
public void takeExam() throws IOException,FileNotFoundException{
BufferedReader q = new BufferedReader(new InputStreamReader(new FileInputStream("questions.txt")));
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
String question = q.readLine().split("|")[0];
long startTime = System.nanoTime();
while(!question.equals("")){
System.out.println(question);
for(int i = 0; i < 4; i++){
String option = q.readLine();
System.out.println(option);
}
System.out.println("Your choice:");
choices.put(question,in.readLine());
tofinish = (System.nanoTime() - startTime >= duration);
if(tofinish){
System.out.println("Time's up!");
return;
}
question = q.readLine().split("|")[0];
}
}
public int calMarks() throws IOException,FileNotFoundException{
int sum = 0;
String answer;
BufferedReader q = new BufferedReader(new InputStreamReader(new FileInputStream("questions.txt")));
Set set = choices.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
answer = q.readLine().split("|")[1];
Map.Entry x = (Map.Entry)i.next();
if(x.getValue().equals(answer)){
sum = sum + 1;
}
}
return sum;
}
}