Question

Task 2C: User picks an integer in a range and computer guesses by randomly This approach...

Task 2C: User picks an integer in a range and computer guesses by randomly This approach is similar to Task 2B. The only difference is, the computer will generate a random integer in the range as a guess. In this strategy, a guess is a random int in a given range. Hence, for the same range and same answer, the number of guesses will be different. For example, suppose the range is in [0, 10] and the answer is 7.

These are possible outputs.

The following running takes 8 tries to get the answer.

Enter left end in range: 0

Enter right end in range: 10

User has an int in [0, 10].

Computer will guess. guess #1: 10. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 1

guess #2: 1. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 2

guess #3: 9. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 1

guess #4: 2. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 2

guess #5: 4. How is my guess? 1. too big 2. too small 3. just right

guess #6: 8. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 1

guess #7: 5. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 2

guess #8: 7. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 3 Congratulations! The answer is 7.

In the following running, it takes only three guesses to get the answer.

Enter left end in range: 0

Enter right end in range: 10

User has an int in [0, 10].

Computer will guess. guess #1: 6. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 2

guess #2: 8. How is my guess? 1. too big 2. too small 3. just right

Enter only 1, 2, or 3: 1

guess #3: The answer must be 7.

Homework Answers

Answer #1

Hi,

Hope you are doing fine. I have coded the aabove question in jaava keeping all the conditions in mind. Note that, nowhere in the question it is mentioned that we must check if the user is lying, hence we assume that the user always provides valid input. The code has been clearly explained using comments that have been highligted in bold. Also find the code snippet and sample output for a clearer picture of the code.

Program:

import java.util.Random;
import java.util.Scanner;

public class randomNumber {

   public static void main(String[] args) {
      
       //creating scanner to take input
       Scanner sc=new Scanner(System.in);
       //declaring variables to store    range
       int left_end,right_end;
       System.out.println("Enter left end in range: ");
       //taking input of left_end from user
       left_end=sc.nextInt();
       System.out.println("Enter right end in range: ");
       //taking input of right_end from user
       right_end=sc.nextInt();
       //declaring variable for guessed number, choice of user and count of guesses
       int guess,choice,count=0;
       //loop that breaks when number is guessed
       while(true)
       {
           //generating a random guess within the range of lleft_end and right_end (both inclusive)
           guess=left_end + (int)(Math.random() * ((right_end - left_end) + 1));
           //increasing count
           count++;
           //printing result
           System.out.println("guess #"+count+": "+guess+". How is my guess? 1. too big 2. too small 3. just right");
           System.out.print("Enter only 1, 2, or 3: ");
           //Taking user choice
           choice=sc.nextInt();
           //if the choice is 3 then it means the number is guessed and the loop is broken
           if(choice==3)
           {
               System.out.println("Congratulations! The answer is "+guess+".");
               break;
           }
           //if the choice is 1, it means that we must decrease the upper range
           else if(choice==1)
           {
               right_end=guess-1;
           }
           //if the choice is 2, it means we must increase the lower range
           else if(choice==2)
           {
               left_end=guess+1;
           }
           //after changing the right_end and left_end, if they are equal, it means that the number picked by user is same as them
           if(right_end==left_end)
           {
               count++;
               System.out.println("guess #"+count+": The answer must be "+right_end);
               break;
           }
       }

   }

}

Executable code snippet:

Sample Outputs:

Know the answer?
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
Similar Questions
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
Hi I need to make a hangman game for school task and it is no an...
Hi I need to make a hangman game for school task and it is no an ordinary game. It has to incorporate the pictures and everytime something wrong is guessed it has to clock over the pictures. The pictures are: hangman_pics = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| |...
Create a very simple TCP Application in JAVA. You will create a client and a server....
Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do. The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to...
This is a very short trivia game. See if the user can guess the year that...
This is a very short trivia game. See if the user can guess the year that MDC (formerly Dade County Junior College) first started offering classes (1960). If the user guesses incorrectly, ask if they want another attempt. Once the user gets it right, report the number of guesses needed and end the program. Initial Output: MDC was originally named Dade County Junior College. Output (per loop) In what year were classes first offered? [user types: 1980] That answer is...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
Write a program that asks the user for a number in the range 1 through 12....
Write a program that asks the user for a number in the range 1 through 12. The program should display the corresponding month of the year, where 1 = January, 2 = February, 3 = March, 4 = April, 5 = May, 6 = June, 7 = July, 8 = August, 9 = September, 10 = October, 11 = November, and 12 = December. The program should display an error message if the user enters a number that is outside...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
How do I break down a user inputted integer to individual digits and store it into...
How do I break down a user inputted integer to individual digits and store it into my array? I have an if statement that compares the spots of the array to check if it is a palindrome, but I need to make it so the user inputted number is broken up individually into the array. Please break it down into steps so I can understand. #include<iostream> using namespace std; int main() {    char array[6];           cout <<...
Task 6: Create a function in Java that counts the number of even digits in a...
Task 6: Create a function in Java that counts the number of even digits in a positive integer. public static int countEvenDigits(int a); // Ex, countEvenDigits(1234) => 2 Task 7: Create a generic function that returns digits in a given range. The ones place corresponds to power 0, the tens place corresponds to power 1, the hundreds place corresponds to power 2, and so on. Think 10^3 => 1000 => thousands place. (Do not use exponents in your code.) //...