Creating an online exam project in Java Swing without a database is possible but it comes with some limitations.
To create an online exam project, you can follow these steps:
- Create a new Java project in your IDE.
- Create a package named “exam” to store all the classes related to the exam project.
- Create a class named “Exam” and extend the JFrame class.
- In the Exam class, add all the necessary components such as JLabel, JTextArea, JButton, etc. for the exam interface.
- Create a class named “Question” to store the questions and answers of the exam.
- In the Question class, add a constructor to initialize the question and its options.
- Create an array of Question objects to store all the questions.
- Use the random number generator to generate a random number to select a question from the array of questions.
- Display the selected question and its options on the exam interface.
- Add ActionListener to each button to check the answer of the selected question.
Here’s a sample code for the Exam class:
package exam; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Exam extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel contentPane; private JLabel lblQuestion; private JButton btnOption1; private JButton btnOption2; private JButton btnOption3; private JButton btnOption4; private Question[] questions = { new Question("What is the capital of France?", "Paris", "Rome", "Madrid", "Berlin"), new Question("What is the largest planet in our solar system?", "Jupiter", "Saturn", "Neptune", "Uranus"), new Question("What is the largest continent in the world?", "Asia", "Africa", "Europe", "North America"), new Question("What is the currency of Japan?", "Yen", "Dollar", "Euro", "Pound"), new Question("What is the chemical symbol for gold?", "Au", "Ag", "Fe", "Cu") }; public Exam() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); setContentPane(contentPane); contentPane.setLayout(null); lblQuestion = new JLabel("Question"); lblQuestion.setBounds(10, 11, 414, 14); contentPane.add(lblQuestion); btnOption1 = new JButton("Option 1"); btnOption1.setBounds(10, 50, 194, 23); btnOption1.addActionListener(this); contentPane.add(btnOption1); btnOption2 = new JButton("Option 2"); btnOption2.setBounds(230, 50, 194, 23); btnOption2.addActionListener(this); contentPane.add(btnOption2); btnOption3 = new JButton("Option 3"); btnOption3.setBounds(10, 95, 194, 23); btnOption3.addActionListener(this); contentPane.add(btnOption3); btnOption4 = new JButton("Option 4"); btnOption4.setBounds(230, 95, 194, 23); btnOption4.addActionListener(this); contentPane.add(btnOption4); generateQuestion(); } private void generateQuestion() { Random random = new Random(); int index = random.nextInt(questions.length); Question question = questions[index]; lblQuestion.setText(question.getQuestion()); btnOption1.setText