The goal of this lab is to write a class GuessingNumber that plays a
number guessing game with the user.
Using the Math.random() it is possible to generate a random integer
between 1 and an integer N. Since Math.random() produces a random
real number between 0 and 1, excluding 1, Math.random()* N produces
a random real number between 0 and N, excluding N itself. So, the
formula (int)(Math.random() * N) produces a random integer between
0 and N-1, and thus, 1 + (int)(Math.random() * N) produces a random
integer between 1 and N.
The game will be played by a void method called playGame that takes
two parameters, the maximum number (called maximum) and the number
of rounds (called noRounds). The way the method works is as follows:
The program generates a secret random number according to the
formula using the formula in the above.
The method explains the nature of the game to the user.
The user then makes a guess at most noRounds times. The
program uses a for-loop to count down from noRounds to 1.
In the loop-body, if the guess is already equal to the number
secret, nothing will happen; otherwise, the user is asked
to make a guess and the program receives the guess, and then
if the guess is greater or smaller than the secret number
the program tells the user which side the guess has fallen.
Here is an example of execution with the two parameter values, 2000 and 10: