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