Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
190 views

Assignment 2 - Java

The document describes a Java program that plays a "Guess the Number" game. The program randomly generates a number between 1-100 and prompts the user to guess it. With each guess, it tells the user if their guess is too high, too low, or correct. If correct, it rates their performance based on number of guesses. It then asks if they want to play again and ends with a goodbye message. The program is meant to validate user input, generate a random number, and provide feedback on each guess according to the specified rules and messages.

Uploaded by

sunnyxm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

Assignment 2 - Java

The document describes a Java program that plays a "Guess the Number" game. The program randomly generates a number between 1-100 and prompts the user to guess it. With each guess, it tells the user if their guess is too high, too low, or correct. If correct, it rates their performance based on number of guesses. It then asks if they want to play again and ends with a goodbye message. The program is meant to validate user input, generate a random number, and provide feedback on each guess according to the specified rules and messages.

Uploaded by

sunnyxm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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