CS 1401 Assignment #9

Date Assigned: Monday, April 3, 2006

Due Date: Correspondingly Monday, April 10, 2006, or Tuesday, April 11, 2006, 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 GUI-based application for entertaining the user. The application should contain five main areas:

        Application name. The name should be “Entertainment Central”

        List of entertainment choices. The list should contain: All, Fortune, Joke, News, and Trivia. Obtain five one-liners for each of these categories from web sites. End each item with “(Source: www.sitename)”.

        Choice entry field. The user will type in his or her entertainment choice in this field.

        “Show / Next” button. The user will click on this button to request an entertainment item from the category entered in the choice entry field.

        Output area. Based on user input and a random number, this area will be used to display a one-liner of the appropriate type. 

1.      Design a GUI layout according to your preference. Something simple would have the following layout:


Type in choice:



Show / Next

You will have fun today.

(Source: www.fortunecookie.com)

Note that this sample layout may be split when printed. If this happens, ignore the last line of the first part and the first line of the second part. 

2.      Add the following functionality to the “Show / Next” button. When the user clicks on the button, the application will generate a random number equal to 1, 2, 3, 4, or 5 to select a one-liner from the typed-in entertainment choice. If the choice is “All”, do this process twice and use the first pass to automatically select a choice (1 = Fortune, 2 = Gossip, and so on). The corresponding one-liner should be displayed in the GUI output area.

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 an entertainment choice followed by a button click.  In response to the entered choice, the application generates an entertainment one-liner. This interaction repeats as long as the user clicks on the “Show / Next” button.

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:

        Use JLabel  class to display title, choices, and output. This information is all application output and there is no reason to use any GUI component that allows the user to modify the information.

        Use JTextField class for choice entry field.  

        Use JButton class for “Show / Next” button. The string displayed on this button should be “Show / Next”.      

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 clicks.

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.


For extra credit: Make input choices case-insensitive and allow using just the first letter of a choice to speed up interaction. Add error checking to make sure that the entertainment choice is valid. Display an error message if the choice is not valid, and instruct the user on what to type for a correct selection.

Deliverables: as announced in the labs and explained in the handouts given to you in the labs.