Skip to content

Instantly share code, notes, and snippets.

@jonahaung
Created July 11, 2020 14:01
Show Gist options
  • Save jonahaung/ffa682a7112be72280a0ef4f174c00d6 to your computer and use it in GitHub Desktop.
Save jonahaung/ffa682a7112be72280a0ef4f174c00d6 to your computer and use it in GitHub Desktop.
public class Quiz {
public static void main(String[] args) {
// Question 1
MultipleChoiceQuestion question1 = new MultipleChoiceQuestion(
"Name the most popular programming language used for developing Android Apps.", // question
"Python", // A
"Swift", // B
"Java", // C
"HTML", // D
"C++", // E
"C"); // Correct Answer
// Question 2
MultipleChoiceQuestion question2 = new MultipleChoiceQuestion(
"Name the most popular programming language used for developing iOS Apps.", // question
"Python", // A
"Swift", // B
"Java", // C
"HTML", // D
"C++", // E
"B"); // Correct Answer
// Question 3
MultipleChoiceQuestion question3 = new MultipleChoiceQuestion(
"Name the most popular programming language used in Machine Learning.", // question
"Python", // A
"Swift", // B
"Java", // C
"HTML", // D
"C++", // E
"A"); // Correct Answer
// Question 4
MultipleChoiceQuestion question4 = new MultipleChoiceQuestion(
"Latest programming language used for creating native Android apps apart from Java.", // question
"Python", // A
"Swift", // B
"Java", // C
"Kotlin", // D
"C++", // E
"D"); // Correct Answer
// Question 5
MultipleChoiceQuestion question5 = new MultipleChoiceQuestion(
"Createing native apps for Android and iOS using React", // question
"Flutter", // A
"Swift", // B
"Java", // C
"Kotlin", // D
"Rect Native", // E
"E"); // Correct Answer
// Question 6
MultipleChoiceQuestion question6 = new MultipleChoiceQuestion(
"Google’s UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase.", // question
"Flutter", // A
"Swift", // B
"Java", // C
"Kotlin", // D
"Rect Native", // E
"A"); // Correct Answer
question1.check();
question2.check();
question3.check();
question4.check();
question5.check();
question6.check();
// show the summary results
MultipleChoiceQuestion.showResults();
}
}
import javax.swing.JOptionPane;
public class MultipleChoiceQuestion {
static int nQuestions = 0;
static int nCorrect = 0;
String question;
String correctAnswer;
MultipleChoiceQuestion(String query, String a, String b, String c, String d, String e, String answer) {
question = query+"\n"; // Initialize "question" using the "query" parameter followed by each choice parameter, "a"-"e".
question += "A. "+a+"\n";
question += "B. "+b+"\n";
question += "C. "+c+"\n";
question += "D. "+d+"\n";
question += "E. "+e+"\n";
correctAnswer = answer.toUpperCase(); // Initialize "correctAnswer" to the parameter "answer". Convert it to upper case
}
// method that ask a question and return an upper-cased answer
String ask() {
String answer = JOptionPane.showInputDialog(question).toUpperCase();
while (!isValidAnswer(answer)) {
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
answer = ask();
}
return answer;
}
// method that take the question and check the correct answer
void check() {
nQuestions ++; // increment questions
String answer = ask(); // call the "ask" method with one of its parameters
if (answer.equals(correctAnswer)) {
nCorrect ++; // increment correct answers
JOptionPane.showMessageDialog(null,"Correct!"); // display message for correct answer
} else {
JOptionPane.showMessageDialog(null,"Incorrect. The correct answer is " + correctAnswer); // display message for incorrect answer
}
}
// to display the number of questions and correct answers
static void showResults() {
// display the number of questions and the number of correct answers using the static member variables
JOptionPane.showMessageDialog(null, nCorrect + " correct out of " + nQuestions + " questions ");
}
// helper method to check valid answer
private boolean isValidAnswer(String answer) {
if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E")) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment