Revise the animal-guessing program below so that the initial knowledge tree is o
ID: 3547820 • Letter: R
Question
Revise the animal-guessing program below so that the initial knowledge tree is
obtained by reading information froma file. Also, when the program ends, the knowledge tree at that point is
written to the same file. You must carefully specify the format of the data in this file. The format should make it easy to do two things: (a) read the file and set the initial tree; and (b) write the knowledge tree to the file, using some kind of traversal.
// FILE: AnimalGuess.java // This animal-guessing program illustrates the use of the binary tree node class. import edu.colorado.nodes.BTNode; import java.util.Scanner; /****************************************************************************** * The <CODE>AnimalGuess</CODE> Java application illustrates the use of * the binary tree node class is a small animal-guessing game. * * <p><dt><b>Java Source Code for this class:</b><dd> * <A href="../applications/Animals.java"> * http://www.cs.colorado.edu/~main/applications/Animals.java * </A> * * @author Michael Main * <A href="mailto:main@colorado.edu"> (main@colorado.edu) </A> * * @version * Jul 22, 2005 ******************************************************************************/ public class AnimalGuess { private static Scanner stdin = new Scanner(System.in); /** * The main method prints instructions and repeatedly plays the * animal-guessing game. As the game is played, the taxonomy tree * grows by learning new animals. The <CODE>String</CODE> argument * (<CODE>args</CODE>) is not used in this implementation. **/ public static void main(String[ ] args) { BTNode<String> root; instruct( ); root = beginningTree( ); do play(root); while (query("Shall we play again?")); System.out.println("Thanks for teaching me a thing or two."); } /** * Print instructions for the animal-guessing game. **/ public static void instruct( ) { System.out.println("Please think of an animal."); System.out.println("I will ask some yes/no questions to try to figure"); System.out.println("out what you are."); } /** * Play one round of the animal guessing game. * @param <CODE>current</CODE> * a reference to the root node of a binary taxonomy tree that will be * used to play the game. * <dt><b>Postcondition:</b><dd> * The method has played one round of the game, and possibly * added new information about a new animal. * @exception java.lang.OutOfMemoryError * Indicates that there is insufficient memory to add new * information to the tree. **/ public static void play(BTNode<String> current) { while (!current.isLeaf( )) { if (query(current.getData( ))) current = current.getLeft( ); else current = current.getRight( ); } System.out.print("My guess is " + current.getData( ) + ". "); if (!query("Am I right?")) learn(current); else System.out.println("I knew it all along!"); } /** * Construct a small taxonomy tree with four animals. * @param - none * @return * a reference to the root of a taxonomy tree with the animals: * kangaroo, mouse, trout, robin. * @exception OutOfMemoryError * Indicates that there is insufficient memory to create the tree. **/ public static BTNode<String> beginningTree( ) { BTNode<String> root; BTNode<String> child; final String ROOT_QUESTION = "Are you a mammal?"; final String LEFT_QUESTION = "Are you bigger than a cat?"; final String RIGHT_QUESTION = "Do you live underwater?"; final String ANIMAL1 = "Kangaroo"; final String ANIMAL2 = "Mouse"; final String ANIMAL3 = "Trout"; final String ANIMAL4 = "Robin"; // Create the root node with the question Revise the animal-guessing program below so that the initial knowledge tree is
obtained by reading information froma file. Also, when the program ends, the knowledge tree at that point is
written to the same file. You must carefully specify the format of the data in this file. The format should make it easy to do two things: (a) read the file and set the initial tree; and (b) write the knowledge tree to the file, using some kind of traversal.
// FILE: AnimalGuess.java // This animal-guessing program illustrates the use of the binary tree node class. import edu.colorado.nodes.BTNode; import java.util.Scanner; /****************************************************************************** * The <CODE>AnimalGuess</CODE> Java application illustrates the use of * the binary tree node class is a small animal-guessing game. * * <p><dt><b>Java Source Code for this class:</b><dd> * <A href="../applications/Animals.java"> * http://www.cs.colorado.edu/~main/applications/Animals.java * </A> * * @author Michael Main * <A href="mailto:main@colorado.edu"> (main@colorado.edu) </A> * * @version * Jul 22, 2005 ******************************************************************************/ public class AnimalGuess { private static Scanner stdin = new Scanner(System.in); /** * The main method prints instructions and repeatedly plays the * animal-guessing game. As the game is played, the taxonomy tree * grows by learning new animals. The <CODE>String</CODE> argument * (<CODE>args</CODE>) is not used in this implementation. **/ public static void main(String[ ] args) { BTNode<String> root; instruct( ); root = beginningTree( ); do play(root); while (query("Shall we play again?")); System.out.println("Thanks for teaching me a thing or two."); } /** * Print instructions for the animal-guessing game. **/ public static void instruct( ) { System.out.println("Please think of an animal."); System.out.println("I will ask some yes/no questions to try to figure"); System.out.println("out what you are."); } /** * Play one round of the animal guessing game. * @param <CODE>current</CODE> * a reference to the root node of a binary taxonomy tree that will be * used to play the game. * <dt><b>Postcondition:</b><dd> * The method has played one round of the game, and possibly * added new information about a new animal. * @exception java.lang.OutOfMemoryError * Indicates that there is insufficient memory to add new * information to the tree. **/ public static void play(BTNode<String> current) { while (!current.isLeaf( )) { if (query(current.getData( ))) current = current.getLeft( ); else current = current.getRight( ); } System.out.print("My guess is " + current.getData( ) + ". "); if (!query("Am I right?")) learn(current); else System.out.println("I knew it all along!"); } /** * Construct a small taxonomy tree with four animals. * @param - none * @return * a reference to the root of a taxonomy tree with the animals: * kangaroo, mouse, trout, robin. * @exception OutOfMemoryError * Indicates that there is insufficient memory to create the tree. **/ public static BTNode<String> beginningTree( ) { BTNode<String> root; BTNode<String> child; final String ROOT_QUESTION = "Are you a mammal?"; final String LEFT_QUESTION = "Are you bigger than a cat?"; final String RIGHT_QUESTION = "Do you live underwater?"; final String ANIMAL1 = "Kangaroo"; final String ANIMAL2 = "Mouse"; final String ANIMAL3 = "Trout"; final String ANIMAL4 = "Robin"; // Create the root node with the question
Explanation / Answer
import java.util.Scanner; public class TwentyQuestions { private static Scanner stdin = new Scanner(System.in); public static void main(String[ ] args) { BTNode root; instruct( ); root = beginningTree( ); do play(root); while (query("Shall we play again?")); System.out.println("Thanks for teaching me a thing or two."); System.out.println ("Here is the tree:"); root.print(1); } public static void instruct( ) { System.out.println("Please think of an ocean animal."); System.out.println("I will ask some yes/no questions to try to figure out which animal you're thinking of."); } public static void play(BTNode current) { while (!current.isLeaf( )) { if (query(current.getData( ))) current = current.getLeft( ); else current = current.getRight( ); } System.out.print("My guess is " + current.getData( ) + ". "); if (!query("Am I right?")) learn(current); else System.out.println("I knew it all along!"); } public static BTNode beginningTree( ) { BTNode root; BTNode child; BTNode child1; BTNode child2; BTNode child3; BTNode child4; BTNode child5; BTNode child6; BTNode child7; BTNode child8; BTNode child9; BTNode child10; BTNode child11; BTNode child12; BTNode child13; BTNode child14; final String ROOT_QUESTION = "Is it a mammal?"; final String LEFT_QUESTION = "Is it able to move on land?"; final String LEFT_QUESTION2 = "Is it a solitary animal?"; final String RIGHT_QUESTION2 = "Is it larger than a truck?"; final String RIGHT_QUESTION3 = "Does it have tusks?"; final String RIGHT_QUESTION = "Does it have any limbs/tentacles?"; final String LEFT_QUESTION4 = "Does it have more than four limbs/tentacles?"; final String LEFT_QUESTION5 = "Does it have an exoskeleton?"; final String LEFT_QUESTION6 = "Does it have claws?"; final String LEFT_QUESTION7 = "Does it have a long tail?"; final String RIGHT_QUESTION7 = "Does it have 8 arms?"; final String RIGHT_QUESTION5 = "Does it have a shell?"; final String RIGHT_QUESTION4 = "Can it sting?"; final String LEFT_QUESTION8 = "Is it long and snakelike?"; final String RIGHT_QUESTION8 = "Is it generally smaller than a car?"; final String ANIMAL1 = "Seal"; final String ANIMAL2 = "Sea Lion"; final String ANIMAL3 = "Walrus"; final String ANIMAL4 = "Whale"; final String ANIMAL5 = "Dolphin"; final String ANIMAL6 = "Shrimp"; final String ANIMAL7 = "Lobster"; final String ANIMAL8 = "Crab"; final String ANIMAL9 = "Jellyfish"; final String ANIMAL10 = "Octopus"; final String ANIMAL11 = "Squid"; final String ANIMAL12 = "Turtle"; final String ANIMAL13 = "Alligator"; final String ANIMAL14 = "Eel"; final String ANIMAL15 = "Stingray"; final String ANIMAL16 = "Shark"; final String ANIMAL17 = "Fish"; // Create the root node with the questionRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.