Answer to Question #275945 in Java | JSP | JSF for oboti

Question #275945

Unit 4 Programming Assignment 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.


1
Expert's answer
2021-12-06T02:16:44-0500
public class MultipleChoiceQuestion {
    static int nQuestions = 0;
    static int nCorrect = 0;
    String question;
    String correctAnswer;
    MultipleChoiceQuestion(String query, String a, String b, Stringc,String d,
                           String e, String answer) {
        question = query+"n";
        question += "A. "+a+"n";
        question += "B. "+b+"n";
        question += "C. "+c+"n";
        question += "D. "+d+"n";
        question += "E. "+e+"n";
        correctAnswer = answer;
        correctAnswer= correctAnswer.toUpperCase();
    }
    String ask() {
        while (true) {
            String answer = JOptionPane.showInputDialog(question);
            answer = answer.toUpperCase();
            boolean valid = (answer.equals("A") || answer.equals("B") ||
                    answer.equals("C") || answer.equals("D") ||answer.equals("E"));
            if (valid) return answer;
            JOptionPane.showMessageDialog(null,"Invalid answer. Pleaseenter
                    A, B, C, D, or E.");
        }
    }
    void check() {
        nQuestions++;
        String answer = ask();
        if (answer.equals(correctAnswer)) {
            JOptionPane.showMessageDialog(null,"Correct!");
            nCorrect++;
        } else {
            JOptionPane.showMessageDialog(null,"Incorrect. The correctanswer
                    is "+correctAnswer+".");
        }
    }
    void showResults() {
        JOptionPane.showMessageDialog(null,nCorrect+" correct out
                of"+nQuestions+" questions");
    }
}
/*
 * To change this license header, choose License Headers in ProjectProperties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
public class Quiz {
    public static void main(String[] args) {
        MultipleChoiceQuestion question = new
                MultipleChoiceQuestion("What is a quiz?",
                "a test of knowledge, especially a brief informal test givento
                students",
                "42",
                "a duck",
                "to get to the other side",
                "To be or not to be, that is the question.",
                "a");
        question.check();
        question.showResults();
        MultipleChoiceQuestion question1 = new
                MultipleChoiceQuestion("When is a quiz?",
                "a long, long ago",
                "right now",
                "the best of times",
                "the worst of times",
                "nevermore","b");
        question1.check();
        question1.showResults();
        MultipleChoiceQuestion question2 = new
                MultipleChoiceQuestion("Where is a quiz?",
                "a galaxy far, far away",
                "under the sea",
                "right here",
                "there and back again",
                "the other side of the mountain",
                "c");
        question2.check();
        question2.showResults();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS