SL Task: Beginning Java: The Guessing Game

Upload your Guessing Game code is on your website and make sure that the indentation is correct and, if possible, your code is highlighted according to the syntax. (Try this method: Syntax highlighting in Google Sites.)

Also make sure that you have added comments to the code, to make it clear what each part of it is doing. You should add a comment to explain the whole routine, a comment to explain any loops, and comments to explain any key lines of code. If in doubt, add a comment. In my opinion you can have as many as one comment per line of code.

Now try to write the code using a very high-level pseudocode (ie like lines of English). You might start of something like:

Generate a random number
Get the first guess from the user

Finally, answer these questions:

  1. What would happen if we hadn’t got another guess from the user at the end of the while loop?
  2. There is another type of while loop in Java, whose loop condition is tested at the end of the loop, not at the beginning. So instead of while (condition) { stuff() }, it’s do { stuff() } while (condition). Rewrite your main loop using this type of while loop. You may find it convenient to use a boolean variable as a flag, or alternatively make use of the break statement.
  3. See if you can improve your guessing game in the following ways:
    1. When the user guesses correctly, tell them how many guesses it took.
    2. Allow the user to set the range of numbers from which the program’s random number will be generated.
  4. If you finish all of that, search for pseudocode on this blog and start reading the IB’s guidelines on pseudocode. See if you can re-write your game in approved pseudocode.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s