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 Does it have four legs? dog Does it have no legs? snake

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!

Add a comment
Know the answer?
Add Answer to:
Using Java - Your job is to create an animal guessing game that will work like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A Backwards Guessing Game For this assignment, you will be a making a guessing game with...

    A Backwards Guessing Game For this assignment, you will be a making a guessing game with a twist. You will play the role of the computer, and the computer will try to guess the number. You will think of a number (you don’t have to read it in as input) between 1 and 100. You will create a loop that runs 5 times. In this loop, the computer will try to guess the number in the range it knows is...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

    Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X'). Write a function called guessLetter that...

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game...

    JAVA Develop a letter guessing game in this assignment using Java. Requirements: 1). When the game is started, generate a random letter between A and Z; 2). Display a message "I have a secret letter (A to Z), can you guess what it is?"; 3). Read the user's answer; 4). Compare the user's answer and the random secret letter generated; 5). If the user answer is before the random secret letter in the alphabet, display "Incorrect. Try something bigger" and...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • Task 4 (20 points) Implement a simple guessing game. Name your file task4.c. Your game will...

    Task 4 (20 points) Implement a simple guessing game. Name your file task4.c. Your game will use rand() to generate a random number from 1 to 10 then prompt user to guess that number. User will have unlimited number of tries to guess the correct number. Your program should work like this hb117@uxb4:$ gco -Wall task4 . c -o task4 hb117@uxb4:~$ ./guessing Guess a number from 1-10: 1 Guess again: 4 Guess again: 2 Guess again: 9 Guess again: 10...

  • USING JAVA, Use the binary search algorithm to create a game where you think of the...

    USING JAVA, Use the binary search algorithm to create a game where you think of the number and the computer guesses it. The computer should be able to guess any number between 1 and 500. When the computer chooses a number, you need to tell it if the guess is too high or too low. Have the program print out the number of guesses that it took to guess the number.

  • java code. James needs to create a computer text based game where some user has to...

    java code. James needs to create a computer text based game where some user has to enter as many words with the given letters as the given word. A good start would be writing a method that checks if a method has the same letters as the other. include pseudo code+ Input-Listen and Silent have the same word that's all information I have is a Anagram program that need to be done.ASAP just help me with the pseudo code of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT