Questions
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.face
1. class variables
Definition: Class variables are also called static variables. They are modified by the static modifier, belong to the class rather than objects, and can be called directly by the class name.
Application: In the Question.java file, I define two class variables: nQuestions and nCorrect, modified by the modifier static and used to count the total questions and the correct questions, respectively.
2. instance variables
Definition: Instance variables are non-static variables. They have no static modifier, belong to the object rather than class.
Application: In the Question.java file, I define two instance variables: question and correctAnswer. The difference between instance and class variables is that instance variables do not have the modifier static and belong to the object instead of the class.
3. inheritance
Definition: Inheritance means a subclass extends one superclass. The subclass will have all the methods and variables in the superclass. Besides, one subclass can only extend one superclass.
Application: The TrueFalseQuestion class and MultipleChoiceQuestion class both extend the Question class in the Quiz program. Hence, they will have all the methods and instance variables in the superclass Question. For example, the subclass TrueFalseQuestion can use the correctAnswer variable and initQuestionDialog() method, which inherit from the superclass Question.
4. polymorphism
Definition: Polymorphism means the method called at run-time depends on the actual (run-time) type instead of the declared (compile-time) type ((Seiter & Palmer, 2020). For example, the class Student extends the class People and overrides the printInfo() method. People s = new Students(); s. printInfo() will call the Student’s printInfo() method instead of the People’s. Besides, we should note that variables are not polymorphic. For instance, s.age will call the People’s variable age instead of the Student’s.
Application: The QuestionDialog class implements the ActionListener interface and overrides its actionPerformed() method. Hence, when a button is clicked, the QuestionDialog’s actionPerformed() method will be called instead of the ActionListener’s. This is polymorphism.
5. abstract classes
Definition: The abstract class is a class with the modifier abstract, it may have or not have abstract methods, and it can also include non-abstract methods. We can use a subclass to extend the abstract class but note that we cannot create objects using an abstract class directly.
Application: The class Question is an abstract class, which has a modifier abstract. And I withdraw some common features from its subclass MultipleChoiceQuestion and TrueFalseQuestion, including some variables (like correctAnswer) and some methods (like initQuestionDialog()), so the codes will be more concise.
6. “this”
Definition: The keyword this is a reference to the current object. We can use it to call other constructors (this() or this(arguments)) and distinguish an instance variable and a local parameter (this.variableName) in the same class (Seiter & Palmer, 2020).
Application: In the Question class’s constructor, I use the statement this.question = new QuestionDialog() to initialize the QuestionDialog type variable question. “this” is a reference to the current object.
7. “super”
Definition: The keyword super is a reference to the superclass object. We can use it to call the superclass constructors (super() or super(arguments)) and superclass instance methods(super.methodName) (Seiter & Palmer, 2020).
Application: In the TrueFalseQuestion class’s constructor, I use super(question) to call its superclass Question’s constructor. In the MultipleChoiceQuestion class’s constructor, I use super(query) to call its superclass Question’s constructor.
8. interfaces
Definition: An interface contains methods declaration without their implementation. To declare an interface, we need to use the keyword interface; to implement an interface, we need to use the keyword implements and override all the methods in the subclass.
Application: I use the QuestionDialog class to implement the ActionListener interface. Hence, I can create a listener to listen to the button click events, and handle it.
9. event listeners
Definition: “An event listener is an instance of a class that implements a listener
interface” ( Horstmann, 2019, p. 800).
Application: In the Question class’s construtor Question(String question), I use the statement this.question = new QuestionDialog() to initialize the variable question. The new QuestionDialog() will create an event listener object because the QuestionDialog class implements the ActionListener interface.
Word count: 679
Appendix
The Quiz program with button-based dialog boxes specialized for multiplechoice and true-false questions:
1 | public class Quiz { |
1 | import java.awt.event.*; |
1 | import java.awt.*; |
1 | import javax.swing.JOptionPane; |
1 | import java.awt.*; |
The Quiz program with text-based dialog boxes specialized for multiplechoice and true-false questions:
1 | public class Quiz { |
1 | import javax.swing.JOptionPane; |
1 | import javax.swing.JOptionPane; |
1 | import javax.swing.JOptionPane; |
References
1.Seiter, L. & Palmer, D. (2020). Introduction to Programming with Java (8th ed.).
https://runestone.academy/runestone/books/published/csjava/index.html
2.Horstmann, C. S. (2019). Core Java Volume I–Fundamentals (11th ed.). Pearson.