CS1102 Programming Assignment Unit 5
CS1102 Programming Assignment Unit 5
CS1102 Programming Assignment Unit 5
import javax.swing.JOptionPane;
public class MultipleChoiceQuestion extends Question {
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
// 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);
}
}
}
//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.");
}
}