diff options
Diffstat (limited to 'c3')
| -rw-r--r-- | c3/Client.java | 91 | ||||
| -rw-r--r-- | c3/QuizTree.java | 170 | ||||
| -rw-r--r-- | c3/colors-cereals.txt | 9 | ||||
| -rwxr-xr-x | c3/compile.sh | 3 |
4 files changed, 273 insertions, 0 deletions
diff --git a/c3/Client.java b/c3/Client.java new file mode 100644 index 0000000..001616e --- /dev/null +++ b/c3/Client.java @@ -0,0 +1,91 @@ +import java.util.*; +import java.io.*; + +public class Client { + public static void main(String[] args) throws FileNotFoundException { + Scanner console = new Scanner(System.in); + + System.out.print("Enter quiz file to read: "); + String inFileName = console.nextLine(); + File inFile = new File(inFileName); + while (!inFile.exists()) { + System.out.println(" File does not exist. Please try again."); + System.out.print("Enter quiz file to read: "); + inFileName = console.nextLine(); + inFile = new File(inFileName); + } + + QuizTree quiz = new QuizTree(new Scanner(inFile)); + System.out.println("Quiz created!"); + System.out.println(); + + String option = ""; + while (!option.equalsIgnoreCase("quit")) { + option = menu(console); + System.out.println(); + + if (option.equalsIgnoreCase("take")) { + quiz.takeQuiz(console); + System.out.println(); + } else if (option.equalsIgnoreCase("creative")) { + // quiz.creativeExtension(); // TODO: Update with any parameters you need! + System.out.println(); + } else if (option.equalsIgnoreCase("export")) { + System.out.print("Enter file to export to: "); + String outFileName = console.nextLine(); + PrintStream outFile = new PrintStream(new File(outFileName)); + quiz.export(outFile); + System.out.println("Quiz exported!"); + System.out.println(); + } else if (option.equalsIgnoreCase("add")) { + addQ(console, quiz); + System.out.println(); + } else if (!option.equalsIgnoreCase("quit")) { + System.out.println(" Invalid choice. Please try again."); + System.out.println(); + } + } + } + + private static String menu(Scanner console) { + System.out.println("What would you like to do? Choose an option in brackets."); + System.out.println(" [take] quiz"); + System.out.println(" [add] question"); + System.out.println(" [export] quiz"); + System.out.println(" [creative] extension"); + System.out.println(" [quit] program"); + return console.nextLine(); + } + + private static void addQ(Scanner console, QuizTree quiz) { + System.out.print("Enter result to replace: "); + String toReplace = console.nextLine(); + + System.out.print("Enter left choice: "); + String leftChoice = console.nextLine(); + + System.out.print("Enter right choice: "); + String rightChoice = console.nextLine(); + + System.out.print("Enter score of choices: "); + String choiceScore = console.nextLine(); + + System.out.print("Enter left result: "); + String leftResult = console.nextLine(); + + System.out.print("Enter left score: "); + int leftScore = Integer.parseInt(console.nextLine()); + + System.out.print("Enter right result: "); + String rightResult = console.nextLine(); + + System.out.print("Enter right score: "); + int rightScore = Integer.parseInt(console.nextLine()); + + String choices = leftChoice + "/" + rightChoice + "-" + choiceScore; + leftResult = leftResult + "-" + leftScore; + rightResult = rightResult + "-" + rightScore; + quiz.addQuestion(toReplace, choices, leftResult, rightResult); + } +} + diff --git a/c3/QuizTree.java b/c3/QuizTree.java new file mode 100644 index 0000000..e8c16d2 --- /dev/null +++ b/c3/QuizTree.java @@ -0,0 +1,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; + } + } +} diff --git a/c3/colors-cereals.txt b/c3/colors-cereals.txt new file mode 100644 index 0000000..80aa285 --- /dev/null +++ b/c3/colors-cereals.txt @@ -0,0 +1,9 @@ +red/blue-0 +yellow/green-1 +END:Froot Loops-3 +END:Raisin Bran-5 +purple/orange-2 +END:Frosted Flakes-1 +black/white-3 +END:Rice Krispies-2 +END:Fruity Pebbles-4 diff --git a/c3/compile.sh b/c3/compile.sh new file mode 100755 index 0000000..577902d --- /dev/null +++ b/c3/compile.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +javac *.java && java Client ; rm *.class |
