CSE142—Computer Programming I
Programming Assignment #5
Due: Tuesday, 5/5/09, 9 pm
This assignment will give you practice with while loops and pseudorandom numbers. You will write a program that lets the user play a simple guessing game in which your program thinks up a random integer and allows the user to make guesses until the user gets it right. For each incorrect guess the program tells the user whether the right answer is higher or lower. Your program is required to reproduce exactly the format and behavior of the log of execution at the end of this write-up.
You are to come up with your own introduction to the game in the form of a haiku. Recall that a haiku has three lines of text: a line with five syllables followed by a line with seven syllables followed by a line with 5 syllables. This is the creative part of this assignment.
At a minimum, your program should have the following static methods in addition to main:
· a method that introduces the game with your haiku
· a method to play one game with the user (just one game, not multiple games)
· a method to report overall results to the user
You may define more methods if you find it helpful, but you will find that the limitation that methods can return only one value will tend to limit how much you can decompose this problem.
You are to define a class constant for the maximum number used in the guessing game. The sample log shows the user making guesses from 1 to 100, but the choice of 100 is arbitrary. By introducing a constant for 100, you should be able to change just the value of the constant to make the program play the game with a range of 1 to 50 or a range of 1 to 250 or some other range starting with 1.
When you ask the user whether or not to play again, you should use the “next()” method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter “y” or the letter “Y”. Notice that the user is allowed to type words like “yes”. You are to look just at the first letter of the user’s response and see whether it begins with a “y” or “n” (either capitalized or not) to determine whether to play again.
Assume that the user always types an integer when guessing, that the integer is always in an appropriate range and that the user gives you a one-word answer beginning with “y”, “Y”, “n” or “N” when asked whether to play again.
You will notice at the end of the log that you
are to report various statistics about the series of games played by the
user. You are to report the total number
of games played, the total number of guesses made (all games included), the
average number of guesses per game, and the best (fewest) number of guesses
used in any single game. The average
number of guesses per game should be rounded to two decimal places using System.out.printf.
Here are a few helpful hints:
You should handle the case where the user
guesses the correct number on the first try.
Print the following message:
You got it right in 1 guess
We normally require you to keep your methods
short and to have a very short main. This
program is more difficult to decompose into methods, so you may end up having somewhat
longer methods. You can also include more code in your main method than usual. In particular, you are required to have a
while loop in main that plays multiple games and prompts the user for whether
or not to play another game. You
shouldn’t have all your code in main because you are required to have the
methods described at the beginning of this write-up.
We will once again expect you to use good
programming style and to include useful comments throughout your program. We will expect you to make appropriate
choices about when to store values as int versus double, which if/else constructs to use, what parameters to pass,
and so on.
For this assignment you are limited to the
language features in Chapters 1-5 shown in lecture or the textbook, although
you are not allowed to use the break statement or what are
described as “forever loops.”
Use whitespace and indentation properly. Limit lines to 100 characters. Give meaningful names to methods and
variables, and follow Java's naming standards.
Localize variables whenever possible.
Include a comment at the beginning of your program with basic description
information and a comment at the start of each method. Some students try to achieve repetition without properly
using while loops, by writing a method that calls itself; this is not
appropriate on this assignment and will result in a deduction in points
Your program should be stored in a file called Guess.java.
Log of execution (user input bold and underlined)
<< your haiku intro message here >>
I'm thinking of a number between 1 and 100...
Your guess? 50
It's lower.
Your guess? 25
It's lower.
Your guess? 10
It's lower.
Your guess? 5
It's higher.
Your guess? 7
You got it right in 5 guesses
Do you want to play again? y
I'm thinking of a number between 1 and 100...
Your guess? 50
It's higher.
Your guess? 75
It's lower.
Your guess? 60
It's higher.
Your guess? 67
It's lower.
Your guess? 63
It's lower.
Your guess? 62
It's lower.
Your guess? 61
You got it right in 7 guesses
Do you want to play again? YES
I'm thinking of a number between 1 and 100...
Your guess? 50
It's higher.
Your guess? 75
It's higher.
Your guess? 90
It's lower.
Your guess? 80
It's higher.
Your guess? 85
It's higher.
Your guess? 87
It's lower.
Your guess? 86
You got it right in 7 guesses
Do you want to play again? nope
Overall results:
total games = 3
total guesses = 19
guesses/game = 6.33
best game = 5