-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapterQuiz.java
More file actions
56 lines (52 loc) · 1.79 KB
/
ChapterQuiz.java
File metadata and controls
56 lines (52 loc) · 1.79 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
import java.util.*;
public class ChapterQuiz {
String filename;
FileParser parser;
String [] words ;
String [] definitions ;
final int PAUSE = 1000; // milliseconds between word and definition
public static void main (String [] args) {
int ch = Integer.parseInt(args[0]);
popQuiz(ch);
}
public static void popQuiz (int chapter) {
String filename = "ch"+chapter+".tsv";
ChapterQuiz quiz = new ChapterQuiz (filename);
quiz.wordQuiz();
}
public ChapterQuiz (String filename) {
this.filename=filename;
this.parser = new FileParser (filename);
words = this.parser.getWords();
definitions = this.parser.getDefinitions();
if (parser.error()) {
System.exit(1);
}
}
public void wordQuiz () {
int numWords = words.length;
Random generator = new Random ();
String oneWord, oneDef, guess, response;
boolean more = true;
Scanner scanner = new Scanner (System.in);
System.out.println("Pop Quiz based on "+this.filename);
System.out.println("Type the word or X to exit.");
while (more) {
int index = generator.nextInt(numWords);
oneWord = words[index];
oneDef = definitions[index];
System.out.println("Definition: "+oneDef);
System.out.print("Word? ");
guess = scanner.next();
if (guess.toUpperCase().equals("X")) {
response="BYE";
more = false;
} else if (guess.equalsIgnoreCase(oneWord)) {
response="YES";
} else {
response="NOPE";
}
System.out.println(response+"! Answer was "+oneWord+"\n");
}
}
}