Question
=====Question.java===== import javax.swing.JOptionPane; //Abstract class created using abstract keyword public abstract class Question { static int nQuestions = 0; static int nCorrect = 0; String
=====Question.java=====
import javax.swing.JOptionPane;
//Abstract class created using abstract keyword public abstract class Question {
static int nQuestions = 0; static int nCorrect = 0; String question; String correctAnswer; //The ask function is declared as abstract. //Any class extending Question class will require to implement the ask function abstract String ask(); void check() { nQuestions++; String answer = ask(); if (answer.equals(correctAnswer)) { JOptionPane.showMessageDialog(null,"Correct!"); nCorrect++; } else { JOptionPane.showMessageDialog(null,"Incorrect. The correct answer is "+correctAnswer+"."); } } void showResults() { JOptionPane.showMessageDialog(null,nCorrect+" correct out of"+nQuestions+" questions"); } }
=====MultipleChoiceQuestion.java====
//Modified MultipleChoiceQuestion class, extending abstract class Question
import javax.swing.JOptionPane;
public class MultipleChoiceQuestion extends Question{
MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) { question = query+" "; question += "A. "+a+" "; question += "B. "+b+" "; question += "C. "+c+" "; question += "D. "+d+" "; question += "E. "+e+" "; 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. Please enter A, B, C, D, or E."); } } }
======TrueFalseQuestion=====
//Newly created TrueFalseQuestion Class. Extending abstract class Question
import javax.swing.JOptionPane;
public class TrueFalseQuestion extends Question {
TrueFalseQuestion(String question, String answer) { this.question = "TRUE or FALSE: "+question; this.correctAnswer = answer; correctAnswer= correctAnswer.toUpperCase(); } @Override String ask() { while (true) { String answer = JOptionPane.showInputDialog(question); //Convert the answer to uppercase, for easy validation answer = answer.toUpperCase(); boolean valid = (answer.equals("F") || answer.equals("FALSE") || answer.equals("N") || answer.equals("NO") || answer.equals("T") || answer.equals("TRUE") || answer.equals("Y") || answer.equals("YES")); if (valid) { answer = answer.toUpperCase(); //Convert the answer into TRUE or FALSE. //We have used Ternary function. if answer belongs to valid false statements, we return FALSE //Else return true // return (condition)? true : false return (answer.equals("F") || answer.equals("FALSE") || answer.equals("N") || answer.equals("NO")) ? "FALSE" : "TRUE"; } JOptionPane.showMessageDialog(null,"Invalid answer. Please enter TRUE or FALSE."); } }
}
=====Quiz.java=====
//The Main Class with added question for True or False
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 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.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(); MultipleChoiceQuestion question3 = new MultipleChoiceQuestion("Which fruit and color has the same name?", "Apple", "Orange", "Grape", "Banana", "Melon", "b"); question3.check(); question3.showResults(); MultipleChoiceQuestion question4 = new MultipleChoiceQuestion("Where is sun located?", "Air", "Water", "Sea", "Forest", "Sky", "e"); question4.check(); question4.showResults(); TrueFalseQuestion trueFalseQuestion = new TrueFalseQuestion("Sky is blue", "TRUE"); trueFalseQuestion.check(); trueFalseQuestion.showResults(); TrueFalseQuestion trueFalseQuestion1 = new TrueFalseQuestion("Sea is red", "FALSE"); trueFalseQuestion1.check(); trueFalseQuestion1.showResults(); TrueFalseQuestion trueFalseQuestion2 = new TrueFalseQuestion("Cricket is a game", "TRUE"); trueFalseQuestion2.check(); trueFalseQuestion2.showResults(); TrueFalseQuestion trueFalseQuestion3 = new TrueFalseQuestion("Dove is an animal", "FALSE"); trueFalseQuestion3.check(); trueFalseQuestion3.showResults(); TrueFalseQuestion trueFalseQuestion4 = new TrueFalseQuestion("Java is a programming language", "TRUE"); trueFalseQuestion4.check(); trueFalseQuestion4.showResults(); } }
First, modify your main program for easier testing. Open your "CS1102" project in Eclipse. Bring up "Quiz.java" in the editor pane. Use "*" and "*/" to comment out all your questions except one true/false question. Run your program to make sure it asks just one true/falseStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started