Question

Write a Java program that implements the number guessing game where the computer tries to guess...

Write a Java program that implements the number guessing game where the computer tries to guess a number in your head. You pick a number between 0 and 100, and the computer generates a guess. You then type 1 if your number is larger than the computer’s guess, -1 if your number is smaller, and 0 if the number is correct. The computer should keep trying to guess the number by adjusting the guess based on your feedback (so if you enter -1, the next guess has to be smaller).

Bonus [ 2 points ] if your program always guesses the correct number within a maximum of 7 tries. Java programming language

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

//COMMENT IF YOU HAVE DOUBTS AND PLEASE LIKE THE ANSWER.

//------------ NumberGuessing.java --------
//create a package named guessing if running in Eclipse if not
//remove below package statement and then save the file as NumberGuessing.java


package guessing;

import java.util.Scanner;

public class NumberGuessing {
   public static void guessingGame() {
       int low = 0;
       int high = 100;

       int response = 1;
       Scanner in = new Scanner(System.in);
       int middle;
       int guessCount = 0;
       do {
           middle = low + (high - low) / 2;
           System.out.println("\nComputer Guess is : " + middle);
           System.out.println("\nIs Guess Correct : ");
           System.out.println("0. Guess Correct");
           System.out.println("-1. if your number is smaller the Guess");
           System.out.println("1. if your number is greater than the Guess");
           System.out.print("Enter your Response: ");
           response = in.nextInt();
           if (response == 1) {
               low = middle + 1;
               ++guessCount;

           } else if (response == -1) {
               ++guessCount;
               high = middle - 1;
           } else if (response == 0) {
               ++guessCount;
               System.out.println("\nGuessed value : " + middle + " in " + guessCount + " guesses.");

           } else {
               System.out.println("\nError: Invalid Response entered, Try Again.");
           }
       } while (response != 0);

   }

   public static void main(String args[]) {
       //call the method guessingGame() method to test
       //the functionality of guessing.
//       guessingGame();
      
       // uncomment below to test the guessing mechanism automatically.
       testGuessing();
       System.out.println("\nExiting....");
   }

   /*
   * method to test the Guessing mechanism to show that guess for a each number
   * from 0 to 100 will be found in less than or equal to 7 guesses. it is just
   * for your testing the answer you wanted is in the method guessingGame()
   * method.
   */
   public static void testGuessing() {
       System.out.println("--------- Number Guessing Automatic Testing for 0 - 100 Numbers -------\n");
       for (Integer num = 0; num <= 100; num++) {
           System.out.println("\nNumber to guess : "+num);
           int low = 0;
           int high = 100;
           int response = 1;
           Integer middle;
           int guessCount = 0;
           do {
               middle = low + (high - low) / 2;
               response = num.compareTo(middle);
               if (response == 1) {
                   low = middle + 1;
                   ++guessCount;

               } else if (response == -1) {
                   ++guessCount;
                   high = middle - 1;
               } else if (response == 0) {
                   ++guessCount;
                   System.out.println("Guessed value : " + middle + " in " + guessCount + " guesses.");

               }
           } while (response != 0);
       }

   }
}

NumberGuessing.java & Console X <terminated> NumberGuessing [Java Application] C:\Program FilesVava\jre 1.8.0_131\bin\javaw.e| | 3 | 5H = | 2 | = B. NumberGuessing.java Console X <terminated> NumberGuessing [Java Application] C:\Program Files Vava\jr*NumberGuessing.java @ Console X <terminated> NumberGuessing (Java Application] C:\Program Files Java\jre 1.8.0_131\bin\javaw| | | E F 1 = T E | *NumberGuessing.java & Console X <terminated> NumberGuessing [Java Application) C:\Program Files\ava\jre1
/*
* ----------------- OUTPUT --------
* my guess : 30
*
Computer Guess is : 50

Is Guess Correct :
0. Guess Correct
-1. if your number is smaller the Guess
1. if your number is greater than the Guess
Enter your Response: -1

Computer Guess is : 24

Is Guess Correct :
0. Guess Correct
-1. if your number is smaller the Guess
1. if your number is greater than the Guess
Enter your Response: 1

Computer Guess is : 37

Is Guess Correct :
0. Guess Correct
-1. if your number is smaller the Guess
1. if your number is greater than the Guess
Enter your Response: -1

Computer Guess is : 30

Is Guess Correct :
0. Guess Correct
-1. if your number is smaller the Guess
1. if your number is greater than the Guess
Enter your Response: 0

Guessed value : 30 in 4 guesses.

Exiting....

------------ Automatic Testing -----------
--------- Number Guessing Automatic Testing for 0 - 100 Numbers -------


Number to guess : 0
Guessed value : 0 in 6 guesses.

Number to guess : 1
Guessed value : 1 in 7 guesses.

Number to guess : 2
Guessed value : 2 in 5 guesses.

Number to guess : 3
Guessed value : 3 in 6 guesses.

Number to guess : 4
Guessed value : 4 in 7 guesses.

Number to guess : 5
Guessed value : 5 in 4 guesses.

Number to guess : 6
Guessed value : 6 in 6 guesses.

Number to guess : 7
Guessed value : 7 in 7 guesses.

Number to guess : 8
Guessed value : 8 in 5 guesses.

Number to guess : 9
Guessed value : 9 in 6 guesses.

Number to guess : 10
Guessed value : 10 in 7 guesses.

Number to guess : 11
Guessed value : 11 in 3 guesses.

Number to guess : 12
Guessed value : 12 in 6 guesses.

Number to guess : 13
Guessed value : 13 in 7 guesses.

Number to guess : 14
Guessed value : 14 in 5 guesses.

Number to guess : 15
Guessed value : 15 in 6 guesses.

Number to guess : 16
Guessed value : 16 in 7 guesses.

Number to guess : 17
Guessed value : 17 in 4 guesses.

Number to guess : 18
Guessed value : 18 in 6 guesses.

Number to guess : 19
Guessed value : 19 in 7 guesses.

Number to guess : 20
Guessed value : 20 in 5 guesses.

Number to guess : 21
Guessed value : 21 in 7 guesses.

Number to guess : 22
Guessed value : 22 in 6 guesses.

Number to guess : 23
Guessed value : 23 in 7 guesses.

Number to guess : 24
Guessed value : 24 in 2 guesses.

Number to guess : 25
Guessed value : 25 in 6 guesses.

Number to guess : 26
Guessed value : 26 in 7 guesses.

Number to guess : 27
Guessed value : 27 in 5 guesses.

Number to guess : 28
Guessed value : 28 in 6 guesses.

Number to guess : 29
Guessed value : 29 in 7 guesses.

Number to guess : 30
Guessed value : 30 in 4 guesses.

Number to guess : 31
Guessed value : 31 in 6 guesses.

Number to guess : 32
Guessed value : 32 in 7 guesses.

Number to guess : 33
Guessed value : 33 in 5 guesses.

Number to guess : 34
Guessed value : 34 in 7 guesses.

Number to guess : 35
Guessed value : 35 in 6 guesses.

Number to guess : 36
Guessed value : 36 in 7 guesses.

Number to guess : 37
Guessed value : 37 in 3 guesses.

Number to guess : 38
Guessed value : 38 in 6 guesses.

Number to guess : 39
Guessed value : 39 in 7 guesses.

Number to guess : 40
Guessed value : 40 in 5 guesses.

Number to guess : 41
Guessed value : 41 in 6 guesses.

Number to guess : 42
Guessed value : 42 in 7 guesses.

Number to guess : 43
Guessed value : 43 in 4 guesses.

Number to guess : 44
Guessed value : 44 in 6 guesses.

Number to guess : 45
Guessed value : 45 in 7 guesses.

Number to guess : 46
Guessed value : 46 in 5 guesses.

Number to guess : 47
Guessed value : 47 in 7 guesses.

Number to guess : 48
Guessed value : 48 in 6 guesses.

Number to guess : 49
Guessed value : 49 in 7 guesses.

Number to guess : 50
Guessed value : 50 in 1 guesses.

Number to guess : 51
Guessed value : 51 in 6 guesses.

Number to guess : 52
Guessed value : 52 in 7 guesses.

Number to guess : 53
Guessed value : 53 in 5 guesses.

Number to guess : 54
Guessed value : 54 in 6 guesses.

Number to guess : 55
Guessed value : 55 in 7 guesses.

Number to guess : 56
Guessed value : 56 in 4 guesses.

Number to guess : 57
Guessed value : 57 in 6 guesses.

Number to guess : 58
Guessed value : 58 in 7 guesses.

Number to guess : 59
Guessed value : 59 in 5 guesses.

Number to guess : 60
Guessed value : 60 in 6 guesses.

Number to guess : 61
Guessed value : 61 in 7 guesses.

Number to guess : 62
Guessed value : 62 in 3 guesses.

Number to guess : 63
Guessed value : 63 in 6 guesses.

Number to guess : 64
Guessed value : 64 in 7 guesses.

Number to guess : 65
Guessed value : 65 in 5 guesses.

Number to guess : 66
Guessed value : 66 in 6 guesses.

Number to guess : 67
Guessed value : 67 in 7 guesses.

Number to guess : 68
Guessed value : 68 in 4 guesses.

Number to guess : 69
Guessed value : 69 in 6 guesses.

Number to guess : 70
Guessed value : 70 in 7 guesses.

Number to guess : 71
Guessed value : 71 in 5 guesses.

Number to guess : 72
Guessed value : 72 in 7 guesses.

Number to guess : 73
Guessed value : 73 in 6 guesses.

Number to guess : 74
Guessed value : 74 in 7 guesses.

Number to guess : 75
Guessed value : 75 in 2 guesses.

Number to guess : 76
Guessed value : 76 in 6 guesses.

Number to guess : 77
Guessed value : 77 in 7 guesses.

Number to guess : 78
Guessed value : 78 in 5 guesses.

Number to guess : 79
Guessed value : 79 in 6 guesses.

Number to guess : 80
Guessed value : 80 in 7 guesses.

Number to guess : 81
Guessed value : 81 in 4 guesses.

Number to guess : 82
Guessed value : 82 in 6 guesses.

Number to guess : 83
Guessed value : 83 in 7 guesses.

Number to guess : 84
Guessed value : 84 in 5 guesses.

Number to guess : 85
Guessed value : 85 in 7 guesses.

Number to guess : 86
Guessed value : 86 in 6 guesses.

Number to guess : 87
Guessed value : 87 in 7 guesses.

Number to guess : 88
Guessed value : 88 in 3 guesses.

Number to guess : 89
Guessed value : 89 in 6 guesses.

Number to guess : 90
Guessed value : 90 in 7 guesses.

Number to guess : 91
Guessed value : 91 in 5 guesses.

Number to guess : 92
Guessed value : 92 in 6 guesses.

Number to guess : 93
Guessed value : 93 in 7 guesses.

Number to guess : 94
Guessed value : 94 in 4 guesses.

Number to guess : 95
Guessed value : 95 in 6 guesses.

Number to guess : 96
Guessed value : 96 in 7 guesses.

Number to guess : 97
Guessed value : 97 in 5 guesses.

Number to guess : 98
Guessed value : 98 in 7 guesses.

Number to guess : 99
Guessed value : 99 in 6 guesses.

Number to guess : 100
Guessed value : 100 in 7 guesses.

Exiting....

* */

Add a comment
Know the answer?
Add Answer to:
Write a Java program that implements the number guessing game where the computer tries to guess...
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
  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • 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...

  • 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...

  • USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing...

    USE JAVA Problem 1: Guess a Number You are writing a program for a number guessing game. For playing "Guess a Number", the program will generate a number for player to guess from within a range specified by the player. The program will take as input the lowest number in the range and the highest number in the range from the player, and then a series of guesses of the form: <n,>n, = n, where n is the number guessed....

  • 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...

  • 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...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

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