<%@LANGUAGE="VBSCRIPT" %> <% Response.CacheControl = "no-cache" %>
Lab 05 (Due by END of Lab on Feb 12)

Overview

This lab will give you practice using loops, decisions, language-defined methods, and programming-oriented problem-solving.

Background

Java provides the capability to produce random numbers for you via the following command:

int randomNumber = new Random().nextInt(10);

The number 10 sets the range of numbers that are possible; in this case the number that is produced will be a random number from from between 0 and 10. The random number that was generated is stored in the variable called randomNumber.

To set a number between 0 and 100, the code would be:

int randomNumber = new Random().nextInt(100);

Assignment

Write a program in which the computer guesses a number between 0 and 10 at random so it can try to guess a magic number you've chosen. List the number of guesses it takes for the computer to guess your number.

Set the magic number as an integer of your choice at the beginning of your program. Then, in a loop, have the computer generate a random number each time it iterates through the loop. Compare the generated random number to the magic number. If it matches, exit the loop; if it doesn't, keep the loop going until the numbers match. Print the results.

Example output:

David Whittinghill
Lab 05 - Guess the Magic Number

(Shhh, don't tell Computer, but the magic number is: 5)

Is the number: 3?
No.
Is the number: 8?
No.
Is the number: 6?
No.
Is the number: 5?
Congratulations, Computer! After 4 guesses, you guessed the magic number of 5

OR (Note: the output will be different every time the program is run.

David Whittinghill
Lab 05 - Guess the Magic Number

(Shhh, don't tell Computer, but the magic number is: 5)

Is the number: 1?
No.
Is the number: 1?
No.
Is the number: 0?
No.
Is the number: 0?
No.
Is the number: 9?
No.
Is the number: 4?
No.
Is the number: 2?
No.
Is the number: 9?
No.
Is the number: 8?
No.
Is the number: 6?
No.
Is the number: 5?
Congratulations, Computer! After 11 guesses, you guessed the magic number of 5


Grading

You will be graded on whether or not your program works as specified in the assignment, the readability of your code, and the correctness of your output. You may not use any API Objects or calls other than println, Random, and nextInt.