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
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(); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
