Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I just need help to get started. The first 7 codes are part of TreePackage the next two are part of StackPackage and the last

I just need help to get started. The first 7 codes are part of TreePackage the next two are part of StackPackage and the last one is GuessingGame.java Please help!!

Design a Guessing game where the computer tries to determine what type of animal the user is thinking of.

In Main.java, create a new instance of GuessingGame, passing in a root question and the two root answers.

private DecisionTreeInterface tree is the reference variable within GuessingGame that will refer to the decision tree.

GuessingGames constructor already creates a new instance of DecisionTree and sets tree to refer to it.

In GuessingGame.play(), the computer should ask the root question. When the user answers yes or no, call either tree.advanceToYes() or tree.advanceToNo() which should move the currentNode variable within the DecisionTree either left or right. Keep asking questions at each level until an answer node is reached (i.e. a leaf). Finally, guess the answer.

If the user says yes, print out I win

If the user says no, call the learn method.

The learn() method should ask the user what animal they were thinking of. Then it should ask the user to give a question for which the answer is yes for the animal the user was thinking of and no otherwise. It should then properly set the data

In DecisionTree, the following methods need to be implemented:

public void setCurrentData(String newData)

public void setResponses(String responseForNo,

String responseForYes)

public boolean isAnswer()

public void advanceToNo()

public void advanceToYes()

Remember that the DecisionTree object uses BinaryNode currentNode to move through the tree.

After each round, the computer should ask the user if they want to play again. When you play again, the tree should grow each time learn() is called. DO NOT stop running the program after each game as we are not backing up the data yet and your tree will reset each time.

package TreePackage; public interface TreeInterface { T getRootData(); int getHeight(); int getNumberOfNodes(); boolean isEmpty(); void clear(); }

package TreePackage; import java.util.Iterator; public interface TreeIteratorInterface { Iterator getPreorderIterator(); Iterator getPostorderIterator(); Iterator getInorderIterator(); Iterator getLevelOrderIterator(); }

package TreePackage; public interface BinaryTreeInterface extends TreeInterface, TreeIteratorInterface { /** Sets this binary tree to a new one-node binary tree * @param rootData The object that is the data for the new tree's root*/ void setTree(T rootData); /** Sets this binary tree to a new binary tree * @param rootData The object that is the data for the new tree's root * @param leftTree The left subtree of the new tree. * @param rightTree The right subtree of the new tree. */ void setTree(T rootData, BinaryTreeInterface leftTree, BinaryTreeInterface rightTree); }

package TreePackage; public interface DecisionTreeInterface extends BinaryTreeInterface { /** * Gets the data in the current node * @return The data object in the current node, or null if node is empty */ T getCurrentData(); /** * Sets the data in the current node * @param newData The new data object */ void setCurrentData(T newData); /** * Sets the data in the children of the current node, creating them if they do not exist * @param responseForNo The new data object for the left child * @param responseForYes The new data object for the right child */ void setResponses(T responseForNo, T responseForYes); /** * Sees whether the current node contains an answer * @return True if the current node is a leaf, false otherwise */ boolean isAnswer(); /** * Sets the current node to its left child, or null if the child doesn't exist */ void advanceToNo(); /** * Sets the current node to its right child, or null if the child doesn't exist */ void advanceToYes(); /** Sets the current node to the root of the tree. */ void resetCurrentNode(); }

package TreePackage; import java.util.Iterator; import java.util.NoSuchElementException; import StackPackage.*; public class BinaryTree implements BinaryTreeInterface { protected BinaryNode root; public BinaryTree() { root = null; } public BinaryTree(T rootData) { root = new BinaryNode<>(rootData); } public BinaryTree(T rootData, BinaryTree leftTree, BinaryTree rightTree) { initializeTree(rootData, leftTree, rightTree); } @Override public T getRootData() { if(!isEmpty()) { return root.getData(); } return null; } @Override public int getHeight() { return root.getHeight(); } @Override public int getNumberOfNodes() { return root.getNumberOfNodes(); } @Override public Iterator getPreorderIterator() { return null; } @Override public Iterator getPostorderIterator() { return null; } @Override public void clear() { root = null; } @Override public Iterator getInorderIterator() { return new InOrderIterator(); } @Override public Iterator getLevelOrderIterator() { return null; } @Override public void setTree(T rootData) { setTree(rootData, null, null); } @Override public void setTree(T rootData, BinaryTreeInterface leftTree, BinaryTreeInterface rightTree) { initializeTree(rootData, (BinaryTree) leftTree, (BinaryTree) rightTree); } private void initializeTree(T rootData, BinaryTree leftTree, BinaryTree rightTree) { root = new BinaryNode<>(rootData); if((leftTree != null) && !leftTree.isEmpty()) root.setLeftChild(leftTree.root); if((rightTree != null) && !rightTree.isEmpty()) { if (rightTree != leftTree) { root.setRightChild(rightTree.root); } else root.setRightChild(rightTree.root.copy()); } if((leftTree != null) && (leftTree != this)) { leftTree.clear(); } if((rightTree != null) && (rightTree != this)) { rightTree.clear(); } } @Override public boolean isEmpty() { return root == null; } // traversal that doesn't use an iterator (for demonstration purposes only) public void iterativeInorderTraverse() { StackInterface> nodeStack = new ArrayStack<>(); BinaryNode currentNode = root; while (!nodeStack.isEmpty() || (currentNode != null)) { while (currentNode != null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } if (!nodeStack.isEmpty()) { BinaryNode nextNode = nodeStack.pop(); System.out.println(nextNode.getData()); currentNode = nextNode.getRightChild(); } } } private class InOrderIterator implements Iterator { private StackInterface> nodeStack; private BinaryNode currentNode; public InOrderIterator() { nodeStack = new LinkedStack<>(); currentNode = root; } @Override public boolean hasNext() { return !nodeStack.isEmpty() || (currentNode != null); } @Override public T next() { BinaryNode nextNode = null; // find leftmost node with no left child while(currentNode != null) { nodeStack.push(currentNode); currentNode = currentNode.getLeftChild(); } if(!nodeStack.isEmpty()) { nextNode = nodeStack.pop(); currentNode = nextNode.getRightChild(); } else throw new NoSuchElementException(); return nextNode.getData(); } } }

package TreePackage; public class DecisionTree extends BinaryTree implements DecisionTreeInterface { BinaryNode currentNode; public DecisionTree(T data) { this(data, null, null); } public DecisionTree(T data, DecisionTree left, DecisionTree right) { setTree(data, left, right); } @Override public T getCurrentData() { if (currentNode!= null) { return currentNode.getData(); } return null; } @Override public void setCurrentData(T newData) { // TODO } @Override public void setResponses(T responseForNo, T responseForYes) { // TODO } @Override public boolean isAnswer() { // TODO return false; } @Override public void advanceToNo() { // TODO } @Override public void advanceToYes() { // TODO } @Override public void resetCurrentNode() { currentNode = root; } @Override public void setTree(T rootData) { super.setTree(rootData); currentNode = root; } @Override public void setTree(T rootData, BinaryTreeInterface leftTree, BinaryTreeInterface rightTree) { super.setTree(rootData, leftTree, rightTree); currentNode = root; } }

package TreePackage; class BinaryNode { private T data; private BinaryNode leftChild; private BinaryNode rightChild; public BinaryNode() { this(null); } public BinaryNode(T dataPortion) { this(dataPortion, null, null); } public BinaryNode(T dataPortion, BinaryNode newLeftChild, BinaryNode newRightChild) { data = dataPortion; leftChild = newLeftChild; rightChild = newRightChild; } public T getData() { return data; } public void setData(T newData) { data = newData; } public BinaryNode getLeftChild() { return leftChild; } public void setLeftChild(BinaryNode newLeftChild) { leftChild = newLeftChild; } public boolean hasLeftChild() { return leftChild != null; } public BinaryNode getRightChild() { return rightChild; } public void setRightChild(BinaryNode newRightChild) { rightChild = newRightChild; } public boolean hasRightChild() { return rightChild != null; } public boolean isLeaf() { return (leftChild == null) && (rightChild == null); } /** counts the nodes in the subtree rooted at this node * @return The number of nodes in the subtree rooted at this node */ public int getNumberOfNodes() { int leftNumber = 0; int rightNumber = 0; if (leftChild != null) { leftNumber = leftChild.getNumberOfNodes(); } if (rightChild != null) { rightNumber = rightChild.getNumberOfNodes(); } return 1 + leftNumber + rightNumber; } /** Computes the height of the subtree rooted at this node * @return The height of the subtree rooted at this node */ public int getHeight() { return getHeight(this); } private int getHeight(BinaryNode node) { int height = 0; if(node != null) { height = 1 + Math.max(getHeight(node.getLeftChild()), getHeight(node.getRightChild())); } return height; } /** Copies the subtree rooted at this node * @return The root of a copy of the subtree rooted at this node. */ public BinaryNode copy() { BinaryNode newRoot = new BinaryNode<>(data); if(leftChild != null) { newRoot.setLeftChild(leftChild.copy()); } if(rightChild != null) { newRoot.setRightChild(rightChild.copy()); } return newRoot; } }

package StackPackage; import java.util.Arrays; import java.util.EmptyStackException; public class ArrayStack implements StackInterface { private T[] stack; private int topIndex; // index of top entry private static final int DEFAULT_CAPACITY = 50; private static final int MAX_CAPACITY = 10000; public ArrayStack() { this(DEFAULT_CAPACITY); } public ArrayStack(int initialCapacity) { checkCapacity(initialCapacity); @SuppressWarnings("unchecked") T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; topIndex = -1; } @Override public T pop() { if(isEmpty()) { throw new EmptyStackException(); } else { T top = stack[topIndex]; stack[topIndex] = null; topIndex--; return top; } } @Override public void push(T newEntry) { ensureCapacity(); stack[topIndex + 1] = newEntry; topIndex ++; } @Override public T peek() { if(isEmpty()) { throw new EmptyStackException(); } else { return stack[topIndex]; } } @Override public boolean isEmpty() { return topIndex < 0; } @Override public void clear() { for(int i = 0; i <= topIndex; i++) { stack[i] = null; } topIndex = -1; } private void ensureCapacity() { if(topIndex == stack.length - 1) { int newLength = 2 * stack.length; checkCapacity(newLength); stack = Arrays.copyOf(stack, newLength); } } private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) { throw new IllegalStateException("Attempt to create a stack whose capacity exceeds allowed maximum"); } } @Override public char setResult(int result) { return 0; } @Override public int getResult() { return 0; } }

package StackPackage; public interface StackInterface { public void push(T newEntry); public T pop(); public T peek(); public boolean isEmpty(); public void clear(); public char setResult(int result); public int getResult(); }

import TreePackage.DecisionTreeInterface; import TreePackage.DecisionTree; public class GuessingGame { private DecisionTreeInterface tree; public GuessingGame(String question, String noAnswer, String yesAnswer) { DecisionTree no = new DecisionTree<>(noAnswer); DecisionTree yes = new DecisionTree<>(yesAnswer); tree = new DecisionTree<>(question, no, yes); } public void play() { // TODO } public void learn() { // TODO } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions

Question

7-16 Compare Web 2.0 and Web 3.0.

Answered: 1 week ago