Question

In: Computer Science

Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...

Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is.

After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers.

(For example, if your range is 0 to 31, you should guess 16 and not 15, but if your range is 0 to 30 you should certainly guess 15).

The flow should look like the following:

Enter n: 32

Welcome to Guess My Number!

Please think of a number between 0 and 31.

Is your number: 16?

Please enter C for correct, H for too high, or L for too low.

Enter your response (H/L/C):

H Is your number: 8?

Please enter C for correct, H for too high, or L for too low.

Enter your response (H/L/C): L

Is your number: 12?

Please enter C for correct, H for too high, or L for too low.

Enter your response (H/L/C): C

Thank you for playing Guess My Number!

As part of your implementation, you should check that n is not 0 or negative. (You need not worry about the case where the user enters a non-integer). You should also check that the user is entering one of the letters H, L, or C each time your program makes a guess. This flow should look like the following: Enter n: -1

Enter a positive integer for n: 32

Welcome to Guess My Number!

Please think of a number between 0 and 31.

Is your number: 16?

Please enter C for correct, H for too high, or L for too low.

Enter your response (H/L/C): asdf

Enter your response (H/L/C): H   Is your number: 8? ...

You can assume that the user will always give honest answers.

Solutions

Expert Solution

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
   Scanner s = new Scanner(System.in);
       System.out.println("Enter n: ");
       int n = s.nextInt();
       while(n<=0)
       {
       System.out.println("Enter a positive integer for n: ");
       n = s.nextInt();
       }
       System.out.println("Welcome to Guess My Number!");
       System.out.println("Please think of a number between 0 and "+(n-1)+".");
       int low=0,high=n-1;
       String c;
       int guess;
       do
       {
       guess = (int)Math.ceil(((low+high)/2)+0.5);
       System.out.println("Is your number: "+guess+"?");
       System.out.println("Please enter C for correct, H for too high, or L for too low.");
       System.out.println("Enter your response (H/L/C): ");
       c=s.next();
       while(!(c.equals("H")||c.equals("L")||c.equals("C")))
       {
       s.next();
       System.out.println("Enter your response (H/L/C): ");
       c=s.next();      
       }
       if(c.equals("H"))
       {
       high=(low+high)/2;
       }
       else if(c.equals("L"))
       {
       low=(low+high)/2;
       }
       }while(!c.equals("C")&&guess!=0&&guess!=n-1);
       System.out.println("Thank you for playing Guess My Number!");
   }
}

OUTPUT:

Enter n:
32
Welcome to Guess My Number!
Please think of a number between 0 and 31.
Is your number: 16?
Please enter C for correct, H for too high, or L for too low.
Enter your response (H/L/C):
H
Is your number: 8?
Please enter C for correct, H for too high, or L for too low.
Enter your response (H/L/C):
L
Is your number: 12?
Please enter C for correct, H for too high, or L for too low.
Enter your response (H/L/C):
C
Thank you for playing Guess My Number!



Related Solutions

Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT