Java Guess

In Writing Java Code - Methods we looked at the basics of Java in terms of the instructions used to build a program - if, while, for and so on. Now we've seen the basic data types that Java supports and been introduced to using Swing and the command line to interact with the user. It is time for a small example.

At this stage in learning any programming language it is difficult to find any convincing demonstration program and in the case of Java this is particularly so. You need to know a lot of Java and its frameworks like Swing to do anything impressive.

To show you a little more of how Java works let's implement an old favourite - number guess.

This works by asking you to pick a number between 1 and 100 and it then offers guesses as to the number you thought of.

The guessing procedure isn’t random, but a simple binary search. The program starts of with the full range of numbers from lower=1 to upper=100 and works out a guess in the middle of the range i.e.

guess=lower+(upper-lower)/2

It then asks if this is the number you have thought of. If the answer is Yes then the game is over but if it isn’t it asks if the number you are thinking of is bigger or smaller.

Using this information the program adjusts the range from upper to lower accordingly, i.e. either upper=guess or lower=guess, and the whole process repeats.

To implement this in Java we need to use a number of facilities that we haven’t discussed in detail and we need to use the control commands introduced in the previous chapter.

In particular you can repeat a block of code using

while(condition){block of instructions}

which will repeat the block until the condition is false, i.e. the loop keeps going “while the condition is true”. There is also an alternative form of loop:

do {block of instructions} while(condition)

which will always carry out the block of instructions at least once. Contrast this to the first loop which, if the condition is initially false, will skip the block altogether.

We also need a conditional statement that will carry out an instruction only if a condition is true. This is the Java if statement, the simplest form of which is:

if(condition) {block of instructions}

which will only carry out the block of instructions if the condition is true.

Finally, we also need some way to get input from the user. In previous examples we have used the command System.out.println() to print something on the screen.

The equivalent input command is System.in.read() but the complication is that this returns a four-byte integer which corresponds to the key or character code. To store this as a character we need to use a type conversion operation called a “cast" - more of converting from one data type to another in a later chapter.

So if ans is a variable of type char the command:

ans=(char) System.in.read();

will store the next character typed at the keyboard in it.

If only life were that easy! There is yet another complication.

System.in.read doesn’t return immediately you press the first key, it waits for you to type everything and then press carriage return. Then it returns with the first character on the line that you typed. When you next call System.in.read it returns the second character and so on until it reaches the carriage return. What this means is, that even if we are only trying to read in a single character, you have to allow for the carriage return!

So start a new Java project called guess and first we declare the variable we are going to use:

This starts the loop which continues until the user answers with a Y. Notice that != is the Java way of saying "not equal to". A new guess is generated and displayed to the user along with a set of instructions as to what to do next.

The user is expected to type a single character followed by a return:

do{ ans=(char) System.in.read();}while(ans=='\n');

This do loop keeps on reading in until the user enters something other than a single return. Once again notice that '\n' means the return key.

Finally we can respond to what the user typed.

if (ans =='B')lower=guess; if (ans =='S')upper=guess;}

If it was a B then the lower bound is the guess, and if it was an S the upper bound is the guess.

If it was a Y then the loop comes to an end and we can congratulate ourselves:

If you type this program in then it should guess any number between 1 and 100 in the minimum number of guesses.

The only part of the program not explained even a little bit is the use of

throws java.io.IOException

which is all to do with picking up run time errors and it is something we will have to return to. I also have to admit that this program would be easier to write using strings, arrays and classes, but more of all of these in the next chapter.

You should also notice that this simple program was in fact quite difficult to write because of all of the tiny details that you needed to know to make it work. Learning any programming langauge is a lot like this. The broad picture is always the same but small details like != meaning not equal and /n meaning the return key vary and you have to get everything 100% right for anything to work.

Play with this example and see if you can improve on it. Try writing a program which accepts any two numbers and displays their sum. Next try to extend this to any number of numbers with the last number indicated by a zero.