Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too small
What's your guess? 75
75 is too big
What's your guess? 62
62 is too small
What's your guess? 69
69 is too big
What's your guess? 65
65 is too big
What's your guess? 63
63 is too small
What's your guess? 64
You've got it in 7 guesses. That was ok!
Goodbye!
The program performs the following actions:
| Number of guesses | Message |
|---|---|
| 1 | That was lucky! |
| 2-4 | That was amazing! |
| 5-6 | That was really good! |
| 7 | That was ok! |
| 8-9 | That was pretty bad! |
| 10 or more | This is not your game! |
int x = (int)(100 * Math.random ()) + 1;
Math.random is a function that returns a pseudo-random double number
greater than or equal to 0.0 and less than 1.0. The (int) notation is called
a cast and truncates to an integer the value of the expression following it.
Do you want to play a game? y
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too small
What's your guess? 75
75 is too big
What's your guess? 62
62 is too small
What's your guess? 69
69 is too big
What's your guess? 65
65 is too big
What's your guess? 63
63 is too small
What's your guess? 64
You've got it in 7 guesses. That was ok!
Do you want to play another game? y
I am thinking of a number between 1 and 100. Try to guess it.
What's your guess? 50
50 is too big
What's your guess? 25
25 is too small
What's your guess? 37
37 is too small
What's your guess? 43
You've got it in 4 guesses. That was amazing!
Do you want to play another game? n
Goodbye!
The difference between the programs in Part 1 and Part 2 is that now the program repeatedly asks the user if he/she wants to play a game; if the user enters anything but 'y', the program terminates; otherwise the program allows the user to play a game before asking again if the user wants to play another game.