CS 1401 Assignment #9

Date Assigned: Tuesday, November 1, 2005 or Wednesday, November 2, 2005.

Due Date: correspondingly, Tuesday, November 8, 2005, or Wednesday, November 9, 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 horoscope telling application with a GUI window. The application invites a user to type in a horoscope sign into a field in the window, and then to press a button displayed in the window to ask for the horoscope for that sign and a lucky number. In response to the button clicking event, the application selects a horoscope message and generates a random number for the lucky number. To keep it simple, horoscope messages are initialized strings in the application. The user may reset the display by selecting another button in the window that clears the user input field and the response message.

Follow the steps below.

1. All the code for this application may be in the same class. Display a window with the following components:

2. Within the application class, define a static class ButtonHandler implementing the ActionListener interface with an actionPerformed method to handle button events. One way to handle two buttons is to use the same button handler to respond to both button events. To figure out which button generated the event, use the getActionCommand() method. This returns the string label from the button. Another way to handle two buttons is to define two different button handlers. You can use either way. The case study for the calculator (code starting on page 417 in our textbook) uses 3 buttons, where one button handler deals with two buttons and another handler deals with the third button.

For extra credit: Do one or both of the following.

1. In the description above, we generate a new random number for the lucky number each time the user asks for a horoscope. This means the lucky number will keep changing. Modify your program so the lucky numbers for a sign will change daily instead of for each request. To do this, use the random number generator with a seed. There is a version of the random() method that takes a long as a seed. The random number generated will depend on the seed. Compute a seed that depends on the current year, month, day and sign. Use a GregorianCalendar object to obtain the current date.

2. Instead of using initialized strings for the horoscope messages, read the strings from an input file.

Deliverables: as announced in the labs.