Create a Quiz class that contains an array that should be able to hold either Qu
ID: 3598864 • Letter: C
Question
Create a Quiz class that contains an array that should be able to hold either Question or MultipleChoiceQuestion objects.
Create a constructor for the Quiz class that receives the total number of questions in the quiz.
Create an addQuestion method that should be able to receive either a Question or MultipleChoiceQuestion object and it will add it to the array of questions in the quiz.
Override the toString method in both Question and MultipleChoiceQuestion so that it will return a String containing the question text (and possible answers in the case of MultipleChoiceQuestion).
Override the toString method in Quiz so that it returns a String containing the text from all questions in the quiz.
Create a Driver class that instantiates at least 1 Question and 1 MultipleChoiceQuestion object. Create a Quiz class and add both to the quiz. Display the result of the Quiz object’s toString method on the screen.
Explanation / Answer
import java.util.Scanner;
public class QuizDriver {
public static void main(String[] args){
String[] possibleAnswers = {"two", "four", "six", "eight"};
MultipleChoiceQuestion q1 = new MultipleChoiceQuestion("How many legs does an octopus have?", "4", possibleAnswers.clone());
Question q2= new Question("What is the size of int in Java","4 bytes");
Quiz quiz = new Quiz(2);
quiz.addQuestion(q1);
quiz.addQuestion(q2);
System.out.println(quiz);
}
}
class Question
{
String question;
String answer;
String[] possibleAnswers;
Question(String q, String a)
{
question=q;
answer=a;
}
public String askQuestion(){
return question;
}
public String getAnswer(){
return answer;
}
public boolean check(String a)
{
if(a.equals(answer))
return true;
else return false;
}
public String toString()
{
return question;
}
}
class MultipleChoiceQuestion extends Question{
private String[] possibleAnswers;
public MultipleChoiceQuestion(String question, String answer, String[] possibleAnswers){
super(question, answer);
this.possibleAnswers = possibleAnswers;
}
@Override
public String askQuestion(){
String result = super.askQuestion() + " ";
for(int i=0; i<possibleAnswers.length; i++){
result += ((i+1) + ". " + possibleAnswers[i] + " ");
}
return result;
}
public void setPossibleAnswers(String[] possibleAnswers){
this.possibleAnswers = possibleAnswers;
}
public String[] getPossibleAnswers(){
return possibleAnswers;
}
@Override
public MultipleChoiceQuestion clone(){
MultipleChoiceQuestion temp = new MultipleChoiceQuestion(super.askQuestion(), getAnswer(), possibleAnswers.clone());
return temp;
}
public String toString()
{
String result = question + " ";
for(int i=0; i<possibleAnswers.length; i++){
result += ((i+1) + ". " + possibleAnswers[i] + " ");
}
return result;
}
}
class Quiz
{
int n,size;
Question questions[];
Quiz(int n)
{
size=0;
questions=new Question[n];
}
public void addQuestion(Question q)
{
questions[size++]=q;
}
public String toString()
{
String s="Quiz Questions: ";
for(int i=0; i<size;i++)
{
s+="Question "+(i+1)+". "+questions[i]+" ";
}
return s;
}
}