CSC15 Lab 7 Due one week from date assigned The lab asks you to design an algorithm around a while loop. For review, the basic form of a while loop is similar to an if statement: while ( condition ) { // code repeated while condition is true } The while loop executes its code if the boolean "condition" is true. After executing its code, it rechecks the condition, and executes the code again if it remains true. If condition is false, nothing is executed. If condition is true, then something inside the body of the while loop must eventually make the condition false. Otherwise the loop will run forever. int i; // loop index i = 0; while (i < 7) { System.out.println(i); i = i+1; } This above loop will execute 7 times, printing out the value of i from 0 to 6. IT'S IMPORTANT THAT YOU UNDERSTAND THAT IT PRINTS I FROM 0 TO 6, NOT 0 TO 7. When i reaches 7 the boolean condition becomes false, and so the body of the loop is not executed again. The condition is checked BEFORE each iteration of the loop starts. For this lab, you are to write a program where the user thinks of a number from 0 to 1023, and the computer must guess it. It's important to formulate the algorithm first, so you should sketch out the code on paper. The first guess for the computer should be 511 - halfway between the minimum and maximum possible numbers (average of the two numbers). The user answers if this guess is correct, less than the correct answer, or larger than the correct answer. The computer makes the next guess based on this response. The loop stops when the computer has guessed the correct answer. Let's say the computer's first guess is 511 and you answered that the real number is larger, that means the possible answer is somewhere between 511 and 1023. The computer's next guess should be halfway between 511 and 1023. If the user answered that it's smaller, than the next guess should be halfway between 0 and 511. So the general strategy is to use two variables to keep track of the range of possible values - the minimum and the maximum. Initially, minimum is 0 and the maximum is 1023. You figure out the rest. For input/output you can use either JOptionPane.showInputDialog or Keyboard.readInt (look at earlier programs you wrote). Since this program consists of only one function, you can just put everything in main. But you'll have to write the program completely from scratch this time. Here's a possible sample sesssion of your program: computer: think of a number between 0 and 1023 and I'll try to guess it. computer: is the number (l)ess than, (g)reater than, or (e)qual to 511? user: l computer: is the number (l)ess than, (g)reater than, or (e)qual to 255? user: g computer: is the number (l)ess than, (g)reater than, or (e)qual to 383? user: g .... computer: is the number (l)ess than, (g)reater than, or (e)qual to 455? user: e computer: I guessed it in 9 tries! I'm really smart! ---- Your program must also do the following: A. terminate when the minimum is >= to the maximum-1 (which means the user cheated and misled the computer). The computer should print out a message of indignation. This refinement means that the continuation condition of your while loop would include &&. (Note to those who know what I mean: you are not allowed to use "break" or "goto"). B. use an additional variable to keep track of how many guesses the computer had to make before getting it right. C. If there are 1000 possible numbers, it'll take the computer about 10 tries to guess the right number. If there are 500 possible numbers, it should take 9 tries. If there are 2000 possible numbers, it should take about 11 tries. Everytime the number of possibilities double, the number of times the computer may have to guess increases by one. Try to come up with a mathematical formula to describe this behavior. That is, suppose there are n possible number, how many guesses (in terms of n) should the computer have to make? Hint: to be precise, the computer should guess at most 3 times if there are 8 possible numbers. Put your answer to this question in comments at the end of your program. You'll find on the web page the complimentary program to this program, where the computer generates a random number and it's the human that has to do the guessing.