complete the Java code based on the template below.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
complete the Java code based on the template below. The images attached is the project description and sample!
Quiz Tree code template:
import java.io.PrintStream;
import java.util.Scanner;
public class QuizTree {
QuizTreeNode root;
public QuizTree(Scanner inputFile) {
root = read(inputFile, root);
}
private QuizTreeNode read(Scanner input, QuizTreeNode node) {
if (!input.hasNext()) return node;
String line = input.nextLine();
/**
* 1. set node to a new node
* 2. check if the line is the leaf => return the node
* 3. otherwise, set node left = read (input, node left)
* set node right = read(input, node right)
* return node
*/
return node;
}
public void takeQuiz(Scanner console) {
takeQuiz(console, root);
}
private void takeQuiz(Scanner console, QuizTreeNode node) {
/**
* if the node is a leaf (both children are null) => display the value and return
* Split the node value into two values (e.g. blue and green)
* prompt the user using nodes' value
* get the response from the user and determine is that left or right
* if the response equals left call takeQuiz(console, node.left)
* else if the response equals right call takeQuiz (console, node.right)
* else prompt the user 'Invalid response; try again.'
*/
}
public void export() {
export(root);
}
private void export(QuizTreeNode node) {
/**
* display the bst data to the console using System.out.println()
* if node is null return
* if node is a leaf print "END:" + leaf value
* else print leaf value
* call export(node left)
* call export(node right)
*/
}
public void addQuestion(String toReplace, String leftChoice, String rightChoice, String leftResult, String rightResult) {
/**
* 1. use the search function to search for the item
* 2. Concat the left choice and right choice into a single new value
* 3. update the node value (toReplace) with the new value
* 4. Add a left node with information from the leftResult
* 5. Add a right node with the information from the rightResult
*/
}
public QuizTreeNode search(String value) {
return search(value, root);
}
private QuizTreeNode search(String value, QuizTreeNode node) {
/**
* if (node is null ) return null;
* if (node value is the same as the value)
* node.left = call itself (value, node.left)
* node.right = call itself (value, node.right);
*/
return node;
}
static class QuizTreeNode {
public String value;
QuizTreeNode left;
QuizTreeNode right;
public QuizTreeNode(String value) {
if (value.startsWith("END:")) {
value = value.substring("END:".length());
}
this.value = value;
}
public boolean isLeaf() { return left == null && right == null; }
@Override
public String toString() {
return "QuizTreeNode{" +
"value='" + value + '\'' +
", left=" + left +
", right=" + right +
'}';
}
}
}
import java.util.Scanner;
public class QuizTree {
QuizTreeNode root;
public QuizTree(Scanner inputFile) {
root = read(inputFile, root);
}
private QuizTreeNode read(Scanner input, QuizTreeNode node) {
if (!input.hasNext()) return node;
String line = input.nextLine();
/**
* 1. set node to a new node
* 2. check if the line is the leaf => return the node
* 3. otherwise, set node left = read (input, node left)
* set node right = read(input, node right)
* return node
*/
return node;
}
public void takeQuiz(Scanner console) {
takeQuiz(console, root);
}
private void takeQuiz(Scanner console, QuizTreeNode node) {
/**
* if the node is a leaf (both children are null) => display the value and return
* Split the node value into two values (e.g. blue and green)
* prompt the user using nodes' value
* get the response from the user and determine is that left or right
* if the response equals left call takeQuiz(console, node.left)
* else if the response equals right call takeQuiz (console, node.right)
* else prompt the user 'Invalid response; try again.'
*/
}
public void export() {
export(root);
}
private void export(QuizTreeNode node) {
/**
* display the bst data to the console using System.out.println()
* if node is null return
* if node is a leaf print "END:" + leaf value
* else print leaf value
* call export(node left)
* call export(node right)
*/
}
public void addQuestion(String toReplace, String leftChoice, String rightChoice, String leftResult, String rightResult) {
/**
* 1. use the search function to search for the item
* 2. Concat the left choice and right choice into a single new value
* 3. update the node value (toReplace) with the new value
* 4. Add a left node with information from the leftResult
* 5. Add a right node with the information from the rightResult
*/
}
public QuizTreeNode search(String value) {
return search(value, root);
}
private QuizTreeNode search(String value, QuizTreeNode node) {
/**
* if (node is null ) return null;
* if (node value is the same as the value)
* node.left = call itself (value, node.left)
* node.right = call itself (value, node.right);
*/
return node;
}
static class QuizTreeNode {
public String value;
QuizTreeNode left;
QuizTreeNode right;
public QuizTreeNode(String value) {
if (value.startsWith("END:")) {
value = value.substring("END:".length());
}
this.value = value;
}
public boolean isLeaf() { return left == null && right == null; }
@Override
public String toString() {
return "QuizTreeNode{" +
"value='" + value + '\'' +
", left=" + left +
", right=" + right +
'}';
}
}
}
Quiz Client Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class QuizClient {
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("add")) {
addQ(console, quiz);
} else if (option.equalsIgnoreCase("export")) {
quiz.export();
System.out.println("Quiz exported!");
System.out.println();
} else if (!option.equalsIgnoreCase("quit")) {
System.out.println(" Invalid choice. Please try again.");
}
}
}
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(" [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 left result: ");
String leftResult = console.nextLine();
System.out.print("Enter right result: ");
String rightResult = console.nextLine();
quiz.addQuestion(toReplace, leftChoice, rightChoice, leftResult, rightResult);
}
}
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class QuizClient {
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("add")) {
addQ(console, quiz);
} else if (option.equalsIgnoreCase("export")) {
quiz.export();
System.out.println("Quiz exported!");
System.out.println();
} else if (!option.equalsIgnoreCase("quit")) {
System.out.println(" Invalid choice. Please try again.");
}
}
}
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(" [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 left result: ");
String leftResult = console.nextLine();
System.out.print("Enter right result: ");
String rightResult = console.nextLine();
quiz.addQuestion(toReplace, leftChoice, rightChoice, leftResult, rightResult);
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education