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
|
import java.util.*;
import java.io.*;
public class QuizTree {
private QuizTreeNode head;
// PROVIDED
// Returns the given percent rounded to two decimal places.
private double roundTwoPlaces(double percent) {
return (double) Math.round(percent * 100) / 100;
}
public int size() {
return size(head);
}
private int size(QuizTreeNode root) {
if (root == null) {
return 0;
}
return 1 + size(root.left) + size(root.right);
}
public QuizTree(Scanner inputFile) {
this.head = null;
this.head = populateTree(inputFile, head);
}
private void printTree(QuizTreeNode root) {
if (root != null) {
System.out.println(root.data);
printTree(root.left);
printTree(root.right);
}
}
private QuizTreeNode populateTree(Scanner inputFile, QuizTreeNode root) {
if (inputFile.hasNextLine()) {
String next = inputFile.nextLine();
String data;
int score;
// we've reached a result, make a node but dont recurse
if (next.startsWith("END:")) {
data = next.substring(4, next.lastIndexOf("-"));
score = Integer.parseInt(next.substring(next.lastIndexOf("-") + 1));
return new QuizTreeNode(data, score);
}
// otherwise, we're not at the end yet, make a node and keep going
else {
data = next.substring(0, next.lastIndexOf("-"));
score = Integer.parseInt(next.substring(next.lastIndexOf("-") + 1));
root = new QuizTreeNode(data, score);
root.left = populateTree(inputFile, root.left);
root.right = populateTree(inputFile, root.right);
}
}
return root;
}
public void takeQuiz(Scanner console) {
takeQuiz(console, head);
}
private void takeQuiz(Scanner console, QuizTreeNode root) {
if (root.left != null && root.right != null) {
String left = root.data.substring(0, root.data.lastIndexOf("/"));
String right = root.data.substring(root.data.lastIndexOf("/")+1, root.data.length());
System.out.print("Do you prefer " + left + " or " + right + "? ");
String input = console.nextLine();
while (!input.equalsIgnoreCase(left) || !input.equalsIgnoreCase(right)) {
System.out.println(" Invalid response; try again.");
System.out.print("Do you prefer " + left + " or " + right + "? ");
input = console.nextLine();
}
takeQuiz(console, input.equalsIgnoreCase(left) ? root.left : root.right);
}
else {
System.out.println("Your result is: " + root.data);
System.out.println("Your score is: " + root.score);
}
}
public void export(PrintStream outputFile) {
export(outputFile, this.head);
}
private void export(PrintStream outputFile, QuizTreeNode root) {
if (root.left == null && root.right == null) {
outputFile.println("END:" + root.data + "-" + root.score);
} else {
outputFile.println(root.data + "-" + root.score);
}
if (root.left != null) {
export(outputFile, root.left);
}
if (root.right != null) {
export(outputFile, root.right);
}
}
//public void addQuestion(String toReplace, String choices, String leftResult, String rightResult) {
//}
public void addQuestion(String toReplace, String choices, String leftResult, String rightResult) {
head = addQuestion(head, toReplace.toLowerCase(), choices, leftResult, rightResult);
}
private QuizTreeNode addQuestion(QuizTreeNode root, String toReplace, String choices, String leftResult, String rightResult) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null && root.data.equalsIgnoreCase(toReplace)) {
// Parse choices to get left choice, right choice, and score
String choiceText = choices.substring(0, choices.lastIndexOf("-"));
int choiceScore = Integer.parseInt(choices.substring(choices.lastIndexOf("-") + 1));
// Create the new choice node
QuizTreeNode choiceNode = new QuizTreeNode(choiceText, choiceScore);
// Parse leftResult to get left result data and score
String leftData = leftResult.substring(0, leftResult.lastIndexOf("-"));
int leftScore = Integer.parseInt(leftResult.substring(leftResult.lastIndexOf("-") + 1));
QuizTreeNode leftNode = new QuizTreeNode(leftData, leftScore);
// Parse rightResult to get right result data and score
String rightData = rightResult.substring(0, rightResult.lastIndexOf("-"));
int rightScore = Integer.parseInt(rightResult.substring(rightResult.lastIndexOf("-") + 1));
QuizTreeNode rightNode = new QuizTreeNode(rightData, rightScore);
// Set the left and right children of the new choice node
choiceNode.left = leftNode;
choiceNode.right = rightNode;
return choiceNode;
} else {
root.left = addQuestion(root.left, toReplace, choices, leftResult, rightResult);
root.right = addQuestion(root.right, toReplace, choices, leftResult, rightResult);
return root;
}
}
public static class QuizTreeNode {
public final String data;
public final int score;
public QuizTreeNode left;
public QuizTreeNode right;
public QuizTreeNode(String data, int score) {
this(data, score, null, null);
}
public QuizTreeNode(String data, int score, QuizTreeNode left, QuizTreeNode right) {
this.data = data;
this.score = score;
this.left = left;
this.right = right;
}
}
}
|