CS1102 Programming Assignment Unit 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3
At a glance
Powered by AI
The document discusses creating multiple choice and true/false quiz questions in Java using classes like MultipleChoiceQuestion and TrueFalseQuestion. It also shows a Quiz class that runs sample questions.

The MultipleChoiceQuestion and TrueFalseQuestion classes are used to create and ask multiple choice and true/false questions. They extend the base Question class.

The Quiz class contains the main method and is used to run sample questions by creating instances of MultipleChoiceQuestion and TrueFalseQuestion and calling their check methods.

//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