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");

current = current.leftChild;

else

current = current.rightChild;}

else {

System.out.println("I know. Is it a " + current.value + "?");

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("Try again?");

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");

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("I know. Is it a " + current.value + "?");

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("Try again?");

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?");

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("Is it a snake?");

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:);

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Does it live underground?);

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Try again?");

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?");

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("Is it a snake?");

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:);

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Does it live in water?);

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Try again?");

current = root;

else

return;}

}

}

while(1){

string ans;

if(ans|0|=='Y)

return true;

else if(ans|0|=='N')

return false;

}

}

public void learnNewAnimal(treeNode<string> current) {

string currentAnimal - current.value;

strint newAnimal;

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);

System.out.println("For a " + newAnimal + " is the answer Y/N?");

Current.leftChild = node1;

Current.rightChild = node2;

}

else{

Current.leftChild = node2;

Current.rightChild = node1;

}

Current.value = newQuestion;

}