fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.ArrayList;
  5.  
  6. public class QuizApplication extends Frame implements ActionListener {
  7. private Label questionLabel;
  8. private CheckboxGroup optionsGroup;
  9. private Checkbox option1, option2, option3, option4;
  10. private Button previousButton, nextButton, submitButton;
  11. private ArrayList<Question> questions;
  12. private int currentQuestionIndex;
  13. private int score;
  14.  
  15. public QuizApplication() {
  16. setLayout(new FlowLayout());
  17. setTitle("Quiz Application");
  18. setSize(400, 300);
  19. addWindowListener(new WindowAdapter() {
  20. public void windowClosing(WindowEvent e) {
  21. dispose();
  22. }
  23. });
  24.  
  25. questionLabel = new Label();
  26. optionsGroup = new CheckboxGroup();
  27. option1 = new Checkbox();
  28. option2 = new Checkbox();
  29. option3 = new Checkbox();
  30. option4 = new Checkbox();
  31. previousButton = new Button("Previous");
  32. nextButton = new Button("Next");
  33. submitButton = new Button("Submit");
  34.  
  35. add(questionLabel);
  36. add(option1);
  37. add(option2);
  38. add(option3);
  39. add(option4);
  40. add(previousButton);
  41. add(nextButton);
  42. add(submitButton);
  43.  
  44. previousButton.addActionListener(this);
  45. nextButton.addActionListener(this);
  46. submitButton.addActionListener(this);
  47.  
  48. questions = loadQuestionsFromFile("questions.txt");
  49. currentQuestionIndex = 0;
  50. score = 0;
  51. displayQuestion(currentQuestionIndex);
  52. }
  53.  
  54. private ArrayList<Question> loadQuestionsFromFile(String filename) {
  55. ArrayList<Question> loadedQuestions = new ArrayList<>();
  56. try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
  57. String line;
  58. while ((line = br.readLine()) != null) {
  59. String[] parts = line.split(",");
  60. if (parts.length >= 5) {
  61. Question question = new Question(parts[0], parts[1], parts[2], parts[3], parts[4]);
  62. loadedQuestions.add(question);
  63. }
  64. }
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. return loadedQuestions;
  69. }
  70.  
  71. private void displayQuestion(int index) {
  72. Question currentQuestion = questions.get(index);
  73. questionLabel.setText(currentQuestion.getQuestion());
  74. option1.setLabel(currentQuestion.getOption1());
  75. option2.setLabel(currentQuestion.getOption2());
  76. option3.setLabel(currentQuestion.getOption3());
  77. option4.setLabel(currentQuestion.getOption4());
  78. }
  79.  
  80. private void checkAnswer() {
  81. Question currentQuestion = questions.get(currentQuestionIndex);
  82. Checkbox selectedOption = optionsGroup.getSelectedCheckbox();
  83. if (selectedOption != null && selectedOption.getLabel().equals(currentQuestion.getCorrectAnswer())) {
  84. score++;
  85. }
  86. }
  87.  
  88. private void displayResult() {
  89. removeAll();
  90. Label resultLabel = new Label("Your score: " + score + "/" + questions.size());
  91. add(resultLabel);
  92. setVisible(true);
  93. }
  94.  
  95. public void actionPerformed(ActionEvent e) {
  96. if (e.getSource() == nextButton) {
  97. checkAnswer();
  98. if (currentQuestionIndex < questions.size() - 1) {
  99. currentQuestionIndex++;
  100. displayQuestion(currentQuestionIndex);
  101. }
  102. } else if (e.getSource() == previousButton) {
  103. if (currentQuestionIndex > 0) {
  104. currentQuestionIndex--;
  105. displayQuestion(currentQuestionIndex);
  106. }
  107. } else if (e.getSource() == submitButton) {
  108. checkAnswer();
  109. displayResult();
  110. }
  111. }
  112.  
  113. public static void main(String[] args) {
  114. QuizApplication quizApp = new QuizApplication();
  115. quizApp.setVisible(true);
  116. }
  117. }
  118.  
  119. class Question {
  120. private String question;
  121. private String option1;
  122. private String option2;
  123. private String option3;
  124. private String option4;
  125. private String correctAnswer;
  126.  
  127. public Question(String question, String option1, String option2, String option3, String option4, String correctAnswer) {
  128. this.question = question;
  129. this.option1 = option1;
  130. this.option2 = option2;
  131. this.option3 = option3;
  132. this.option4 = option4;
  133. this.correctAnswer = correctAnswer;
  134. }
  135.  
  136. // Getters
  137. public String getQuestion() {
  138. return question;
  139. }
  140.  
  141. public String getOption1() {
  142. return option1;
  143. }
  144.  
  145. public String getOption2() {
  146. return option2;
  147. }
  148.  
  149. public String getOption3() {
  150. return option3;
  151. }
  152.  
  153. public String getOption4() {
  154. return option4;
  155. }
  156.  
  157. public String getCorrectAnswer() {
  158. return correctAnswer;
  159. }
  160. }
Success #stdin #stdout 0.03s 25672KB
stdin
1
2
10
42
11
stdout
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;

public class QuizApplication extends Frame implements ActionListener {
    private Label questionLabel;
    private CheckboxGroup optionsGroup;
    private Checkbox option1, option2, option3, option4;
    private Button previousButton, nextButton, submitButton;
    private ArrayList<Question> questions;
    private int currentQuestionIndex;
    private int score;

    public QuizApplication() {
        setLayout(new FlowLayout());
        setTitle("Quiz Application");
        setSize(400, 300);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });

        questionLabel = new Label();
        optionsGroup = new CheckboxGroup();
        option1 = new Checkbox();
        option2 = new Checkbox();
        option3 = new Checkbox();
        option4 = new Checkbox();
        previousButton = new Button("Previous");
        nextButton = new Button("Next");
        submitButton = new Button("Submit");

        add(questionLabel);
        add(option1);
        add(option2);
        add(option3);
        add(option4);
        add(previousButton);
        add(nextButton);
        add(submitButton);

        previousButton.addActionListener(this);
        nextButton.addActionListener(this);
        submitButton.addActionListener(this);

        questions = loadQuestionsFromFile("questions.txt");
        currentQuestionIndex = 0;
        score = 0;
        displayQuestion(currentQuestionIndex);
    }

    private ArrayList<Question> loadQuestionsFromFile(String filename) {
        ArrayList<Question> loadedQuestions = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 5) {
                    Question question = new Question(parts[0], parts[1], parts[2], parts[3], parts[4]);
                    loadedQuestions.add(question);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return loadedQuestions;
    }

    private void displayQuestion(int index) {
        Question currentQuestion = questions.get(index);
        questionLabel.setText(currentQuestion.getQuestion());
        option1.setLabel(currentQuestion.getOption1());
        option2.setLabel(currentQuestion.getOption2());
        option3.setLabel(currentQuestion.getOption3());
        option4.setLabel(currentQuestion.getOption4());
    }

    private void checkAnswer() {
        Question currentQuestion = questions.get(currentQuestionIndex);
        Checkbox selectedOption = optionsGroup.getSelectedCheckbox();
        if (selectedOption != null && selectedOption.getLabel().equals(currentQuestion.getCorrectAnswer())) {
            score++;
        }
    }

    private void displayResult() {
        removeAll();
        Label resultLabel = new Label("Your score: " + score + "/" + questions.size());
        add(resultLabel);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == nextButton) {
            checkAnswer();
            if (currentQuestionIndex < questions.size() - 1) {
                currentQuestionIndex++;
                displayQuestion(currentQuestionIndex);
            }
        } else if (e.getSource() == previousButton) {
            if (currentQuestionIndex > 0) {
                currentQuestionIndex--;
                displayQuestion(currentQuestionIndex);
            }
        } else if (e.getSource() == submitButton) {
            checkAnswer();
            displayResult();
        }
    }

    public static void main(String[] args) {
        QuizApplication quizApp = new QuizApplication();
        quizApp.setVisible(true);
    }
}

class Question {
    private String question;
    private String option1;
    private String option2;
    private String option3;
    private String option4;
    private String correctAnswer;

    public Question(String question, String option1, String option2, String option3, String option4, String correctAnswer) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.correctAnswer = correctAnswer;
    }

    // Getters
    public String getQuestion() {
        return question;
    }

    public String getOption1() {
        return option1;
    }

    public String getOption2() {
        return option2;
    }

    public String getOption3() {
        return option3;
    }

    public String getOption4() {
        return option4;
    }

    public String getCorrectAnswer() {
        return correctAnswer;
    }
}