Unit 7 Programming Assignment Solution
The quiz questions, answers, and choices for correct answer should be unique for each
submission. Here is an example.
Files "[Link]":
import [Link].*;
import [Link].*;
public class QuestionDialog extends JDialog implements ActionListener {
String answer;
public void actionPerformed(ActionEvent e) {
answer = [Link]();
dispose();
}
}
File "[Link]":
import [Link].*;
import [Link].*;
public abstract class Question {
static int nQuestions = 0;
static int nCorrect = 0;
QuestionDialog question;
String correctAnswer;
Question(String question) {
[Link] = new QuestionDialog();
[Link](new GridLayout(0,1));
[Link](new JLabel(" "+question+" ",[Link]));
}
String ask() {
[Link](true);
return [Link];
}
void check() {
nQuestions++;
String answer = ask();
if ([Link](correctAnswer)) {
[Link](null,"Correct!");
nCorrect++;
} else {
[Link](null,"Incorrect. The correct
answer is "+correctAnswer+".");
}
}
void initQuestionDialog() {
[Link](true);
[Link]();
[Link](null);
}
static void showResults() {
[Link](null,nCorrect+" correct out of
"+nQuestions+" questions");
}
}
File "[Link]".
import [Link].*;
public class TrueFalseQuestion extends Question {
TrueFalseQuestion(String question, String answer) {
super(question);
JPanel buttons = new JPanel();
addButton(buttons,"TRUE");
addButton(buttons,"FALSE");
[Link](buttons);
initQuestionDialog();
answer = [Link]();
if ([Link]("T") || [Link]("TRUE") || [Link]("Y") ||
[Link]("YES")) correctAnswer = "TRUE";
if ([Link]("F") || [Link]("FALSE") || [Link]("N") ||
[Link]("NO")) correctAnswer = "FALSE";
}
void addButton(JPanel buttons, String label) {
JButton button = new JButton(label);
[Link](question);
[Link](button);
}
}
File "[Link]"
import [Link].*;
import [Link].*;
public class MultipleChoiceQuestion extends Question {
MultipleChoiceQuestion(String query, String a, String b, String c, String d, String
e, String answer) {
super(query);
addChoice("A",a);
addChoice("B",b);
addChoice("C",c);
addChoice("D",d);
addChoice("E",e);
initQuestionDialog();
correctAnswer = [Link]();
}
void addChoice(String name, String label) {
JPanel choice = new JPanel(new BorderLayout());
JButton button = new JButton(name);
[Link](question);
[Link](button,[Link]);
[Link](new JLabel(label+" ",[Link]),[Link]);
[Link](choice);
}
}
File "[Link]". (This file should not have changed since the previous assignment.)
public class Quiz {
public static void main(String[] args) {
Question question = new TrueFalseQuestion("Quizzes are fun!","y");
[Link]();
question = new TrueFalseQuestion("Quizzes are more fun than
programming!","n");
[Link]();
question = new TrueFalseQuestion("Which one starts with T?","t");
[Link]();
question = new TrueFalseQuestion("Which one starts with F?","f");
[Link]();
question = new TrueFalseQuestion("Which one is not true?","false");
[Link]();
question = new MultipleChoiceQuestion(
"What is a quiz?",
"a test of knowledge, especially a brief informal test given to
students",
"42",
"a duck",
"to get to the other side",
"To be or not to be, that is the question.",
"a");
[Link]();
question = new MultipleChoiceQuestion("When is a quiz?",
"long, long ago",
"right now",
"the best of times",
"the worst of times",
"nevermore",
"b");
[Link]();
question = new MultipleChoiceQuestion("Where is a quiz?",
"a galaxy far, far away",
"under the sea",
"right here",
"there and back again",
"the other side of the mountain",
"c");
[Link]();
question = new MultipleChoiceQuestion("Why is a quiz?",
"I think, therefore it is.",
"Who is to say?",
"You tell me.",
"Because learning is fun!",
"Because I said so.",
"d");
[Link]();
question = new MultipleChoiceQuestion("How is a quiz?",
"fair to middling",
"not bad",
"by a stroke of luck",
"by accident",
"Using Java object-oriented programming!",
"e");
[Link]();
[Link]();
}
}
Screen shot of a true/false question.
Screen shot of a multiple-choice question.