Results 1 to 2 of 2
Thread: Tree
- 07-19-2012, 05:53 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
Tree
question 1
Write a Java program that learns about a universe of your choice by asking the user yes/no questions. For example, your program might learn about animals by having the following dialog with its user. (User responses are in uppercase.)
Think of an animal and I will guess it.
Doest it have legs? YES
Is it a cat? YES
I win! Continue? YES
Think of an animal and I will guess it.
Does it have legs? NO
Is it a snake? YES
I win! Continue? YES
Think of an animal and I will guess it.
Doess it have legs? NO
Is it a snake? NO
I give up. What is it? EARTHWORM
Please type a question whose answer is yes for an earthworm and no for a snake:
DOES IT LIVE UNDERGROUND?
Continue? YES
Think of an animal and I will guess it.
Does it have have legs? NO
Does it live underground? NO
Is it a snake? NO
I give up. What is it? FISH
Please type a question whose answer is yes for a fish and no for a snake:
DOES IT LIVE IN WATER?
Continue? NO
Good-bye.
The program begins with minimal knowledge about animals: It knows that cats have legs and snakes do not. When the program incorrectly guesses, “snake” the next time, it asks for the answer and also asks for a way to distinguish between snakes and earthworms. The program builds a binary tree of questions and animals. A YES response to a question is stored in the question’s left child; a NO response is stored in the question’s right child.
---------------------------------------------------------------------------------------
import java.util.*;
import java.io.*;
public void animalGame()
{
treeNode<string>root=new treeNode<string>("cat",0,0,0);
treeNode<string> *current=root;
System.out.println("Let's play guess the animal.");
while(current!=0){
System.out.println("current.value");
if(answer())
current = current.leftChild;
else
current = current.rightChild;}
else {
System.out.println("I know. Is it a " + current.value + "?");
if(answer())
System.out.println("I won.");
else{
learnNewAnimal(current);
}
System.out.println("Try again?");
if(answer())
current = root;
else
return;}
}
}
public void animalGame()
{
treeNode<string>root=new treeNode<string>("snake",0,0,0);
treeNode<string> *current=root;
System.out.println("Let's play guess the animal.");
while(current!=0){
System.out.println("Does it have leg");
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
else {
System.out.println("I know. Is it a " + current.value + "?");
if(answer())
System.out.println("I won.");
else{
learnNewAnimal(current);
}
System.out.println("Try again?");
if(answer())
current = root;
else
return;}
}
}
public void animalGame()
{
treeNode<string>root=new treeNode<string>("earthworm",0,0,0);
treeNode<string> *current=root;
System.out.println("Think of an animal and I will guess it.");
while(current!=0){
System.out.println("Does it have legs?");
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
else {
System.out.println("Is it a snake?");
if(answer())
System.out.println("I won.");
else{
learnNewAnimal(current);
}
System.out.println("I give up what is it?");
string ew = scan.nextstring();
System.out.println("Please type a question whose answer is yes for an earthworm and no for a snake:);
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
System.out.println("Does it live underground?);
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
System.out.println("Try again?");
if(answer())
current = root;
else
return;}
}
}
public void animalGame()
{
treeNode<string>root=new treeNode<string>("fish",0,0,0);
treeNode<string> *current=root;
System.out.println("Think of an animal and I will guess it.");
while(current!=0){
System.out.println("Does it have legs?");
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
else {
System.out.println("Is it a snake?");
if(answer())
System.out.println("I won.");
else{
learnNewAnimal(current);
}
System.out.println("I give up what is it?");
string fish = scan.nextstring();
System.out.println("Please type a question whose answer is yes for an fish and no for a snake:);
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
System.out.println("Does it live in water?);
if(answer())
current = current.rightChild;
else
current = current.leftChild;}
System.out.println("Try again?");
if(answer())
current = root;
else
return;}
}
}
public boolean answer(){
while(1){
string ans;
ans = Simple().readLine();
if(ans|0|=='Y)
return true;
else if(ans|0|=='N')
return false;
System.out.println("please answer yes or no. (Y/N)");
}
}
public void learnNewAnimal(treeNode<string> current) {
string currentAnimal - current.value;
System.out.println("What is your animal?");
strint newAnimal;
newAnimal = SimpleIO.readLine()
System.out.println("What is a Y/N question that I can use to tell a" + current.value + "from a " + newAnimal + "?");
string newQuestion;
treeNode<string>node1 = new treeNode<string>(newAnimal, current, 0,0);
treeNode<string>node2 = new treeNode<string>(currentAnimal, current, 0,0);
newQuestion = SimpleIO.readLine();
System.out.println("For a " + newAnimal + " is the answer Y/N?");
if(answer()){
Current.leftChild = node1;
Current.rightChild = node2;
}
else{
Current.leftChild = node2;
Current.rightChild = node1;
}
Current.value = newQuestion;
}
-
Re: Tree
Question closed as a homework dump. Original poster, please read a private message that I've sent to you. Also moved to the new to java forum since there's nothing advanced about this question.
Last edited by Fubarable; 07-19-2012 at 06:46 AM.
Similar Threads
-
Binary Tree Help - Find the largest sub-tree
By joshhazel in forum New To JavaReplies: 2Last Post: 01-30-2012, 02:08 AM -
KD tree
By izac01 in forum Java AppletsReplies: 2Last Post: 04-07-2011, 06:07 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Creating a Tree and then saving the Tree
By jackmatt2 in forum New To JavaReplies: 0Last Post: 08-22-2009, 12:51 PM -
Need Help With Tree Adt
By avi in forum New To JavaReplies: 0Last Post: 03-20-2008, 03:11 AM


LinkBack URL
About LinkBacks

Bookmarks