Date Assigned: Wednesday, March 16, 2005
Due Date: Wednesday, March 30, 2005, before the beginning of your lab section.
Goals: to learn to design an application with a GUI and to use a random-number generator.
Points: This assignment is worth a total of 20 points. No late assignments will be accepted, but credit will be given for partially completed assignments turned in on time.
Assignment: A Graphical User Interface (GUI) adds an attractive front-end to a software application and often makes it easier for the user to interact with the application. In this assignment, you will be creating a number-guessing game application with a GUI window. The application invites a user to type in an integer between 1 and 10 into a field in the window, and then to press a button displayed in the window in order to submit the number. In response to the event (the user clicking on the button to submit the number) the application generates its own random number in the same range 1-10, and compares the number generated with the number entered by the user. If the two numbers match, the application displays a response message in the window informing the user of the successful match. If the two numbers do not match, the application displays a response message in the window informing the user of the failed match. The user may play the game again by selecting another button in the window that clears the user input field and the response message.
Providing interaction with the user through a GUI interface is an example of stimulus-response, or a dialog. The application offers instructions to the user, and the user enters a number. In response to the number entered, the application generates its own number, compares it to the user’s number, and provides information about the success or failure of the match. This dialog repeats as long as the user chooses to play the game again.
Specific details are as follows:
1. For this application, all code may be included within the application class. Write the code to display a GUI window with the following components:
· Text message welcoming the user and displaying the instruction for the user to enter an integer number between 1 and 10. Use JLabel class.
· Data entry field to accept user input. Use JTextField class.
· Button for the user to select in order to submit the number input. Use JButton class. The string that appears in this button on the screen should be “Enter”.
· Button for the user to select in order to play the game again. Use JButton class. You may choose the string that appears in the button. In the example below, the string inside this button is “Play Again”.
· Text message displaying the results of the comparison between the number provided by the user and the random number generated by the application. Use JLabel class.
2. Within the application class, define a static class implementing the ActionListener interface with an actionPerformed( ) method in order to handle button events by responding to the user’s button selection. You may use the same button handler to respond to both buttons by using the getActionCommand( ) method (see p. 410 in your textbook). That method returns the string displayed on the button and you can distinguish actions to take based on which button was selected. Or, you may define two different button handlers, with each one responding to one button.
3. Although it is impossible to generate a truly random number, we can obtain a pseudorandom number which will serve our purposes for this application by using one of the Java utility classes. Refer to p. 123 in your textbook for an example of how to use this class and its method to generate a random number.
1. The following figure displays an example of the initial window, before the user has entered a number guess:
2. The following figure displays how the window might look after the user enters a 3, but the application generates a 7:
3. The following figure displays how the window might look if the user’s guess of 7 matches the number generated by the application:
For extra credit: Add error checking to make sure the number entered by the user is between 1 and 10. Display an error message if the number is not within the range, and instruct the user on what to do to play again with a correct number.
Deliverables: as announced in the labs and explained in the handouts given to you in the labs.