question archive Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments
Subject:Computer SciencePrice:2.87 Bought7
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners

Answer:
abstract class Question {
String question;
String correctAnswer;
static int nQuestions = 0;
static int nCorrect = 0;
void check() {
nQuestions++;
String answer = ask();
if (answer.equals(correctAnswer)) {
JOptionPane.showMessageDialog(null,"Correct!");
nCorrect++;
} else {
JOptionPane.showMessageDialog(null,"Incorrect. The correct answer is "+correctAnswer+".");
}
}
static void showResults() {
JOptionPane.showMessageDialog(null,nCorrect+" correct out of "+nQuestions+" questions.");
}
abstract String ask();
}
class MultipleChoiceQuestion extends Question {
MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) {
question = query+"\n";
question += "A. "+a+"\n";
question += "B. "+b+"\n";
question += "C. "+c+"\n";
question += "D. "+d+"\n";
question += "E. "+e+"\n";
correctAnswer = answer.toUpperCase();
}
String ask() {
while (true) {
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
boolean valid = (answer.equals("A") || answer.equals("B") ||
answer.equals("C") || answer.equals("D") || answer.equals("E"));
if (valid) return answer;
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
}
}
}
class TrueFalseQuestion extends Question {
String ask() {
while (true) {
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();
boolean valid = (answer.equals("TRUE") || answer.equals("FALSE") ||
answer.equals("F") || answer.equals("N") || answer.equals("NO") || answer.equals("T") || answer.equals("Y") || answer.equals("YES"));
if (valid) {
if(answer.equals("F") || answer.equals("N") || answer.equals("NO")) {
answer = "FALSE";
}
if(answer.equals("T") || answer.equals("Y") || answer.equals("YES")) {
answer = "TRUE";
}
return answer;
}
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter TRUE or FALSE.");
}
}
TrueFalseQuestion(String question, String answer) {
this.question = "TRUE or FALSE: "+question;
correctAnswer = answer.toUpperCase();
}
}
public class Quiz {
public static void main(String[] args) {
Question question = new TrueFalseQuestion("Quizzes are fun!","y");
question.check();
question = new TrueFalseQuestion("Quizzes are more fun than programming!","n" );
question.check();
question = new TrueFalseQuestion("Which one starts with T?","t");
question.check();
question = new TrueFalseQuestion("Which one starts with F?","f");
question.check();
question = new TrueFalseQuestion("Which one is not true?","false");
question.check();
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");
question. check();
question = new MultipleChoiceQuestion("When is a quiz?", "long, long ago", "right now", "the best of times", "the worst of times", "nevermore", "b");
question.check();
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");
question.check();
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");
question.check();
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");
question.check(); MultipleChoiceQuestion.showResults();
}
}
Step-by-step explanation
class variables:nQuestions, nCorrect : These are static class variables and are shared across objects. nQuestions represents number of questions asked, whereas nCorrect represents no of questions answered correctly.
instance variables: question, correctAnswer: Stores the questions and correct answer for each object
inheritance: MultipleChoiceQuestion and TrueFalseQuestion class inherit the Question class and its attributes like nQuestions, nCorrect and member functions
polymorphism: We have a run time polymorphism when we create an object with base/super class Question and initialise it with the sub classes. Whenever a member function is called through the object of the base class, run time polymorphism helps in called the correct method of sub class if it is inherited.
abstract classes: Quiz is an abstract class. The method implementation of Quiz class is done by its subclasses
"this": This represents the current object of the class.
"super": super refers to the parent/super class which is being extended by the subclasses. For e.g Quiz is a super class. The member function and variables of this class can be called using super keyword
interfaces: Interfaces do not provide the implication and it is just the method definition. The class which implements the interface are responsible for defining the business logic/body of the interface.
Event listener: The Event listener represent the interfaces responsible to handle events. . Every method of an event listener method has a single argument as an object which is subclass of EventObject class. Changing the state of an object is known as an event. For example, click on button, dragging mouse etc.

