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

Using Java - Your job is to create an animal guessing game that will work like t

ID: 3838146 • Letter: U

Question

Using Java -

Your job is to create an animal guessing game that will work like the following example: Computer: Does it have 4 legs? User: No Computer: Then it is a chicken? User: No Computer: What was your animal? User: Snake Computer: Enter in a question that differentiates your animal from the other animal, and is a yes answer to your animal. User: Does it have no legs? Then the next time the game is played it will run like: Computer: Does it have 4 legs? User: No Computer: Does it have no legs? User: Yes Computer: Then it is a snake? User: yes Computer: I guessed it! Write your game so that it uses a binary tree that looks like this:

Explanation / Answer

Below is the Main class which has the main functions and prompts the user with the questions.

Main.java :

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

boolean newLoop = true;
Question que1 = new Question("Does it have four legs?");
que1.setYesAnswer("Dog");
que1.setNoAnswer("Chiken");
que1.setHasYesAnswer(true);
que1.setHasNoAnswer(true);
Question tempQuestion = que1;
while (true) {
if (newLoop) { //Ask question from beginning....
System.out.println("***********************************************");
System.out.println("Welcome to new game !!!!");
tempQuestion = que1;
newLoop = false;
}
System.out.println("Computer :" + tempQuestion.getQuestion());
System.out.print("User:");
Scanner sc = new Scanner(System.in);
String ans = sc.nextLine();
if (ans.equalsIgnoreCase("yes")) {
if (tempQuestion.isHasYesAnswer()) {
System.out.println("Computer : Then it is a " + tempQuestion.getYesAnswer());
System.out.print("User:");
sc = new Scanner(System.in);
ans = sc.nextLine();
if (ans.equalsIgnoreCase("Yes")) {
System.out.println("Computer : I guessed It!");
break;
} else if (ans.equalsIgnoreCase("no")) {
System.out.println("Computer : What was your animal?");
System.out.print("User:");
sc = new Scanner(System.in);
String newAnswer = sc.nextLine();
System.out.println("Computer : Enter a question that differentiates your animal from the other animal and is a yes answer to your animal.");
sc = new Scanner(System.in);
ans = sc.nextLine();
  
//create a new question and assign the yes answer to it
Question newQuestion = new Question(ans);
  
//new question becomes the yes question for the previous question.
tempQuestion.setYesQuestion(newQuestion);
//it means that now it does not have a yes answer..... it has yes question
tempQuestion.setHasYesAnswer(false);
newQuestion.setYesAnswer(newAnswer);
  
//the yes answer for previous question becomes the no answer for new question
newQuestion.setNoAnswer(tempQuestion.getYesAnswer());
newQuestion.setHasNoAnswer(true);
newQuestion.setHasYesAnswer(true);
//tempQuestion = newQuestion;
newLoop = true; // start asking question from biginning....
}
} else if (ans.equalsIgnoreCase("Yes")) {
//ask the next yes question
tempQuestion=tempQuestion.getYesQuestion();
} else if (ans.equalsIgnoreCase("no")) {
//ask the next no question
tempQuestion=tempQuestion.getNoQuestion();
}
} else if (ans.equalsIgnoreCase("no")) {
if (tempQuestion.isHasNoAnswer()) {

System.out.println("Computer : Then it is a " + tempQuestion.getNoAnswer());
System.out.print("User:");
sc = new Scanner(System.in);
ans = sc.nextLine();
if (ans.equalsIgnoreCase("Yes")) {
System.out.println("Computer : I guessed It!");
break;
} else if (ans.equalsIgnoreCase("no")) {
System.out.println("Computer : What was your animal?");
System.out.print("User:");
sc = new Scanner(System.in);
String newAnswer = sc.nextLine();
System.out.println("Computer : Enter a question that differentiates your animal from the other animal and is a yes answer to your animal.");
System.out.print("User:");
sc = new Scanner(System.in);
ans = sc.nextLine();
Question newQuestion = new Question(ans);
tempQuestion.setNoQuestion(newQuestion);
tempQuestion.setHasNoAnswer(false);
newQuestion.setYesAnswer(newAnswer);
newQuestion.setNoAnswer(tempQuestion.getYesAnswer());
newQuestion.setHasNoAnswer(true);
newQuestion.setHasYesAnswer(true);
//tempQuestion = newQuestion;
newLoop = true;
}
} else if (ans.equalsIgnoreCase("Yes")) {
tempQuestion=tempQuestion.getYesQuestion();
} else if (ans.equalsIgnoreCase("no")) {
tempQuestion=tempQuestion.getNoQuestion();
}
}

}
}
}

Below the Question class which consists the both the asnwers or corresponding questions....Used to form a tree

Question.java :


public class Question {
private String question ;
private String yesAnswer ;
private String noAnswer ;
private Question yesQuestion ;
private Question noQuestion ;
  
//below two variables are used to know whether it has a answer or not
private boolean hasYesAnswer = false;
private boolean hasNoAnswer = false;
  
Question(String que){
this.question = que;
}
  
public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public String getYesAnswer() {
return yesAnswer;
}

public void setYesAnswer(String yesAnswer) {
this.yesAnswer = yesAnswer;
}

public String getNoAnswer() {
return noAnswer;
}

public void setNoAnswer(String noAnswer) {
this.noAnswer = noAnswer;
}

public Question getYesQuestion() {
return yesQuestion;
}

public void setYesQuestion(Question yesQuestion) {
this.yesQuestion = yesQuestion;
}

public Question getNoQuestion() {
return noQuestion;
}

public void setNoQuestion(Question noQueston) {
this.noQuestion = noQueston;
}

public boolean isHasYesAnswer() {
return hasYesAnswer;
}

public void setHasYesAnswer(boolean hasYesAnswer) {
this.hasYesAnswer = hasYesAnswer;
}

public boolean isHasNoAnswer() {
return hasNoAnswer;
}

public void setHasNoAnswer(boolean hasNoAnswer) {
this.hasNoAnswer = hasNoAnswer;
}

}

Below the output :

Welcome to new game !!!!
Computer :Does it have four legs?
User:yes
Computer : Then it is a Dog
User:no
Computer : What was your animal?
User:lion
Computer : Enter a question that differentiates your animal from the other animal and is a yes answer to your animal.
Can it swim?
***********************************************
Welcome to new game !!!!
Computer :Does it have four legs?
User:yes
Computer :Can it swim?
User:yes
Computer : Then it is a lion
User:yes
Computer : I guessed It!

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