Java Programming: The program will figure out a number chosen by a  user. Ask user to think of a number from 1 to 100. The program will make guesses and the user will tell the program to guess higher (h) or lower (l). Sample run of the program might look like below : Guess a number from 1 to 100. (here, the user is thinking of 81) Is it 50?  (h/l/c): h Is it 75?  (h/l/c): h Is it 88?  (h/l/c): l Is it 81?  (h/l/c): c Great! Do you want to play again? (y/n): y Guess a number from 1 to 100. (here the user is thinking 37) Is it 50?  (h/l/c): l Is it 25?  (h/l/c): h Is it 37?  (h/l/c): c Great! Do you want to play again? (y/n): n our program should implement the binary search algorithm.  Every time the program makes a guess it should guess the midpoint of the remaining possible values.  Consider the first example above, in which the user has chosen the number 81: On the first guess, the possible values are 1 to 100. The midpoint is 50. The user responds by saying “higher” On the second guess the possible values are 51 to 100. The midpoint of 75. The user responds by saying “higher” On the third guess the possible values are 76 to 100. The midpoint is 88. The user responds by saying “lower” On the fourth guess the possible values are 76 to 87. The midpoint is 81. The user responds by saying “correct”   The main function must look like this, call all functions in main: public static void main(String[] args) {      boolean playAgain = true; while (playAgain) { playOneGame(); playAgain = shouldPlayAgain(); } }   The playOneGame function should have a return type of void. It should implement a complete guessing game on the range of 1 to 100 inclusive.  Both 1 and 100 should be valid guesses for the human user to make. The shouldPlayAgain function should have a boolean return type. It should prompt the user to determine if the user wants to play again, read in a character, then return true if the character is a ‘y’, and otherwise return false. In addition, you should implement the helper functions getUserResponseToGuess, and getMidpoint. They should be invoked inside your playOneGame function. getUserResponseToGuess. This function should prompt the user with the phrase “is it ? (h/l/c): “  with the value replacing the token . It should return a char. The char should be one of three possible values: ‘h’, ‘l’, or ‘c’.  It should have the following signature: public static char getUserResponseToGuess(int guess) getMidpoint. This function should accept two integers, and it should return the midpoint of the two integers. If there are two values in the middle of the range then you should consistently choose the smaller of the two. It should have the following signature:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Programming:

The program will figure out a number chosen by a  user. Ask user to think of a number from 1 to 100. The program will make guesses and the user will tell the program to guess higher (h) or lower (l).

Sample run of the program might look like below :

Guess a number from 1 to 100. (here, the user is thinking of 81)

Is it 50?  (h/l/c): h

Is it 75?  (h/l/c): h

Is it 88?  (h/l/c): l

Is it 81?  (h/l/c): c

Great! Do you want to play again? (y/n): y

Guess a number from 1 to 100. (here the user is thinking 37)

Is it 50?  (h/l/c): l

Is it 25?  (h/l/c): h

Is it 37?  (h/l/c): c

Great! Do you want to play again? (y/n): n

our program should implement the binary search algorithm.  Every time the program makes a guess it should guess the midpoint of the remaining possible values.  Consider the first example above, in which the user has chosen the number 81:

On the first guess, the possible values are 1 to 100. The midpoint is 50.

The user responds by saying “higher”

On the second guess the possible values are 51 to 100. The midpoint of 75.

The user responds by saying “higher”

On the third guess the possible values are 76 to 100. The midpoint is 88.

The user responds by saying “lower”

On the fourth guess the possible values are 76 to 87. The midpoint is 81.

The user responds by saying “correct”

 

The main function must look like this, call all functions in main:

public static void main(String[] args) {

     boolean playAgain = true;

while (playAgain) {

playOneGame();

playAgain = shouldPlayAgain();

}

}

 

The playOneGame function should have a return type of void. It should implement a complete guessing game on the range of 1 to 100 inclusive.  Both 1 and 100 should be valid guesses for the human user to make.

The shouldPlayAgain function should have a boolean return type. It should prompt the user to determine if the user wants to play again, read in a character, then return true if the character is a ‘y’, and otherwise return false.

In addition, you should implement the helper functions getUserResponseToGuess, and getMidpoint. They should be invoked inside your playOneGame function.

getUserResponseToGuess. This function should prompt the user with the phrase “is it <guess>? (h/l/c): “  with the value replacing the token <guess>. It should return a char. The char should be one of three possible values: ‘h’, ‘l’, or ‘c’.  It should have the following signature:

public static char getUserResponseToGuess(int guess)

getMidpoint. This function should accept two integers, and it should return the midpoint of the two integers. If there are two values in the middle of the range then you should consistently choose the smaller of the two. It should have the following signature:

public static int getMidpoint(int low, int high)

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education