Skip to content
Advertisement

Java quiz with multi choices in answers

I’m trying to develop a java quiz, here’s the basic idea: A quiz contains n questions, and a question has 0 to n true answers, I have to link that to a database, so my tables are:

Table Quiz:

id_quiz Integer Primary key, quiz_name Text

Table Question:

id_question Integer Primary key, question, id_quiz (foreign key)

Table answer:

id_answer Integer primary key, answer TEXT, statut TEXT, id_question (foreign key)

So my java classes are:

public class Quiz {
    int idQuiz;
    String QuizName;

    public Quiz(int id, String quiz) {
        this.idQuiz = id;
        this.QuizName = quiz;
    }

    // getters ...
}

My Question class:

public class Question {
    int idQuestion;
    String question;
    int idQuiz;

    public Question(int id, String question){
        this.idQuiz = id;
        this.question = question;
    }

    // getters ...
}

and the same for Answer:

public class Answer {
    int idAnswer;
    String answer;
    String statut;
    int idQuestion;

    public Answer(int id, String answer, String statut){
        this.idAnswer = id;
        this.answer = answer;
        this.statut = statut;
    }

    // getters ...
}

The main JFrame contains a JCombobox with the list of available quizzes, when the choice is done, a button that send the idQuiz to get the List of questions of the chosen quiz. In my quiz JFrame, I made a function that’s filling the question JLabel with a next button, and the multiple choises of this question with a JRadioButton to select.

I have a little problem to get the list of true answers, the user must choose all of true answers, not only one if there is more than one true question. Has someone an idea about that? Thanks.

Advertisement

Answer

I’m afraid you will need the help of Destructor The Destroyer Of Worlds.

He will attack the answers the user marked as correct one by one until the fate of the user is decided.

First, you need this very important class:

public class DestructorTheDestroyerOfWorlds
{
    private static List<Answer> correctAnswers;

    public static boolean destroyAnswers(List<Answer> userAnswers)
    {
        boolean success = true;
        for(Answer userAnswer : userAnswers) 
        {
            success &= destroyAnswer(userAnswer);
        }
        return success;
    }

    private static boolean destroyAnswer(Answer userAnswer)
    {
        for (int i = 0; i < correctAnswers.length; i++)
        {
            if (correctAnswers.get(i).idAnswer == userAnswer.idAnswer)
            {
                correctAnswers.remove(i);
                return true;
            }
        }
        return false;
    }

    public static boolean isOutOfAmmo()
    {
        return correctAnswers.isEmpty();
    }

    public static void loadUp(List<Answer> answers)
    {
        correctAnswers = answers;
    }
}

Now we have to actually get all our information somewhere, so:

List<Answer> possibleAnswers = readThoseFromTheDatabase();
List<Answer> correctAnswers = doAFilterOn(possibleAnswers);

List<Answer> userAnswers = new List();
if (checkbox1.isSelected()) userAnswers.add(possibleAnswers.get(0));
if (checkbox2.isSelected()) userAnswers.add(possibleAnswers.get(1));
if (checkbox3.isSelected()) userAnswers.add(possibleAnswers.get(2));
if (checkbox4.isSelected()) userAnswers.add(possibleAnswers.get(3));

Then comes the fun part:

DestructorTheDestroyerOfWorlds.loadUp(correctAnswers);

boolean successfullyDestroyedAllAnswers = DestructorTheDestroyerOfWorlds.destroyAnswers(userAnswers);

if (successfullyDestroyedAllAnswers && DestructorTheDestroyerOfWorlds.isOutOfAmmo())
{
    System.out.println("Oh fateful user, thy answers were right!");
}
else
{
    System.out.println("Oh shameful user, thy answers were wrong!");
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement