So my assignment is to create a Quiz main driver class, and an abstract question class. The purpose is to create a question class so that a user may come up with a type of question and incorporate it in the main driver class. We already supply some of our own question class types that "extend" question such as true/false and multiple choice to name a few. The driver has to take in a text file that is filled with questions like the format below. The first letters tells what kind of question it will be. The next line is a string of the question, and the next thing is the answer.
N
How many parameters does a copy constructor accept?
1
S
What standard Java class do all classes inherit from?
Object
T
To compare two char values, use the equals() method.
false
M
Which types are good for storing numbers with decimal points?
- String
+ double
- boolean
+ float
- int
m
What is "computer".substring(3, 5)?
- mpute
+ pu
- pute
The single characters identify what type of question it is going to be. For example the letter T stands for a true/false question. The massive roadblock i cant get by is how to incorporate the one letter identifier if the user can only A.) use the driver program and B.) extend question to make their own. My code for the question class is below. I cannot use if-else statements, or even mention question classes I am building myself in this class. It has to be completely separate and cannot be edited, yet somehow any user should be able to come along and say they want to create an "essay question" class and add its own identifier and put it in a text file and have the driver run to read in the question and create that question object. Each question has it's own method isCorrect() to identify when the user answers a question, if it is right or not. I also added a sample question type to kind of show how a user would incorporate their own.
To my main point, does anyone have an idea on how I can incorporate identifiers to create the certain type of question that is needed?
Thanks
David
package Questions; public abstract class Question { private String question; private Object answer; public String getQuestion() { return question; } public Object getAnswer() { return answer; } public void setQuestion(String s) { this.question = s; } public abstract boolean isCorrect(); }
package Questions; public class TrueFalse extends Question { boolean answer; public TrueFalse(String question,Boolean answer) { this.setQuestion(question); this.answer = answer; } public boolean isCorrect() { return (); } }