Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED TO BUILD IN JAVA AND RUN PLEASE Code in bold comment needs to be finished.

ID: 3709108 • Letter: N

Question

NEED TO BUILD IN JAVA AND RUN PLEASE

Code in bold comment needs to be finished. Just need help finishing the code

The code is for a Tree guessing game

//package trees;

import java.util.Scanner;

class AnimalNode {

   public String text;
   public AnimalNode rightPtr;
   public AnimalNode leftPtr;

   public AnimalNode (String s) {
       text = s;
   }

   public String toString() {
       return text;
   }  
}

class TreeGuessingGame {

   public AnimalNode root;

   public TreeGuessingGame() {
       root = new AnimalNode("Does it have 4 legs?");
       root.leftPtr = new AnimalNode("dog");
       root.rightPtr = new AnimalNode("chicken");
   }

   public void play() {

       Scanner keyboard = new Scanner(System.in);
       System.out.println("Think of an animal and I will");
       System.out.println("try and guess it");

       AnimalNode current = root;
       AnimalNode parent;
       boolean isLeftChild = true;
       String answer;
      
       while(current.leftPtr != null) { //while not a leaf
           parent = current;
          
           System.out.println(current.text);
           answer = keyboard.nextLine();
  
           if(answer.equalsIgnoreCase("yes")) {
               current = current.leftPtr;
               isLeftChild = true;
           }  
           else {
               current = current.rightPtr;
               isLeftChild = false;
           }
       }
      
       System.out.print("Is the animal you're thinking of a ");
       System.out.println(current.text + "?");
       answer = keyboard.nextLine();
      
       if(answer.equalsIgnoreCase("yes"))
           System.out.println("I won, I guessed it!!");
       else {
           System.out.println("You won");
           //1 ask the user to enter in their animal
           //2 Make a new Node with this animal
           //3 ask the user to enter in a "yes" question
           //   about their animal
           //4 Make a new Node with this question
           //5 set New Question Node leftPtr to point to
           //   the new Animal Node
           //6 set New Question Node rightPtr to point to
           //   the old Animal Node that was just guessed
           //7 set the parent's left or right node pointer
           //   (depending if the left or right path was taken)
           //   set that parent's ptr to point to the new
           //   question node

          
          
       }
          
   }

   public static void main(String[] args) {

       TreeGuessingGame game = new TreeGuessingGame();
       game.play();
   }

}

Explanation / Answer

java code:


package treeguessinggame;

import java.util.Scanner;

class AnimalNode {

public String text;
public AnimalNode rightPtr;
public AnimalNode leftPtr;

public AnimalNode (String s) {
text = s;
}

public String toString() {
return text;
}   
}

class TreeGuessingGame {

public AnimalNode root;

public TreeGuessingGame() {
root = new AnimalNode("Does it have 4 legs?");
root.leftPtr = new AnimalNode("dog");
root.rightPtr = new AnimalNode("chicken");
}

public void play() {

Scanner keyboard = new Scanner(System.in);
System.out.println("Think of an animal and I will");
System.out.println("try and guess it");

AnimalNode current = root;
AnimalNode parent;
boolean isLeftChild = true;
String answer;
  
while(current.leftPtr != null) { //while not a leaf
parent = current;
  
System.out.println(current.text);
answer = keyboard.nextLine();
  
if(answer.equalsIgnoreCase("yes")) {
current = current.leftPtr;
isLeftChild = true;
}   
else {
current = current.rightPtr;
isLeftChild = false;
}
}
  
System.out.print("Is the animal you're thinking of a ");
System.out.println(current.text + "?");
answer = keyboard.nextLine();
  
if(answer.equalsIgnoreCase("yes"))
System.out.println("I won, I guessed it!!");
else {
System.out.println("You won");
//1 ask the user to enter in their animal
System.out.println("Enter the animal name");
String name=keyboard.nextLine();
  
//2 Make a new Node with this animal
AnimalNode node=new AnimalNode(name);
  
//3 ask the user to enter in a "yes" question
// about their animal
System.out.println("Enter a "yes" question");
String question=keyboard.nextLine();
//4 Make a new Node with this question
AnimalNode node1=new AnimalNode(question);
  
//5 set New Question Node leftPtr to point to
// the new Animal Node
node1.leftPtr=node;
//6 set New Question Node rightPtr to point to
// the old Animal Node that was just guessed
node1.rightPtr=current;
//7 set the parent's left or right node pointer
// (depending if the left or right path was taken)
// set that parent's ptr to point to the new
// question node
parent=node;
  
  
}
  
}

public static void main(String[] args) {

TreeGuessingGame game = new TreeGuessingGame();
game.play();
}

}

//i have given the code that asked in bold character. for any clarification,please do comments