Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment - Number Guessing Game Java Code In this assignment you will write a program in that can figure out a number chosen by a

Assignment - Number Guessing Game

Java Code

In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.

A sample run of the program might look like this (numbers do not need to match exactly) :

Guess a number between 1 and 100.

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

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

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

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

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

Guess a number between 1 and 100.

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

Notice that the user inputs the characters h, l, and c for higher, lower, and correct, respectively.

Your 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 87.

The user responds by saying lower

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

The user responds correct

The exact numbers guess may vary by one, based on implementation. The midpoint between 75 and 100 might be either 87 or 88, for example.

Requirements

The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your solution should be heavily structured. The main function must look like this:

public static void main(String[] args) {

do {

playOneGame();

} while (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.

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 chose the smaller of the two. It should have the following signature:

public static int getMidpoint(int low, int high)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions