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
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...
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...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Hi, I need a program in C, where it will prompt a user for a math...
Hi, I need a program in C, where it will prompt a user for a math quiz, either easy or medium. If the user choses difficulty easy, it will prompt the user for how many questions they would like from 1-5. If the user enters medium it will prompt the user on how many questions they would like from 1-10. After those prompts are entered questions will be displayed using rand(), the questions must be multiple choice or division, aswell...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Scenario: A computer system allows a user to choose a password with a length of one...
Scenario: A computer system allows a user to choose a password with a length of one to eight characters. A system administrator needs to figure out how long a user’s password should expire. Assume that 1 million passwords can be tested per second by a hacker. The hacker can guess a user’s password 24 hours a day and 7 days a week. A password should be expired if it has an equal or greater than 10% probability of having been...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
(1) Ask the user to input a character. You will draw your right triangle with this...
(1) Ask the user to input a character. You will draw your right triangle with this character. (2) Ask the user to input the number of rows (integer). This will also signify the number of characters at the base of the triangle. (3) Use a nested loop to draw the triangle. The first line will only have one character but will now need to output enough spaces such that the character is output at the end of the row instead...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT