Results 1 to 2 of 2

Thread: Tree

  1. #1
    bryan238 is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

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

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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 07:46 AM.

Similar Threads

  1. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:08 AM
  2. KD tree
    By izac01 in forum Java Applets
    Replies: 2
    Last Post: 04-07-2011, 07:07 AM
  3. Replies: 0
    Last Post: 04-04-2010, 08:40 AM
  4. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 01:51 PM
  5. Need Help With Tree Adt
    By avi in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •