Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
569 views

CS1102 Programming Assignment Unit 5

This document contains code for a multiple choice quiz program with multiple choice and true/false questions. The MultipleChoiceQuestion and TrueFalseQuestion classes extend the abstract Question class and implement methods to display the question, accept and validate the user's answer, check if it is correct, and tally the results. The main method creates sample multiple choice and true/false questions and calls their check methods to run the quiz.

Uploaded by

sam akil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
569 views

CS1102 Programming Assignment Unit 5

This document contains code for a multiple choice quiz program with multiple choice and true/false questions. The MultipleChoiceQuestion and TrueFalseQuestion classes extend the abstract Question class and implement methods to display the question, accept and validate the user's answer, check if it is correct, and tally the results. The main method creates sample multiple choice and true/false questions and calls their check methods to run the quiz.

Uploaded by

sam akil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//MultipleChoiceQuestion

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+"\n";
question += "A. "+a+"\n";
question += "B. "+b+"\n";
question += "C. "+c+"\n";
question += "D. "+d+"\n";
question += "E. "+e+"\n";

correctAnswer = answer.toUpperCase();
}
String ask() {
while(true) {
String answer = JOptionPane.showInputDialog(question);
answer=answer.toUpperCase();
while (true) {
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.");
answer = ask();
}
}
}
}
//Quiz

public class Quiz {

public static void main(String[] args) {

// Question 1
MultipleChoiceQuestion question1 = new MultipleChoiceQuestion(
"What is Java?.","Its a programming language","Its a thread","Its a
Software","Its a Virus","There is nothing Like java","A");
question1.check();

// Question 2
MultipleChoiceQuestion question2 = new MultipleChoiceQuestion(
"What is a Computer?.","Its a Book","Its an Instrument","A computer is an
electronic device that manipulates information, or data","Its Software","Its
a keyboard","C");
question2.check();

// Question 3
MultipleChoiceQuestion question3 = new MultipleChoiceQuestion(
"What is Software?.","Battery","Charger","excutable","Query", "the programs
and other operating information used by a computer", "E");
question3.check();

// Question 4
MultipleChoiceQuestion question4 = new MultipleChoiceQuestion(
"What is LAN?.","Its a Program","Its an Instrument","Network","Local Area
Network","Its a straight line","D");
question4.check();

//Question5
Question question5= new TrueFalseQuestion("Software is better
than Hardware!", "t") ;
question5.check();

//Question6
Question question6= new TrueFalseQuestion("Python is better than
JAVA!", "f") ;
question6.check();

//Question7
Question question7= new TrueFalseQuestion("Quiz are simpler than
Assigments!", "t") ;
question7.check();

//Question8
Question question8= new TrueFalseQuestion("Zambia is not a
landlocked Country!", "False") ;
question8.check();

MultipleChoiceQuestion.showResults();
}
}
//Question

import javax.swing.JOptionPane;
public abstract class Question{
static int nQuestions = 0;
static int nCorrect = 0;

String question;
String correctAnswer;
abstract String ask();
void check() {
nQuestions ++;
String answer = ask();
if (answer.equals(correctAnswer)) {
nCorrect ++;
JOptionPane.showMessageDialog(null,"Correct!");
} else {
JOptionPane.showMessageDialog(null,"Incorrect. The correct
answer is " + correctAnswer);
}
}

static void showResults() {


JOptionPane.showMessageDialog(null, nCorrect + " Answers Correct
out of " + nQuestions + " Questions ");
}

}
//TrueFalseQuestion

import javax.swing.JOptionPane;
public class TrueFalseQuestion extends Question{
String ask() {
while (true) {
String answer =
JOptionPane.showInputDialog(question).toUpperCase();
answer = answer.toUpperCase();
if (answer.equals("T") || answer.equals("Y") ||
answer.equals("YES")) answer = "TRUE";
if (answer.equals("F") || answer.equals("N") ||
answer.equals("NO")) answer = "FALSE";
boolean valid=(answer.equals("FALSE")|| answer.equals("TRUE"));
if (valid) return answer;
JOptionPane.showMessageDialog(null,"Invalid answer. Please
enter TRUE or FALSE.");
}
}

TrueFalseQuestion(String question, String answer){


this.question = "TRUE or FALSE: "+question;
answer = answer.toUpperCase();
if (answer.equals("T") || answer.equals("TRUE") || answer.equals("Y")
|| answer.equals("YES")) correctAnswer = "TRUE";
if (answer.equals("F") || answer.equals("FALSE") || answer.equals("N")
|| answer.equals("NO")) correctAnswer = "FALSE";
}
}

You might also like