Contents Assignment 5
Due: Monday 10/26/2020 11:59pm
Program 2
(a) Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
(b) Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
(c) Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Submission 3
Rubric 3
Example Output 4
1
Program
This program is intended to give you experience building a basic GUI application with Java Swing. You will utilize a few different JComponents and pop up a modal dialog upon a button press. More specifically, your program is meant to simulate a “Magic 8-Ball” program, where the user of your program has the ability to ask a yes/no question, and a random yes/no response is provided. Your program must meet the following requirements:
• the GUI look and feel is up to you
• utilize at least 1 JLabel, 1 JTextField, and 1 JButton
• set the window title, icon, and default close behavior to exit
• pop up a modal dialog with the randomly selected response
• at least 7 different responses (3 affirmative, 3 negative, 1 noncommittal)
• no pop up message if no question is asked
See the Example Output for our implementation of this assignment. Your implementation does not have to look like this; it only needs to support the requested functionality.
(a) Components
Use the Swing examples in this course, or tutorials online, to help you create a GUI that you are happy with and that supports the requested functionality. Start simple and build up. Your main class should extend JFrame so that your program is inherently a Java Swing window. Setup your window by changing the window title, specifying a new window icon, setting the desired size, and specifying the default close behavior to exit the program when closed. Once your window is to your liking, concentrate on the window’s layout manager and adding the required components to it. You can use any layout manager you want (see a Visual Guide to Layout Managers). You can also add any components you want beyond what is required.
Here is an example with the minimum requirements:
Program continued on next page…
Program (continued)
(b) Events
Your program must contain at least 1 JButton for the user to press to submit their question to be answered. When the button is pressed, it will trigger an ActionEvent. You can handle this event inline, but it is cleaner and recommended to make your class listen for this event and handle it in a separate method. One easy way to do this, is to have your class implement the ActionListener interface and override the actionPerformed() method.
Tip
Pressing ENTER from a JTextField also triggers an ActionEvent, just like a JButton. Therefore you can listen for these events from your textfield as well, and perform the same action as the button. This way, users of your program can press the button or press ENTER from the textfield to have their question answered.
(c) Responses
Your program must at least support the following response types:
• strongly affirmative
• affirmative
• slightly affirmative • strongly negative
• negative
• slightly negative • noncommittal
Your program may support more responses if desired.
Submission
You will commit and push your changes to your specific GitHub Classroom repository for this assignment. Please follow the directions in this assignment, make the requested code changes, and commit and push your changes any time before the due date. Keep all source files in the folder called src which is one directory in from the root of your repository (e.g. YourRepo/src). If you need help getting that working with your IDE, please ask. The class with your main() method should be defined inside of a class called Main. Do not commit multiple copies of source files to your repository, and do not move them to another location. All of this is important for grading purposes. Failure to follow these directions will result in a loss of points.
Rubric
Task Percentage
Following submission instructions 10%
General attempt at solving the problem 40%
Program compiles and runs 10%
Set application title, icon and default closing behavior to exit 10%
Use of JLabel, JTextField, JButton 15%
7 distinct responses 10%
Does not pop up response when no question is asked 5%
Total 100%
Rubric
Example Output
Here are some examples of our program running. Your program may look different than this.
Here are all the responses our program generates: