-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogramfour.java
94 lines (79 loc) · 2.94 KB
/
programfour.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
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class programfour {
Scanner sc;
public String[] qns;
String[] option;
private char[] ans;
int qn = 5;
private final int TIME_LIMIT = 10;
public void quizfn() {
sc = new Scanner(System.in);
qns = new String[qn];
option = new String[qn];
ans = new char[qn];
QuizData();
}
private void QuizData() {
qns[0] = "Who is the CEO of BlackRock?";
qns[1] = "Which of these is a Indian Origin Language?";
qns[2] = "Which Country has reache on South Pole of Moon?";
qns[3] = "Who is the Indian President?";
qns[4] = "What is part of a database that holds only one type of information?";
option[0] = "A. Larry Fink\nB. Jeff Bezos\nC. Curtis Priem\nD. Ronald Wayne";
option[1] = "A. Spanish\nB. Bhojpuri\nC. Pastuni\nD. English";
option[2] = "A. India\nB. Russia\nC. America\nD. China";
option[3] = "A. Nirmala Sitaraman\nB. Rahul Gandhi\nC. Droupadi Murmu\nD. A.P.J. Abdul Kalam";
option[4] = "A. Report\nB. Field\nC. Record\nD. File";
ans[0] = 'A';
ans[1] = 'B';
ans[2] = 'A';
ans[3] = 'C';
ans[4] = 'B';
}
public void displayQuestion(int index) {
System.out.println("Question " + (index + 1) + ":\n" + qns[index] + "\n" + option[index]);
}
public char getAnswer(int index) {
return ans[index];
}
public void sQuiz() {
int score = 0, correct = 0, incorrect = 0;
for (int i = 0; i < qn; i++) {
displayQuestion(i);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Time is up!");
// incorrect++;
timer.cancel();
}
};
timer.schedule(task, TIME_LIMIT * 1000);
System.out.print("Enter your answer: ");
char answer = sc.next().toUpperCase().charAt(0);
task.cancel();
if (answer == getAnswer(i)) {
System.out.println("Correct!");
score += 10;
correct++;
} else {
System.out.println("Incorrect!");
incorrect++;
}
}
displayResult(score, correct, incorrect);
}
public void displayResult(int score, int correct, int incorrect) {
System.out.println("\nYour final score is: " + score);
System.out.println(
"You answered " + correct + " questions correctly and " + incorrect + " questions incorrectly.");
}
public static void main(String[] args) {
programfour quiz = new programfour();
quiz.quizfn();
quiz.sQuiz();
quiz.sc.close();
}
}