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

Using Java, can someone help me put in something that would read a text file cal

ID: 649928 • Letter: U

Question

Using Java, can someone help me put in something that would read a text file called AnimalGuessingGame.txt and then write to it the user responses given in the binary tree. Basically the user tries to guess an animal and if it is not listed, then the user writes a question for a new animal. When the program is exited, none of that user info is saved. By saving the binary tree to a file it can then be read back into the program. I'm not really sure where to put the lines of code as in the main method, I keep getting errors. Both classes are posted below and seperated by 2 lines of *****..

import java.util.Scanner;

public class AnimalGuess
{
   private static Scanner keyboard = new Scanner(System.in);
   public static void main(String[ ] args)
   {
      AnimalNode<String> RootOfTree;

      startingInfo();
      RootOfTree = startTree();
      do
         play(RootOfTree);
      while (UserInput("Shall we play again?"));

      System.out.println("Bye!");
   }

//Starting info for user
   public static void startingInfo()
   {
      System.out.println("Think of an animal and I will try to guess it.");
   }
   public static void play(AnimalNode<String> current)
   {
      while (!current.isLeaf())
      {
         if (UserInput(current.getData()))
            current = current.getLeft();
         else
            current = current.getRight();
      }

      System.out.print("Is it a " + current.getData() + ". ");
      if (!UserInput("Am I right?"))
         ModifyTree(current);
      else
         System.out.println();
   }
//create tree with three potential questions to be asked (start of tree)
   public static AnimalNode<String> startTree()
   {
      AnimalNode<String> RootOfTree;
      AnimalNode<String> child;
      final String First_Question = "Is your animal larger than a car?";
      final String YesToQ1 = "Is your animal larger than a fox?";
      final String NoToQ1 = "Does your animal fly?";
      final String ANIMAL1 = "elephant";//y,y
      final String ANIMAL2 = "deer";//y,n
      final String ANIMAL3 = "seagull";//n,y
      final String ANIMAL4 = "squirrel";//n,n
      // Create the RootOfTree node with First_Question
      RootOfTree = new AnimalNode<String>(First_Question, null, null);
      //connect YesToQ1 to First_Question
      child = new AnimalNode<String>(YesToQ1, null, null);
      child.setLeft(new AnimalNode<String>(ANIMAL1, null, null));
      child.setRight(new AnimalNode<String>(ANIMAL2, null, null));
      RootOfTree.setLeft(child);
      //connect NOToQ1 to First_Question
      child = new AnimalNode<String>(NoToQ1, null, null);
      child.setLeft(new AnimalNode<String>(ANIMAL3, null, null));
      child.setRight(new AnimalNode<String>(ANIMAL4, null, null));
      RootOfTree.setRight(child);
      return RootOfTree;
   }

   public static void ModifyTree(AnimalNode<String> current)
   //current represents a wrong guess that was previously made
   //make tree update after user input is given (question and answer to the question)
   {
      String guessedAnimal;   // The animal that was just guessed
      String correctAnimal;// The animal that the user was thinking of
      String newQuestion;// A question to distinguish the two animals
      // Set Strings for the guessed animal, correct animal and a new question.
      guessedAnimal = current.getData();
      System.out.println("Ok-you stumped me! ");
      System.out.println("What animal were you thinking of?");
      correctAnimal = keyboard.nextLine();
      System.out.println("Please type a yes/no question that would distinguish a");
      System.out.println(correctAnimal + " from a " + guessedAnimal + ".");
      newQuestion = keyboard.nextLine();
      // Put the new question in the current node, and add two new children.
      current.setData(newQuestion);
      System.out.println("And the answer for a " + correctAnimal +" would be?");
      if (UserInput("Please answer"))
      { current.setLeft(new AnimalNode<String>(correctAnimal, null, null));
         current.setRight(new AnimalNode<String>(guessedAnimal, null, null));
      }
      else
      { current.setLeft(new AnimalNode<String>(guessedAnimal, null, null));
         current.setRight(new AnimalNode<String>(correctAnimal, null, null));
      }       
   }

   public static boolean UserInput(String prompt)
   {
      String answer;
      System.out.print(prompt + " (y or n): ");
      answer = keyboard.nextLine().toUpperCase();
      while (!answer.startsWith("Y") && !answer.startsWith("N"))
      {
   System.out.print("Invalid response. Please type y or n: ");
   answer = keyboard.nextLine().toUpperCase();
      }
      return answer.startsWith("Y");
   }
}

**************************************************************************************************************************************************************************

**************************************************************************************************************************************************************************


public class AnimalNode<E>
{
   private E data;
   private AnimalNode<E> left, right;
   public AnimalNode(E initialData, AnimalNode<E> initialLeft, AnimalNode<E> initialRight)
   {
      data = initialData;
      left = initialLeft;
      right = initialRight;
   }     

   public E getData()
   {
      return data;
   }
//left of node
   public AnimalNode<E> getLeft()
   {
      return left;                                             
   }
//return farthest left of node
   public E getLeftmostData()
   {
      if (left == null)
         return data;
      else
         return left.getLeftmostData();
   }
//return right of node
   public AnimalNode<E> getRight()
   {
      return right;                                             
   }
//return farthest right of node
   public E getRightmostData()
   {
      if (left == null)
         return data;
      else
         return left.getRightmostData();
   }


//print data below node
   public void inorderPrint()
   {
      if (left != null)
         left.inorderPrint();
      System.out.println(data);
      if (right != null)
         right.inorderPrint();
   }
//find if node is a leaf
   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                             
   }

//print data below or at node
   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   }

   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }
//0 as RootOfTree and 1 for children, etc.
   public void print(int depth)
   {
      int i;

      // Print the indentation and the data from the current node:
      for (i = 1; i <= depth; i++)
         System.out.print("    ");
      System.out.println(data);

      // Print the left subtree (or a dash if there is a right child and no left child)
      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }

      // Print the right subtree (or a dash if there is a left child and no left child)
      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }
   }
//remove farthest node on left
   public AnimalNode<E> removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }

//remove farthest right node
   public AnimalNode<E> removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
   }
   public void setData(E newData)
   {
      data = newData;
   }                                                             

//new left child
   public void setLeft(AnimalNode<E> newLeft)
   {                  
      left = newLeft;
   }
//new right child
   public void setRight(AnimalNode<E> newRight)
   {                  
      right = newRight;
   }
//copy all of tree
   public static <E> AnimalNode<E> CopyTree(AnimalNode<E> source)
   {
      AnimalNode<E> leftCopy, rightCopy;

      if (source == null)
         return null;
      else
      {
         leftCopy = CopyTree(source.left);
         rightCopy = CopyTree(source.right);
         return new AnimalNode<E>(source.data, leftCopy, rightCopy);
      }
   }

//keep a count of the nodes in the tree
   public static <E> long treeSize(AnimalNode<E> RootOfTree)
   {
      if (RootOfTree == null)
         return 0;
      else
         return 1 + treeSize(RootOfTree.left) + treeSize(RootOfTree.right);
   }

}

Explanation / Answer

import java.util.Scanner;

public class AnimalGuess
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[ ] args)
{
AnimalNode<String> RootOfTree;

startingInfo();
RootOfTree = startTree();
do
play(RootOfTree);
while (UserInput("Shall we play again?"));

System.out.println("Bye!");
}

//Starting info for user
public static void startingInfo()
{
System.out.println("Think of an animal and I will try to guess it.");
}
public static void play(AnimalNode<String> current)
{
while (!current.isLeaf())
{
if (UserInput(current.getData()))
current = current.getLeft();
else
current = current.getRight();
}

System.out.print("Is it a " + current.getData() + ". ");
if (!UserInput("Am I right?"))
ModifyTree(current);
else
System.out.println();
}
//create tree with three potential questions to be asked (start of tree)
public static AnimalNode<String> startTree()
{
AnimalNode<String> RootOfTree;
AnimalNode<String> child;
final String First_Question = "Is your animal larger than a car?";
final String YesToQ1 = "Is your animal larger than a fox?";
final String NoToQ1 = "Does your animal fly?";
final String ANIMAL1 = "elephant";//y,y
final String ANIMAL2 = "deer";//y,n
final String ANIMAL3 = "seagull";//n,y
final String ANIMAL4 = "squirrel";//n,n
// Create the RootOfTree node with First_Question
RootOfTree = new AnimalNode<String>(First_Question, null, null);
//connect YesToQ1 to First_Question
child = new AnimalNode<String>(YesToQ1, null, null);
child.setLeft(new AnimalNode<String>(ANIMAL1, null, null));
child.setRight(new AnimalNode<String>(ANIMAL2, null, null));
RootOfTree.setLeft(child);
//connect NOToQ1 to First_Question
child = new AnimalNode<String>(NoToQ1, null, null);
child.setLeft(new AnimalNode<String>(ANIMAL3, null, null));
child.setRight(new AnimalNode<String>(ANIMAL4, null, null));
RootOfTree.setRight(child);
return RootOfTree;
}

public static void ModifyTree(AnimalNode<String> current)
//current represents a wrong guess that was previously made
//make tree update after user input is given (question and answer to the question)
{
String guessedAnimal; // The animal that was just guessed
String correctAnimal;// The animal that the user was thinking of
String newQuestion;// A question to distinguish the two animals
// Set Strings for the guessed animal, correct animal and a new question.
guessedAnimal = current.getData();
System.out.println("Ok-you stumped me! ");
System.out.println("What animal were you thinking of?");
correctAnimal = keyboard.nextLine();
System.out.println("Please type a yes/no question that would distinguish a");
System.out.println(correctAnimal + " from a " + guessedAnimal + ".");
newQuestion = keyboard.nextLine();
// Put the new question in the current node, and add two new children.
current.setData(newQuestion);
System.out.println("And the answer for a " + correctAnimal +" would be?");
if (UserInput("Please answer"))
{ current.setLeft(new AnimalNode<String>(correctAnimal, null, null));
current.setRight(new AnimalNode<String>(guessedAnimal, null, null));
}
else
{ current.setLeft(new AnimalNode<String>(guessedAnimal, null, null));
current.setRight(new AnimalNode<String>(correctAnimal, null, null));
}   
}

public static boolean UserInput(String prompt)
{
String answer;
System.out.print(prompt + " (y or n): ");
answer = keyboard.nextLine().toUpperCase();
while (!answer.startsWith("Y") && !answer.startsWith("N"))
{
System.out.print("Invalid response.Please type y or n: ");
answer = keyboard.nextLine().toUpperCase();
}
return answer.startsWith("Y");
}
}
public class AnimalNode(E)
{
private E data;
private AnimalNode E , left, right;

{
data = initialData;
left = initialLeft;
right = initialRight;
}   

public E getData()
{
return data;
}
//left of node
public AnimalNode<E> getLeft()
{
return left;   
}
//return farthest left of node
public E getLeftmostData()
{
if (left == null)
return data;
else
return left.getLeftmostData();
}
//return right of node
public AnimalNode<E> getRight()
{
return right;   
}
//return farthest right of node
public E getRightmostData()
{
if (left == null)
return data;
else
return left.getRightmostData();
}


//print data below node
public void inorderPrint()
{
if (left != null)
left.inorderPrint();
System.out.println(data);
if (right != null)
right.inorderPrint();
}
//find if node is a leaf
public boolean isLeaf( )
{
return (left == null) && (right == null);   
}

//print data below or at node
public void preorderPrint( )
{
System.out.println(data);
if (left != null)
left.preorderPrint( );
if (right != null)
right.preorderPrint( );
}

public void postorderPrint( )
{
if (left != null)
left.postorderPrint( );
if (right != null)
right.postorderPrint( );
System.out.println(data);
}
//0 as RootOfTree and 1 for children, etc.
public void print(int depth)
{
int i;

// Print the indentation and the data from the current node:
for (i = 1; i <= depth; i++)
System.out.print(" ");
System.out.println(data);

// Print the left subtree (or a dash if there is a right child and no left child)
if (left != null)
left.print(depth+1);
else if (right != null)
{
for (i = 1; i <= depth+1; i++)
System.out.print(" ");
System.out.println("--");
}

// Print the right subtree (or a dash if there is a left child and no left child)
if (right != null)
right.print(depth+1);
else if (left != null)
{
for (i = 1; i <= depth+1; i++)
System.out.print(" ");
System.out.println("--");
}
}
//remove farthest node on left
public AnimalNode<E> removeLeftmost( )
{
if (left == null)
return right;
else
{
left = left.removeLeftmost( );
return this;
}
}

//remove farthest right node
public AnimalNode<E> removeRightmost( )
{
if (right == null)
return left;
else
{
right = right.removeRightmost( );
return this;
}
}
public void setData(E newData)
{
data = newData;
}   

//new left child
public void setLeft(AnimalNode <E> newLeft)
{
left = newLeft;
}
//new right child
public void setRight(AnimalNode<E> newRight)
{
right = newRight;
}
//copy all of tree
public static <E> AnimalNode<E> CopyTree(AnimalNode<E> source)
{
AnimalNode<E> leftCopy, rightCopy;

if (source == null)
return null;
else
{
leftCopy = CopyTree(source.left);
rightCopy = CopyTree(source.right);
return new AnimalNode<E>(source.data, leftCopy, rightCopy);
}
}

//keep a count of the nodes in the tree
public static <E> long treeSize(AnimalNode<E> RootOfTree)
{
if (RootOfTree == null)
return 0;
else
return 1 + treeSize(RootOfTree.left) + treeSize(RootOfTree.right);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote