<<Previous | ToC
| Next >>

public static void main() {int boyz = 7, gulz = 3;if (boyz>gulz) System.out.println("Chauvinist!");else if (boyz<gulz) System.out.println("Feminist!");else System.out.println("Equality!");}

Let's look at the structure of those conditionals. Normally we want
the condition (inside the parentheses, which are required in Java) to be
a boolean value, either the result of a compare as here, or else a boolean
variable calculated at some other time, or perhaps a composite expression
value. You know, even before running this program -- and any decent Java
compiler probably does too -- that it's going to print out "`Chauvinist!`"
not "`Feminist!`". But now we are going to ask the human operator
for how many boys and girls there are, and the program actually does something
we couldn't know when it was written. Insert these four lines after the
declaration and before the first `println`:

System.out.print("How many boys? ");boyz = Zystem.ReadInt();System.out.print("How many girls? ");gulz = Zystem.ReadInt();

Ordinarily we would use "`System`" for both input and output,
but the Java folks made that unnecessarily complicated, so I made up a
fake "`Zystem`" to use in the beginning. Later on you can look through
what I did and see why. And then you won't need it any more. For today,
"`Zystem.ReadInt()`" gets a user-typed number from the console window.

Now after you compile and run it, it will stop and ask how many boys
and girls you want it to compute on. Type in the number 4 and press
Enter,
then the number 5 and press Enter
again. You have told the computer something it did not know, then it told
you something based on the information it had. Run it a few times and convince
yourself that you understand how it works.

The first thing you do when writing any real program is to stop and think about what needs to be done. Suppose you want the computer to guess a number between 1 and 8. It could ask you (eight times) "Is it 1, is it 2, is it 3, ..." but that's both tedious and unnecessary. So let's think about how to divide the search space into smaller portions. Although we start off with eight possible numbers, if we ask "Is it more than 4?" and the answer is "no" then we have reduced the choices to four, 1 to 4. Similarly, if the answer is "yes" we still have reduced the search space to half. So each time the computer wants to ask a question, it can divide its search space approximately in half, until there's only one number left. For an initial search space of eight items, that only takes three questions, like this:

int hi = 8, lo = 1, mid;char ans;System.out.println("Think of a number between " + lo + " and " + hi);mid = (hi+lo)/2; // initial test @ 4System.out.print("Is your number greater than " + mid + " (y/n)? ");ans = Zystem.ReadLetter();if (ans == 'y') lo = mid+1;else hi = mid;mid = (hi+lo)/2; // second test @ 2System.out.print("Is your number greater than " + mid + " (y/n)? ");ans = Zystem.ReadLetter();if (ans == 'y') lo = mid+1;else hi = mid;mid = (hi+lo)/2; // third test @ 1System.out.print("Is your number greater than " + mid + " (y/n)? ");ans = Zystem.ReadLetter();if (ans == 'y') lo = mid+1;else hi = mid;if (hi==lo) System.out.println("Your number is " + hi);else System.out.println("Oops something went wrong");

The first question will always be 4. Like Chomp, this is integer
(whole number) division, so half of (1+8=9) 4, not 4.5. Anyway, if the
result of the compare is greater, then the range becomes 5-8 and the second
question is 6, otherwise the range is reduced to 1-4, and the second question
is 2. Can you figure out what the four different possible third questions
are? What happens if you give it different starting numbers?

Let's look more closely at how this works.

Next: Using the BlueJ Debugger

<<Previous | ToC | Next >>

Revised: 2020 July 21