Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Quiz { public static void main(String[] args) { // we create object question that will take question, choices and answer as argument MultipleChoiceQuestion

public class Quiz {   public static void main(String[] args) {     //we create object question that will take question, choices and answer as argument     MultipleChoiceQuestion question = new MultipleChoiceQuestion("Which planet is the hottest?",         "Venus",         "Saturn",         "Mercury",         "Mars",         "Earth",         "A");     //everytime we instantiate the object we call on check method     question.check();         question = new MultipleChoiceQuestion("In Pirates of the Caribbean, what was the name of Captain Jack Sparrow's ship",         "The Marauder",         "The Black Pearl",         "The Black Python",         "The Slytherin",         "Dark Knight",         "B");     question.check();         question = new MultipleChoiceQuestion("According to Forrest Gump, "life was like...",         "A bag of lemons",         "A handful of roses",         "A lollipop",         "A box of chocolates",         "A tub of rice",         "D");     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("In The Wizard of Oz, the Tin Man wanted to see the Wizard about getting...",         "A brain",         "An oil can",         "A dog",         "A heart",         "A hand",         "A");     question.check();     //lastly we show the results     question.showResults();   } } 

import javax.swing.JOptionPane; public class MultipleChoiceQuestion {   //our data members   static int nQuestions = 0;   static int nCorrect = 0;   String question;   String correctAnswer;   //our constructor. It takes the question, choices and answer as parameters   public MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) {     this.question = query;     this.question += " A. " + a;     this.question += " B. " + b;     this.question += " C. " + c;     this.question += " D. " + d;     this.question += " E. " + e;     this.correctAnswer = answer.toUpperCase();   }   //ask method   public String ask() {     //ask the question     String answer = JOptionPane.showInputDialog(question);     answer = answer.toUpperCase();     //we check if answer is valid     //if not we take input again until we get a, b, c, d, or e     while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D")         && !answer.equals("E")) {       JOptionPane.showMessageDialog(null, "invalid answer please enter A,B,C,D or E");       answer = JOptionPane.showInputDialog(question);       answer = answer.toUpperCase();     }     return answer;   }   //check method   //we increment the number of questions if called   public void check() {     nQuestions++;     //we call on ask method to get the answer of user     String answer = ask();     //we match answer of user to correctAnswer     //we add to nCorrect if answer is equal to correctAnswer     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.");   } } 

The new file "Question.java" should appear in the editor pane. Make sure it is also listed under "(default package)" in the "Package Explorer" pane, along with "Quiz.java" and "MultipleChoiceQuestion.java".

If you forgot to check the "abstract" box, add the modifier "abstract" to the "Question" class.public abstract class Question {Add variables and methods to the "Question" class that will be inherited by the subclasses. Copy the following from the "MultipleChoiceQuestion" class and paste them into the "Question" class.The class variables "nQuestions" and "nCorrect".

The instance variables "question" and "correctAnswer".The instance method "check".The class method "showResults".Do not copy the constructor for "MultipleChoiceQuestion" or the instance method "ask".The editor pane for "Question" should now show one error at the statement in "check" that calls the "ask" method.

Add an abstract declaration for the "ask" method. This should be inside the "Question" class but outside all the method definitions.abstract String ask();Note that this is a method declaration only, with no "{...}", because the method is abstract. It must be defined (implemented) in any concrete (non-abstract) subclasses of "Question".The error warning in the editor pane should disappear. The "ask" call in "check" will use the methods defined in the subclasses of "Question".Now modify the "MultipleChoiceQuestion" class to be a subclass of "Question".

Delete from "MultipleChoiceQuestion" all the variables and methods that you pasted into "Question": "nQuestions", "nCorrect", "question", "correctAnswer", "check", and "showResults".

Do not delete the constructor or the "ask" method.The editor pane should show many errors, particularly in the constructor.

Make "MultipleChoiceQuestion" a subclass of "Question" using the "extends" keyword.public class MultipleChoiceQuestion extends Question {All the error warnings should disappear.Convert "Quiz" to use "Question" variables with "MultipleChoiceQuestion" objects.

Change the type of any "MultipleChoiceQuestion" variables in the main method of "Quiz" to "Question".

But do not change the constructor calls used to initialize these variables. They should still be "MultipleChoiceQuestion".Your program should still work using "Question" variables to reference "MultipleChoiceQuestion" objects. Test it to make sure.Next add a new subclass of "Question" for true/false questions.

Select "New" -> "Class" from the File menu.

Enter the Name "TrueFalseQuestion".

Enter the Superclass "Question".

Click "Finish".The new file "TrueFalseQuestion.java" should appear in the editor pane.

If you did not add the Superclass name, you will need to add "extends Question" to the class declaration.public class TrueFalseQuestion extends Question {The IDE may have already added an empty "ask" method because of the abstract method it found in "Question". It may have also added the annotation "@Override", which is optional and instructs the compiler to make sure the method actually does override a method in a parent class.Add an implementation of the "ask" method that is specific to true/false questions.

Import the "JOptionPane" package

.Like in "ask" for "MultipleChoiceQuestion", have the new "ask" pose the "question" String repeatedly until the user provides a valid answer.In this case, valid answers are: "f", "false", "False", "FALSE", "n", "no", "No", "NO", "t", "true", "T", "True", "TRUE", "y", "yes", "Y", "Yes", "YES".When users provide an invalid answer, display the message, "Invalid answer. Please enter TRUE or FALSE."Convert any valid answer representing true or yes to "TRUE" and any valid answer representing false or no to "FALSE".

Hint: Convert all answers to upper case before checking their validity.Add a "TrueFalseQuestion" constructor to initialize the "question" and "correctAnswer" Strings inherited from "Question".

Give the constructor two String parameters, "question" and "answer".

Use "this" to set the instance variable "question" using the parameter "question". Add the text, "TRUE or FALSE: " to the beginning of the question.this.question = "TRUE or FALSE: "+question;(You could also use "super.question", since the instance variable is in "Question". Either works because "TrueFalseQuestion" does not hide the instance variable in "Question" with its own instance variable named "question".)

Set the instance variable "correctAnswer" to only "TRUE" or "FALSE" based on "answer". Allow the parameter "answer" to be any String that is considered valid by "ask".Now add a true/false question to your quiz!

Initialize a "Question" variable using a "TrueFalseQuestion" constructor.Question question = new TrueFalseQuestion(...);Run your program and test that the following are true.It accepts only valid answers.

It responds appropriately to correct and incorrect answers.

It provides accurate counts of correct answers and total questions.Finally, add at least four more true/false questions, for a total of at least five multiple-choice questions and five true/false questions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions