You are on page 1of 2

COSC 2050 (JAVA Programming I)

Assignment 2, (Due 01/27/2014, before class)

Guess a number from 1 to 100


Console
Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess again. Enter number: 30 Too high! Guess again. Enter number: 15 Too low! Guess again. Enter number: 23 Too high! Guess again. Enter number: 19 Too low! Guess again. Enter number: 21 Too high! Guess again. Enter number: 20 You got it in 7 tries. Not too bad! You've got some potential. Try again? (y/n): Error! This entry Try again? (y/n): Error! Entry must Try again? (y/n): is required. Try again. x be 'y' or 'n'. Try again. n

Bye - Come back soon!

Operation
The application prompts the user to enter an int value from 1 to 100 until the user guesses the random number that the application has generated. The application displays messages that indicate whether the users guess is too high or too low.

Page 1 of 2

When the user guesses the number, the application displays the number of guesses along with a rating. Then, the application asks if the user wants to play again. When the user exits the game, the application displays a goodbye message. If the users guess is more than 10 higher than the random number, the application should say, Way too high! If the users guess is higher than the random number, the application should say, Too high! If the users guess is lower than the random number, the application should say, Too low! The message thats displayed when the user gets the number should vary depending on the number of guesses. For example:
Number of guesses ================= <=3 >3 and <=7 >7 Message ======= Great work! You are a mathematical wizard. Not too bad! You've got some potential. What took you so long? Maybe you should take some lessons

Specifications

When the user guesses a number, the application should only accept numbers from 1 to 100. When the user responds to the Play Again prompt, the application should only accept a value of y or n. If the user enters invalid data, the application should display an appropriate error message and prompt the user again until the user enters valid data. The code thats used to validate data should be stored in separate methods. For example:
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max) public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)

Use the random method of the java.lang.Math class to generate a random number.

Page 2 of 2

You might also like