question archive In this assignment, you will again modify your Quiz program from the previous assignment

In this assignment, you will again modify your Quiz program from the previous assignment

Subject:Computer SciencePrice:3.87 Bought7

In this assignment, you will again modify your Quiz program from the previous assignment. You will create a separate class for quiz questions, and you will create objects of that class to ask questions and check answers.
This assignment will include multiple cut-and-paste operations from the existing "Quiz" class into the new "MultipleChoiceQuestion" class. Object-oriented programming is designed to avoid cut and paste, and you will see some of the techniques for re-using existing code in the next assignment. In this assignment, however, you will be converting from procedural programming to object-oriented programming, and cut-and-paste is a simple strategy for this conversion.
First create the new "MultipleChoiceQuestion" class.
• Open your "CS1102" project in Eclipse.
• Select "New" -> "Class" from the File menu.
• Enter the Name "MultipleChoiceQuestion" and click "Finish".
The new file "MultipleChoiceQuestion.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". Having both files in the same package eliminates complications with package imports and access specifiers when the "Quiz" class tries to use the "MultipleChoiceQuestion" class.
Add class variables and instance variables to the "MultipleChoiceQuestion" definition.
• Copy the class variables "nQuestions" and "nCorrect" from the "Quiz" class and paste them into the "MultipleChoiceQuestion" class. static int nQuestions = 0; static int nCorrect = 0;
• Add instance variables for the question and correct answer. Like class variables, these go inside the class definition for "MultipleChoiceQuestion". String question; String correctAnswer;
Now add a constructor for "MultipleChoiceQuestion" objects.
• Name the constructor "MultipleChoiceQuestion". Remember that constructors have no return type. Their name is the return type!
• Give the constructor 7 parameters, all of type String, named the following
o query
o a, b, c, d, and e
o answer
• Thus, the constructor should start as follows. MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) {
(Notice that the parameter names for the constructor are all different from the instance variable names
so that they can be distinguished. In the next unit, you will learn how to distinguish between parameters
and instance variables with the same name.)
Have the constructor initialize the instance variables using its parameters.
• Initialize "question" using the "query" parameter followed by each choice parameter, "a"-"e".
• Remember to add the letter and a newline for each choice. For example:
question = query+"\n";
question += "A. "+a+"\n";
...
• Initialize "correctAnswer" to the parameter "answer". Convert it to upper case, so that the
String "a" will work as an answer, in addition to "A".
Convert "ask" from a class method of "Quiz" to an instance method of "MultipleChoiceQuestion".
• Add the import statement for "JOptionPane" to "MultipleChoiceQuestion.java" since the copied
methods call "JOptionPane" methods.
• Copy the "ask" method from the "Quiz" class and paste it into the "MultipleChoiceQuestion"
class.
• Remove the "static" modifier and the "question" parameter, leaving the following instance
method with no parameters. It does not need parameters because it will use the instance
variables.
String ask() {
• If you named your variables as instructed in the last assignment, the "ask" method should now
use the instance variable "question" without further modification. Otherwise, edit the method
to use "question".
Now convert "check" from a class method of "Quiz" to an instance method of
"MultipleChoiceQuestion".
• Copy the "check" method from the "Quiz" class and paste it into the "MultipleChoiceQuestion"
class.
• Remove the "static" modifier and the two parameters from "check", leaving the following
instance method with no parameters. It does not need parameters because it will use the
instance variables.
void check() {
• Remove the "question" parameter from the "ask" call in "check". Both methods have direct
access to the instance variable.
• If you named your variables as instructed in the last assignment, the "check" method should not
require further modification. Otherwise, edit the method to use the correct names for instance
variables and class variables.
Add a new class method called "showResults" to display the number of questions and correct answers.
• Define the class method "showResults". It needs no parameters and returns no value.
static void showResults() {
• Copy the "JOptionPane" call that displays the results from the main method of "Quiz". Paste it
into the "showResults" method.
• Again, if you named your variables as instructed, this method should not require further
modification. Otherwise, edit the method to use the correct names for the class variables.
Your "MultipleChoiceQuestion" class should now be ready to use. Make sure that no error icons appear
on the left side of the editor pane for "MultipleChoiceQuestion.java". If they do appear, mouse over
them to see what needs to be fixed.
Next convert the "Quiz" class to use the "MultipleChoiceQuestion" class.
First delete the following from "Quiz.java".
• The "import" statement.
• The class variables.
• The class methods "ask" and "check".
Convert the main method to construct and use "MultipleChoiceQuestion" objects.
• Instead of using a String for "question", create "MultipleChoiceQuestion" objects. Here is an
example.
MultipleChoiceQuestion 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");
Notice that the constructor call does not need the choice letters ("A"-"E") or newlines because
the constructor adds those automatically. Also notice that the correct answer can be upper or
lower case because the constructor automatically converts it to upper case.
• Replace calls to "check(question)", where "question" is a String, with calls to "question.check()",
where "question" is a MultipleChoiceQuestion.
• Replace the message dialog that shows the results with a call to the class method
"showResults".
Note that you can either use a new MultipleChoiceQuestion variable for each question or you can use
the same one and just reset its value to reference another object created with "new". Java garbage
collection will clean up the finished objects.

import javax.swing.JOptionPane;

public class Quiz {

    

        static String ask(String question) {

            return JOptionPane.showInputDialog(question);

        }

        static int nQuestions = 0;

        static int nAnswers = 0;

        static void check(String question, String correctAnswer) {

            // we were told to take off while loop but while loop here will help us keep if we give invalid answer,

            // but given incorrect answer it will ask the next question.

            nQuestions += 1;

            while (true) {  

                String answer = (ask(question));

                answer = answer.toUpperCase();

                if(answer.equals(correctAnswer)) {

                JOptionPane.showMessageDialog(null,"Correct!");

                nAnswers += 1;

                return;

                }

                else if(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D")  || answer.equals("E")) {

                JOptionPane.showMessageDialog(null,"Inorrect. Please try again");

                return;

                }

                else{

                JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");

                }

            }

        }

            public static void main(String args[]) {

                String a = "What are the variables required to calculate heat input?\n";

                a += "A. Ampere\n";

                a += "B. Voltage\n";

                a += "C. Travel speed\n";

                a += "D. Understanding if it's in KJ/in. or J/in.\n";

                a += "E. All of the above\n";

                String b = "E";

                check(a, b);

                String c = "When was google formed?\n";

                c += "A. 1998\n";

                c += "B. 1997\n";

                c += "C. 1996\n";

                c += "D. 1990\n";

                c += "E. 2001\n";

                String d = "A";

                check(c, d);

                String e = "Which is the most valuable company in world in february 2020?\n";

                e += "A. Apple\n";

                e += "B. Tesla\n";

                e += "C. Microsoft\n";

                e += "D. Amazon\n";

                e += "E. Visa\n";

                String f = "D";

                check(e, f);

                JOptionPane.showMessageDialog(null,"Number of questions asked "+ nQuestions+ ". The number of correct answers given are "+ nAnswers);

Option 1

Low Cost Option
Download this past answer in few clicks

3.87 USD

PURCHASE SOLUTION

Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

rated 5 stars

Purchased 7 times

Completion Status 100%

Related Questions