Unit 7 Programming Assignment PDF
Unit 7 Programming Assignment PDF
In this assignment, you will again modify your Quiz program from the previous assignment. You
will replace the text based dialog boxes with button-based dialog boxes specialized for multiple-
choice and true-false questions.
This assignment uses many classes and methods described in Chapter 6 of Eck (2019), and the
instructions below include references to the relevant sections of Chapter 6.
This class will extend the Java component "JDialog" to create a dialog window. ("JDialog" is
similar to "JFrame".) Your class will also implement the interface "ActionListener" to respond to
button clicks. (See Section 6.1.3.)
• Add import statements for "JDialog" and "ActionListener". Use "*" to simplify the list.
import java.awt.event.*;
import javax.swing.*;
• Exend "JDialog" and promise to implement "ActionListener".
public class QuestionDialog extends JDialog implements
ActionListener {
The dialog box will store the user's answer based on a button click.
When a button is clicked in a "QuestionDialog", "answer" will get set to its label. This is all the
dialog box needs to do, so it can then return control to the main program.
• Inside the "actionPerformed" method, after setting "answer", return control by closing the
dialog. This is done with the "dispose()" method that "QuestionDialog" inherits from a
parent class.
dispose();
The editor pane for the class should show no error warnings. (The editor may show an
informational warning about a missing ID for serialization, but you may ignore this since you
will not be using the serialization features. Serialization is the ability to read and write object
data from and to files.)
Next modify "TrueFalseQuestion" to use "QuestionDialog". Start with the "ask" method, which
becomes much simpler. It only needs to do two things: make the question dialog visible and then
get the answer from it.
Next comes the more-complicated task of building up the "QuestionDialog" box for a true/false
question. You do this in the constructor for "TrueFalseQuestion".
The next step is to add buttons to the "buttons" panel. You use a separate panel for the buttons so
they appear in a single row (using the default layout, "FlowLayout", as described in Section
6.6.1).
Because you will add more than one button, encapsulate the steps in an "addButton" method.
• Add an "addButton" method to the "TrueFalseQuestion" class. The method takes the
buttons panel and a button label as parameters.
void addButton(JPanel buttons, String label) {
• In the "addButton" method, create a new button with the provided label.
JButton button = new JButton(label);
• The "question" object is programmed to respond to button clicks (by implementing
"actionPerformed" from the "ActionListener" interface). Wire it up to the new button by
adding it as a listener. (See Section 6.1.3.)
button.addActionListener(question);
• Finally, add the button to the "buttons" panel. (See Section 6.1.2.)
buttons.add(button);
Use this "addButton" method to add true/false buttons to the dialog box.
The construction of the dialog box requires a few additional steps in the "TrueFalseQuestion"
constructor for proper behavior, size, and location.
• Make the dialog box "modal", as described in Section 6.7.2. This will keep the dialog box
from returning control to the program until it is disposed. (Recall that this disposal
happens after a button is clicked, thanks to your implementation of "actionPerformed" in
"QuestionDialog".)
this.question.setModal(true);
• Use the "pack" method to resize the dialog box based on its contents (Section 6.7.3).
this.question.pack();
• Center the dialog box on the screen using the "setLocationRelativeTo" method with a
"null" argument. This method is not described in Eck (2014), but it appears in standard
Java documentation.
this.question.setLocationRelativeTo(null);
That completes the construction of the dialog box "question" in the "TrueFalseQuestion"
constructor. Note that the constructor should still include the initialization of the "answer" String
from the previous assignment.
The editor pane for "TrueFalseQuestion.java" should have no errors. If you open
"MultipleChoiceQuestion.java", however, you should see errors from the initialization of
"question", which is now a "QuestionDialog" instead of a String.
If you properly commented out all but one true/false question in your main program, you should
now be able to run. The program should display a dialog box with your true/false question, along
with "TRUE" and "FALSE" buttons. Clicking one of the buttons should close the window and
replace it with the appropriate correct/incorrect dialog.
Next comes building the dialog box for "MultipleChoiceQuestion". The first step is to re-use
what you can from the "TrueFalseQuestion" definition. You can do this by moving parts of the
definition to the parent class, "Question".
For example, the "ask" method in "TrueFalseQuestion" is now also appropriate for
"MultipleChoiceQuestion" also.
• In "Question.java", replace the existing import statement for "JOptionPane" with the two
imports in "TrueFalseQuestion.java".
import java.awt.*;
import javax.swing.*;
• Replace the abstract "ask" declaration in "Question" with the concrete "ask" definition
from "TrueFalseQuestion".
• Delete the "ask" definition from "TrueFalseQuestion".
You should now have no error warnings, and your program should work as before. Give it a try.
The "TrueFalseQuestion" constructor has more statements to share, but they come after some
statements that are specific to "TrueFalseQuestion". Encapsulate the sharable statements in a
"Question" method.
Again, you should have no error warnings, and your program should work as before. Test once
more.
Now you are ready to create the dialog box for "MultipleChoiceQuestion".
Just as you gave "TrueFalseQuestion" the method "addButton", you will give
"MultipleChoiceQuestion" a method to create its answer choices. Each choice will need both a
button and a text label, however, and the choices should be arranged in a column instead of a
row.
The final step is to update the "MultipleChoiceQuestion" constructor to build the dialog box.
• Un-comment all the questions in your main program in "Quiz". The file "Quiz.java"
should now be unchanged from the previous assignment.
You should not have any error warnings, and you should be able to run your program to ask all
your previous questions, but with your new dialog boxes!
Your assignment will be graded by your peers using the following criteria. Each item is worth 10
points, for a total of 90 points.
• Does the submission include a file "QuestionDialog.java" with a class that extends
"JDialog" and implements "ActionListener"?
• Does the class "QuestionDialog" have a method "actionPerformed" that uses its
"ActionEvent" parameter to set the instance variable "answer" and then calls the inherited
"dispose" method?
• Does the submission include a file "Question.java" with a constructor that initializes
instance variable "question", gives it a single-column grid layout, and adds a text label
using the constructor's String parameter?
• Does class "Question" have a method "ask" that makes the instance variable "question"
visible and returns the value "question.answer"?
• Does class "Question" have a method "initQuestionDialog" that makes instance variable
"question" modal, sets its size with "pack", and positions it in the center of the screen?
• Does the class "TrueFalseQuestion" have a method "addButton" that constructs a button
using its String parameter, adds the instance variable "question" as a listener for that
button, and adds the button to its "JPanel" parameter?
• Does the class "TrueFalseQuestion" have a constructor that calls its superclass
constructor with its first String parameter, calls "addButton" to add "TRUE" and
"FALSE" buttons to a panel, adds that panel to the instance variable "question", calls
"initQuestionDialog", and initializes the instance variable "correctAnswer" with its
second String parameter?
• Does class "MultipleChoiceQuestion" have a method "addChoice" that creates a panel
with a border layout, creates a button using its first String parameter, adds the instance
variable "question" as a listener for that button, adds the button to the left side of the
panel, adds a label to the center of the panel using its second String parameter, and adds
the panel to the instance variable "question"?
• Does the class "MultpleChoiceQuestion" have a constructor that calls its superclass
constructor with its first parameter, calls "addChoice" using its next five parameters, calls
"initQuestionDialog", and initializes the instance variable "correctAnswer" with its last
parameter?