Sie sind auf Seite 1von 5

Design and implement an application that plays a game that will allow a person to

increase her/his skills in guessing the next term in a sequence of numbers, a typical
question in an IQ test. Before the game begins, the application will allow the user to
select the level of difficulty, (Beginner, Intermediate, Advance). The level (s)he selects
must control the difficulty of the sequence as well as the number of attempts the user is
allowed per game.

The application generates a sequence of integers t1, t2, t3, t4, t5,......., using the following
formula:

ti= a*ib + c

where ti represents the ith term of the sequence. The coefficients a, b and c are integers.

The number of attempts, the difficulty of the sequence and the number of initial terms
displayed to the user according to the level of difficulty is as follows:

Number Number Coefficients


Level of
Level of
Number Initial Attempts a b c
Terms
Random number Random number
Beginner 1 8 15 1
between 1 and 4 between 1 and 20
Random number Random number
Intermediate 2 5 10 2
between 4 and 8 between 1 and 50
Random number Random number
Advance 3 3 5 3
between 8 and 12 between 1 and 100

At the beginning of the program, the program asks the user which the level of difficulty
(s)he wants to play the game. If the user inputs an incorrect value, the program should
keep asking the user for values until the user inputs a valid value for the level of
difficulty.

Afterwards, the program shows the first terms of the sequence according to the level of
difficulty and asks the user to enter the next term of that sequence. If the user does not
enter the correct answer, the application displays a message indicating that the user's
answer is wrong and prompts the user for another answer. The application continues
asking until either the user enters the correct answer or the number of permitted attempts
is exceeded. At the end of each run of the game, the application will show the formula
that generates the sequence and the next term of the sequence if the user has not entered
the correct answer. Afterwards, the program prompts the user whether (s)he wants to play
another run of the game or to stop. If the user enters n or N, the game terminates. On the
other hand, if the user wants to continue playing and has guessed correctly the next term
in the previous run of game, the program will prompt the user if (s)he wants to be play at
the next game level, unless (s)he already is playing at the advance level. If the user
answers y or Y, the new run of the game will be played one level of difficulty higher than
the previous run.

Once the user has decided to stop playing altogether, your application will report some
statistics such as number of games the user has won, the total number of games played
and the win percentage (correctly formatted).

Example:
The text in purple is the program’s output, the text in black corresponds to the answers
entered by the user.

Choose your difficulty level?


1-Beginner (15 guesses), 2-Average (10 guesses), 3-Expert (5 guesses)
Enter choice (1/2/3):0
Incorrect level of difficulty
Choose your difficulty level?
1-Beginner (15 guesses), 2-Average (10 guesses), 3-Expert (5 guesses)
Enter choice (1/2/3):5
Incorrect level of difficulty
Choose your difficulty level?
1-Beginner (15 guesses), 2-Average (10 guesses), 3-Expert (5 guesses)
Enter choice (1/2/3):1

Level 1
Sequence:23,26,29,32,35,38,41,44....
What is the next term in this sequence?
Attempt #1.Enter your answer:47
Congratulations!!
The sequence formula is :3i^1 + 20
Play again (y/n)?:y
Do you want to play now at the intermediate level(y/n)?y

Level 2
Sequence:52,73,108,157,220....
What is the next term in this sequence?
Attempt #1.Enter your answer:250
Sorry, wrong answer.
Attempt #2.Enter your answer:270
Sorry, wrong answer.
Attempt #3.Enter your answer:297
Congratulations!!
The sequence formula is :7i^2 + 45
Play again (y/n)?:y
Do you want to play now at the advance level(y/n)?n

Level 2
Sequence:49,67,97,139,193....
What is the next term in this sequence?
Attempt #1.Enter your answer:201
Sorry, wrong answer.
Attempt #2.Enter your answer:245
Sorry, wrong answer.
Attempt #3.Enter your answer:250
Sorry, wrong answer.
Attempt #4.Enter your answer:257
Sorry, wrong answer.
Attempt #5.Enter your answer:259
Congratulations!!
The sequence formula is :6i^2 + 43
Play again (y/n)?:y
Do you want to play now at the advance level(y/n)?y

Level 3
Sequence:47,103,255....
What is the next term in this sequence?
Attempt #1.Enter your answer:300
Sorry, wrong answer.
Attempt #2.Enter your answer:350
Sorry, wrong answer.
Attempt #3.Enter your answer:385
Sorry, wrong answer.
Attempt #4.Enter your answer:423
Sorry, wrong answer.
Attempt #5.Enter your answer:453
Sorry, wrong answer.
The next term is :551.The sequence formula is :8i^3 + 39
Play again (y/n)?:n

Summary
Number of Games: 4
Number of Wins: 3
Win percentage: 0.75

Additional Requirements:

• The implementation of this application will use several loop statements. A


mandatory requirement that must be fulfilled by your Java code is to use a loop
statement to generate and display the first terms of the sequence.
• In order to facilitate the testing of your program, you must include a Boolean
variable called testingMode initialized to true. If the value of the variable
testingMode is true, your program will display the next term of the sequence just
after the program asks the user for that next term. For example, a run of the
program in testingMode will display the following:
Level 1
Sequence:23,26,29,32,35,38,41,44....
What is the next term in this sequence? (next term 47)
Attempt #1.Enter your answer:47
Congratulations!!
The sequence formula is :3i^1 + 20
Play again (y/n)?:y
Do you want to play now at the intermediate level(y/n)?y
On the other hand, if the value of the variable testingMode is set to false, the
program will not show the value of the next term of the sequence. After you have
concluded the testing of your program, initialized the value of the variable
testingMode to false and compile your program.

• The program must validate the level of difficulty entered by the user at the
beginning of the game. When the user inputs an invalid value, the program should
ask the user to enter a valid value. Be sure the program displays an error message
that indicates to the use how (s)he missed it and/or what the valid values are. The
program should continue asking until the user inputs a valid value.
• The name of your class must be SequenceGame and the name of your java source
code file SequenceGame.java.

Hints:

• Use the symbol ^ to represent exponentiation when the application displays the
formula of the sequence. For example, the expression 2* i4 will be display as 2i^4.
• Use the method random of the class Math to compute a random integer between
two integers a and b in the following manner:

int a=5;
int b=10;

int random =(int) (Math.random()*(b - a + 1 )) + a;

The method random of the class Math returns a double random number greater
than or equal to zero and less than 1. Multiplying the random number returned by
the random method by the difference of b minus a plus 1 and casting its result as
an integer will generate an integer between 0 and b - a . After adding the value a
to this result, we will get a random integer between the integers a and b.

• Use the method pow of the class Math to compute the power of a number. The
method pow, Math.pow(a,b), returns a double value of the first argument raised
to the power of the second, ab .For example, to compute 23 as a double, we can
write the following java code:
int a=2, b=3;

double c=Math.pow(a,b);

• To generate a term of the sequence properly, you may need to apply data
conversions.
• The method printf displays output in a formatted manner. The method printf
takes as first argument a string that indicates the format of the output to be
displayed, followed by a comma-separated list of the values that will be displayed.
For example, if you want to display a message followed by a floating value with
exactly two decimal digits, you should do the following:
double aValue=2.876847984;

System.out.printf("%s %.2f","The value with 2 decimal digits is:",aValue);

The previous code will display:

The value with 2 decimal digits is: 2.88

Please notice that the method printf automatically rounds the number to the
second decimal place.