Programming Assignment Unit 1 – Programming 1
Programming Assignment Unit 1
The breakdown of the program:
1. Swing Components:
• The program uses Swing components (JFrame, JPanel, JLabel, JButton, JRadioButton,
ButtonGroup) to create a graphical user interface (GUI) for the quiz.
2. Questions and Answers:
• Questions and their corresponding options are stored in arrays (questionsHeaders,
questions, optionsArray), making it easy to manage and modify quiz content.
3. Correct Answers:
• Correct answers for each question are stored in the correctAnswers array as characters.
4. GUI Initialization:
• The GUI is initialized in the constructor (QuizProgram()) where JFrame, JPanel, JLabels,
JRadioButtons, and JButton are created and configured.
5. Layout Management:
• The layout of the main panel uses a combination of FlowLayout and BoxLayout to organize
components vertically.
6. ActionListener for Submit Button:
• An ActionListener is attached to the "Submit" button to handle button clicks. When clicked,
it calls the checkAnswer method.
7. checkAnswer Method:
• This method is responsible for checking the user's answer.
• It uses a switch statement to handle user input ('A', 'B', 'C', 'D').
• If the user's answer is correct, the score is incremented. If the input is invalid, an error
message is displayed.
8. getSelectedAnswer Method:
• This method iterates through the radio buttons to determine which one is selected and
returns the corresponding answer.
9. updateQuestion Method:
• This method updates the displayed question and options based on the current question
index.
10. showResult Method:
• This method calculates the percentage of correct answers and displays a message based on
the percentage.
Chrisna Badar
Programming Assignment Unit 1 – Programming 1
• The final result is shown in a JOptionPane.
• The application is closed (frame.dispose()) after displaying the result.
11. Main Method:
• The main method is the entry point of the program.
• It schedules the creation of the GUI on the event dispatch thread using
SwingUtilities.invokeLater.
12. Styling and Fonts:
• The program uses different fonts and styles to enhance the appearance of the GUI.
Link to the program in Github:
https://github.com/ChrisnaBadar/programming_1_written_assignment/blob/83b021281
20c8dce0f6cc342f8bb759d890269fd/QuizProgram.java
<Program Code on Next Page>
Chrisna Badar