DOC PREVIEW
UW CSE 142 - Study Notes

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 of 2 CSE 142, Summer 2011 Programming Assignment #5: Guessing Game (20 points) due Tuesday, July 26, 2011, 11:00 PM This assignment focuses on while loops and random numbers. Turn in a file named GuessingGame.java. This program allows the user to play a game in which the program picks a random integer and accepts guesses from the user until the user guesses the number correctly. After each incorrect guess, the program will tell the user whether the correct an-swer is higher or lower. Your program must exactly reproduce the format and behavior of the logs in this document. The log below shows one sample execution of your program. Your output will differ depending on the random numbers cho-sen and user input typed, but the overall output structure should match that shown below. First, the program prints an introduction in the form of a haiku poem. Recall that a haiku has 3 lines: one with 5 syllables, the second with 7 syllables, and the third with 5 syllables. Next, a series of guessing games is played. In each game, the computer chooses a random number between 1 and 100 inclusive. The game asks the user for guesses until the cor-rect number is guessed. After each incorrect guess, the program gives a clue about whether the correct number is higher or lower than the guess. Once the user types the correct number, the game ends and the program reports how many guesses were made. After each game ends and the number of guesses is shown, the program asks the user if he/she would like to play again. Assume that the user will type a one-word string as the response to this question. A new game should begin if the user's response starts with a lower- or upper-case Y. For example, answers such as "y", "Y", "yes", "YES", "Yes", or "yeehaw" all indicate that the user wants to play again. Any other response means that the user does not want to play again. For example, responses of "no", "No", "okay", "0", "certainly", and "hel-lo" are all assumed to mean no. Once the user chooses not to play again, the program prints overall statistics about all games played. The total number of games, total guesses made in all games, average number of guesses per game (as a real number rounded to the near-est tenth), and best game (fewest guesses needed to win any one game) are displayed. Your statistics should be correct for any number of games or guesses ≥ 1. You may assume that no game will re-quire more than one million guesses. You should handle the special case where the user guesses the correct number on the first try. Print a message as follows: I'm thinking of a number between 1 and 100... Your guess? 71 You guessed it in 1 guess! Assume valid user input. When prompted for numbers, the user will type integers only, and they will be in proper ranges. << 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 higher. Your guess? 35 It's lower. Your guess? 30 It's higher. Your guess? 32 It's lower. Your guess? 31 You guessed it in 6 guesses! 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? 65 It's lower. Your guess? 64 You guessed it in 4 guesses! Play again? YES I'm thinking of a number between 1 and 100... Your guess? 60 It's lower. Your guess? 20 It's higher. Your guess? 30 It's higher. Your guess? 40 It's higher. Your guess? 50 It's lower. Your guess? 47 It's higher. Your guess? 49 You guessed it in 7 guesses! Play again? no Your overall results: Total games = 3 Total guesses = 17 Guesses/game = 5.7 Best game = 42 of 2 Implementation Guidelines: Define a class constant for the maximum number used in the games. The previous page's log shows games from 1 to 100, but you should be able to change the constant value to use a range from 1 to any maximum. Use your constant throughout your code and do not refer to the number 100 directly. Test your program by changing your constant and running it again to make sure that everything uses the new value. A guessing game for numbers from 1 to 5 would produce output such as that shown at left. The web site shows other expected output cases. As in Homework 2, we suggest that you add the constant last, after all of the other code works properly. Produce randomness using a single Random object, as seen in Chapter 5. Remember to import java.util.*; Display rounded numbers using the System.out.printf command or a rounding method of your own. Read user yes/no answers using the Scanner's next method (not nextLine, which can cause strange bugs when mixed with nextInt). To test whether the user's response represents yes or no, use String methods seen in Chapters 3-4 of the book. If you get an InputMismatchException, you are trying to read the wrong type of value from a Scanner. Produce repetition using while or do/while loops. You may also want to review fencepost loops from Chapter 4 and sen-tinel loops from Chapter 5. Chapter 5's case study is a relevant example. Some students try to avoid properly using while loops by writing a method that calls itself; this is not appropriate on this assignment and will result in a deduction. To help you solve the "best game" part of the program, you may want to read textbook section 4.2 on min/max loops. We suggest that you begin by writing a simpler version that plays a single guessing game. Ignore other features such as multiple games and displaying overall statistics. While debugging it is useful to print a temporary "hint" message like that shown at left. This way you will know the correct answer and can test whether the program gives proper clues for each guess. Style Guidelines: For this assignment you are limited to the language features in Chapters 1-5 shown in lecture and the textbook. Structure your solution using static methods that accept parameters and return values where appropriate. For full credit, you must have at least the following two specific methods other than main in your program: 1. a method to play one game with the user This method should not contain code to ask the user to play again. Nor should it play multiple games in one call. 2. a method to report the overall statistics to the user This method should print the statistics only, not do anything else such as while loops or playing


View Full Document

UW CSE 142 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?