Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write the program using java. Write a program that guesses the number you have in your mind! For this program, we will use an
Please write the program using java.
Write a program that guesses the number you have in your mind! For this program, we will use an algorithm called "Binary Search" as we discussed in class. The game goes like this, you pick a number between 1 and 100, the program asks you if the number is 50, if it is, it wins! If it isn't it will ask you whether the number, you have in mind is less than 50. Based on your response, it will ask you another question and keep repeating it for a total number of 5 times. If the program cannot guess your number by the fifth attempt it will print out "Aah OK, you win I guess :(" otherwise it will print out "I WIN!!!" and the number of times it guessed your number. A sample Q&A is : ====== ======= Pick a number between 1 and 100 Is the number you picked 50? no Is the number less than 50? yes Is the number you picked 25? no Is the number less than 25? no Is the number you picked 37? yes I WIN!! Number of guesses : 3 =========EEEEEEEEE EEEEEEEEEEEEEEEE Another sample game : ============== Pick a number between 1 and 100 Is the number you picked 50 ? no Is the number less than 50? no Is the number you picked 75? no Is the number less than 75? yes Is the number you picked 62? no Is the number less than 62? no Is the number you picked 68? no Is the number less than 68? no Is the number you picked 71? for this program, you need a variable to keep track of the number of guesses. Call it numGuesses for example. You also need 3 integer variables to keep track of the current guessed value, lower bound, and upper bound. Call them low, hight, and guess and define both on top of your program Hint: After defining the variables and printing the initial messages, you should use a loop to keep prompting the user. In that loop you should have 3 if-else statements. If the guessed number is the same as what the user had in mind, you are done, and you should print the victory message and break. If the guess isn't right and the number that the user guessed is less than the guessed number, set high = guess; else low = guess; Finally set guess = low + (high-low) / 2; After the for loop is done without breaking, it means that the algorithm couldn't guess the number correctly check for the last answer that user gave with if (answer.equalsignoreCase("yes")) and print out "Aah OK, you win :(" Test your program multiple times with different values You can use the provided sample file to solve this problem or you may start from scratch and use the provided file as a referenceStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started