Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BTNode.java : https://www.cs.colorado.edu/~main/edu/colorado/nodes/BTNode.java AnimalGuess.java : https://www.cs.colorado.edu/~main/applications/AnimalGuess.java Download binary tree node class BTNode.java and animal guessing program AnimalGuess.java . You will need to revise the animal-guessing

BTNode.java : https://www.cs.colorado.edu/~main/edu/colorado/nodes/BTNode.java

AnimalGuess.java : https://www.cs.colorado.edu/~main/applications/AnimalGuess.java

Download binary tree node class BTNode.java and animal guessing program AnimalGuess.java . You will need to revise the animal-guessing program so that the initial knowledge tree is obtained by reading information from a file. Also, when program ends, the knowledge tree at that point is written to the same file. The file has data in a specific format - nodes of the knowledge tree are listed one per line using pre-order traversal. If a node contains a question (is a nonleaf node), then the question is preceeded by "?" in the line. If a node contains an animal (is a leaf node), then the line contains just the name of the animal. File textbookAnimals contains the knowledge tree from p.471 represented in this format. Such format makes it easy to read the file and set the initial tree, and to write the knowledge tree to the file using pre-order traversal.

You need to do the following:

1. Add recursive method printLeaves to the generic BTNode class:

 public void printLeaves() 

The method should output all the leaves in the binary tree with this node as its root.

2. Add method readFromFile to animal guessing program:

public static BTNode < String> readFromFile(Scanner input)

The method has one parameter (input) - a stream of the class Scanner which is connected to a text file for reading. The method must be recursive. The method should build a tree which representation starts on the next line in input and return the root of the built tree. Note: once you read a line you may check whether this line is a question or an animal by checking the first character of the line.

3. Add method writeToFile to animal guessing program: public static void writeToFile(PrintWriter output, BTNode < String> root)

The method has two parameters. The first parameter (output) is a stream of the class PrintWriter which is connected to a text file for writing. The second paramater (root) is a root of a knowledge tree. The method must be recursive. It should write a tree with root root to output using the data format described above.

4. Modify animal-guessing program (file AnimalGuess.java) so that

o it prompts the user to enter file name, o instead of beginningTree method it uses readFromFile method to build the initial knowledge

tree from the file user specified, o after the user is finished playing it uses method printLeaves to output a list of animals it knows,

and o uses method writeToFile to write modified knowledge tree to the same file.

Sample output of the program is given below. You may assume that file is always in the correct format. You do not need to check whether file is in correct format or not.

For a knowledge tree represented in file animals (https://www.cs.nmsu.edu/~ipivkina/cs272/Homework/animals) sample run of your program may look like the following. Please enter file name: animals

 Knowledge tree is read from animals 
 Please think of an animal. I will ask some yes/no questions to try to figure out what you are. Does it have feathers? [Y or N]: y Does it live in a barnyard? [Y or N]: y My guess is chicken. Am I right? [Y or N]: y I knew it all along! 
 Shall we play again? [Y or N]: y Does it have feathers? [Y or N]: n Is it a mammal? [Y or N]: y Does it have stripes? [Y or N]: n My guess is elephant. Am I right? [Y or N]: n I give up. What are you? 

kangaroo Please type a yes/no question that will distinguish a kangaroo from a elephant. Your question: Does it hop? As a kangaroo, Does it hop? Please answer [Y or N]: y

 Shall we play again? [Y or N]: y Does it have feathers? [Y or N]: n Is it a mammal? [Y or N]: y Does it have stripes? [Y or N]: n Does it hop? [Y or N]: y 
 My guess is kangaroo. Am I right? [Y or N]: y I knew it all along! 

Shall we play again? [Y or N]: n Thanks for teaching me a thing or two. Now I know the following animals: chicken owl raven turkey eagle tiger kangaroo elephant crocodile

Updated knowledge tree is written to file animals

Specifications for all the methods which you implement should be included as comments in your code. Also, please use inline comments, meaningful variable names, indentation, formatting and whitespace throughout your program to improve its readability.

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_2

Step: 3

blur-text-image_3

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions