4
\$\begingroup\$

I made a Java quiz program. The program will let you take the Java quiz and then display the quiz's result according to your score.

/*
 * Java Quiz
 * by Clint
 * 2020.07.07
 * This Java program let's you take a quiz. Quiz are taken at: https://www.w3schools.com/java/java_quiz.asp
 */
 
import javax.swing.JOptionPane;

public class JavaQuiz {

    static int points = 0;
    
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Welcome to Quiz.");
        
        // call the quiz method
        question1();
        question2();
        question3();
        question4();
        question5();
        question6();
        question7();
        question8();
        question9();
        question10();
        question11();
        question12();
        question13();
        question14();
        question15();
        result();
    }


    public static void question1() {
        String answer = JOptionPane.showInputDialog(null, "Question 1 of 15:\n" +
                "What is a correct syntax to output \"Hello World\" in Java?\n" +
                "A. echo(\"Hello World\")\n" +
                "B. Console.WriteLine(\"Hello World\")\n" +
                "C. System.out.println(\"Hello World\")\n" +
                "D. print(\"Hello World\")");
        switch (answer){
            case "C", "c" -> points++;
        }
    }


    public static void question2() {
        String answer =  JOptionPane.showInputDialog(null, "Question 2 of 15:\n" +
                "Java is short for \"Javascript\".\n" +
                "A. true\n" +
                "B. false\n" +
                "C. All of the above.\n" +
                "D. None of the above.");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question3() {
        String answer =  JOptionPane.showInputDialog(null, "Question 3 of 15:\n" +
                "How do you insert COMMENTS in Java code?\n" +
                "A. # This is a comment.\n" +
                "B. // This is a comment.\n" +
                "C. /* This is a comment\n" +
                "D. All of the above.");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question4() {
        String answer =  JOptionPane.showInputDialog(null, "Question 4 of 15:\n" +
                "Which data type is used to create a variable that should store text?\n" +
                "A. String\n" +
                "B. myString\n" +
                "C. string\n" +
                "D. Txt");
        switch (answer){
            case "A", "a" -> points++;
        }
    }


    public static void question5() {
        String answer =  JOptionPane.showInputDialog(null, "Question 5 of 15:\n" +
                "How do you create a variable with the numeric value 5?\n" +
                "A. num x = 5\n" +
                "B. x = 5;\n" +
                "C. float x = 5;\n" +
                "D. int x = 5;");
        switch (answer){
            case "D", "d" -> points++;
        }
    }


    public static void question6() {
        String answer =  JOptionPane.showInputDialog(null, "Question 6 of 15:\n" +
                "How do you create a variable with the floating number 2.8?\n" +
                "A. byte x = 2.8f\n" +
                "B. float x = 2.8f;\n" +
                "C. int x = 2.8f;\n" +
                "D. x = 2.8f;");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question7() {
        String answer = JOptionPane.showInputDialog(null, "Question 7 of 15:\n" +
                "Which method can be used to find the length of a string?\n" +
                "A. getSize()\n" +
                "B. length()\n" +
                "C. getLength()\n" +
                "D. len()");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question8() {
        String answer = JOptionPane.showInputDialog(null, "Question 8 of 15:\n" +
                "Which operator is used to add together two values?\n" +
                "A. The & sign\n" +
                "B. The * sign\n" +
                "C. The + sign\n" +
                "D. The / sign");
        switch (answer){
            case "C", "c" -> points++;
        }
    }


    public static void question9() {
        String answer = JOptionPane.showInputDialog(null, "Question 9 of 15:\n" +
                "The value of a string variable can be surrounded by single quotes.\n" +
                "A. True\n" +
                "B. False\n" +
                "C. All of the above.\n" +
                "D. None of the above.");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question10() {
        String answer = JOptionPane.showInputDialog(null, "Question 10 of 15:\n" +
                "Which method can be used to return a string in upper case letters?\n" +
                "A. tuc()\n" +
                "B. toUpperCase()\n" +
                "C. toupperCase()\n" +
                "D. touppercase()");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question11() {
        String answer = JOptionPane.showInputDialog(null, "Question 11 of 15:\n" +
                        "Which operator can be used to compare two values?\n" +
                        "A. <>\n" +
                        "B. ==\n" +
                        "C. =\n" +
                        "D. ><");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question12() {
        String answer = JOptionPane.showInputDialog(null, "Question 12 of 15:\n" +
                        "To declare an array in Java, define the variable type with:\n" +
                        "A. []\n" +
                        "B. ()\n" +
                        "C. {}\n" +
                        "D. None of the above.");
        switch (answer){
            case "A", "a" -> points++;
        }
    }


    public static void question13() {
        String answer = JOptionPane.showInputDialog(null, "Question 13 of 15:\n" +
                        "Array indexes start with:\n" +
                        "A. 1\n" +
                        "B. 0\n" +
                        "C. All of the above.\n" +
                        "D. None of the above.");
        switch (answer){
            case "B", "b" -> points++;
        }
    }


    public static void question14() {
        String answer = JOptionPane.showInputDialog(null, "Question 14 of 15:\n" +
                        "How do you create a method in Java?\n" +
                        "A. methodName()\n" +
                        "B. methodName[]\n" +
                        "C. methodName\n" +
                        "D. (methodName)");
        switch (answer){
            case "A", "a" -> points++;
        }
    }


    public static void question15() {
        String answer = JOptionPane.showInputDialog(null, "Question 15 of 15:\n" +
                        "How do you call a method in Java?\n" +
                        "A. methodName();\n" +
                        "B. methodName;\n" +
                        "C. (methodName);\n" +
                        "D. methodName[];");
        switch (answer){
            case "A", "a" -> points++;
        }
    }


    public static void result() {
        String name = JOptionPane.showInputDialog(null,"You finished the quiz.\nPlease enter your name?");
        if (points == 15)
        {
            JOptionPane.showMessageDialog(null, "Name: " + name + "\nTotal Score: " + points + "\nGood job!");
        }else if(points >= 10) {
            JOptionPane.showMessageDialog(null, "Name: " + name + "\nTotal Score: " + points + "\nYou Passed the quiz.");
        }else{
            JOptionPane.showMessageDialog(null, "Name: " + name + "\nTotal Score: " + points + "\nYou can still study more for the next quiz.");
        }
    }

}
```
\$\endgroup\$
2
\$\begingroup\$

The code you wrote works and satisfies the requirement of a quiz app. However, you can simplify and handle a few edge cases:

  • For every new question, you need to duplicate 10 lines of question* method. You can create encapsulate the question, options and the correct answer's index in a class and create an array to write all the questions and answers. Think about the future requirement to read the list of question/answers from a input file or a database.
class QuestionAnswer {
    String question;
    String optA;
    String optB;
    String optC;
    String optD;
    String correctAns;
    //all args constructor
}

static QuestionAnswer[] questions = {
    new QuestionAnswer("What is the correct syntax..", "echo (hello world)", "console..", "sysout", "printf", "C"),
    new QuestionAnswer("What java is short for ...", "echo (hello world)", "console..", "sysout", "printf", "C"),
     //... all questions
    };

You can then create a single method to handle all question and answers

// loop over questions array and call the displayAndCheckAnswer method 
        int correctResults = 0;
        for (int i = 0; i < questions.length; i++) {
            if (displayAndCheckAnswer(questions[i], i, questions.length)) {
                correctResults++;
            }
        }


    public static boolean displayAndCheckAnswer(QuestionAnswer qna, int index, int total) {
        String answer = JOptionPane.showInputDialog(null, "Question" + (index + 1) + "of " + total + ":\n" +
                qna.question +
                "A. " + qna.optA + "\n" +
                "B. " + qna.optB + "\n" +
                "C. " + qna.optC + "\n" +
                "D. " + qna.optD);

        return answer.equalsIgnoreCase(qna.correctAns);
    }
  • Handle edge cases like - what if user enters space at the end of the their result eg : "A ".
  • Better UI: You can render the questions and answers in JPanel and display a running correct answer count.
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you. I'll be practicing on it to make my code efficient and how to use JPanel for GUI. Thanks a lot. \$\endgroup\$ – user226918 Jul 7 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy