From 284c3e3ec6c2cba8131e58e64ce5a18f678ddeb5 Mon Sep 17 00:00:00 2001 From: Piyush Date: Thu, 9 Oct 2025 15:06:00 +0530 Subject: [PATCH] Added Quiz app using swing gui --- Quiz-app/Quiz.json | 102 ++++++++++++++++++ Quiz-app/src/QuizApp.java | 217 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 319 insertions(+) create mode 100644 Quiz-app/Quiz.json create mode 100644 Quiz-app/src/QuizApp.java diff --git a/Quiz-app/Quiz.json b/Quiz-app/Quiz.json new file mode 100644 index 0000000..c502c88 --- /dev/null +++ b/Quiz-app/Quiz.json @@ -0,0 +1,102 @@ +[ + { + "question": "What does JVM stand for?", + "options": ["Java Variable Manager", "Java Virtual Machine", "Java Value Method", "Java Version Manager"], + "correctAnswer": "Java Virtual Machine" + }, + { + "question": "Which of the following is a valid data type in Java?", + "options": ["int", "String", "boolean", "All of the above"], + "correctAnswer": "All of the above" + }, + { + "question": "Which keyword is used to declare a constant in Java?", + "options": ["const", "final", "static", "constant"], + "correctAnswer": "final" + }, + { + "question": "What is the default value of a boolean in Java?", + "options": ["true", "false", "0", "null"], + "correctAnswer": "false" + }, + { + "question": "Which method starts a thread in Java?", + "options": ["run()", "start()", "execute()", "begin()"], + "correctAnswer": "start()" + }, + { + "question": "Which package is used for input/output in Java?", + "options": ["java.io", "java.util", "java.net", "java.lang"], + "correctAnswer": "java.io" + }, + { + "question": "What operator is used for object comparison?", + "options": ["==", "equals()", "compare()", "isEqual()"], + "correctAnswer": "equals()" + }, + { + "question": "What is the size of a float in Java?", + "options": ["32 bits", "64 bits", "8 bits", "16 bits"], + "correctAnswer": "32 bits" + }, + { + "question": "Which of these is not a Java keyword?", + "options": ["class", "interface", "method", "package"], + "correctAnswer": "method" + }, + { + "question": "Which method is used to find the length of an array in Java?", + "options": ["length()", "size()", "getLength()", "arraySize()"], + "correctAnswer": "length()" + }, + { + "question": "Which access modifier allows visibility within the same package?", + "options": ["public", "protected", "default", "private"], + "correctAnswer": "default" + }, + { + "question": "Which class is the superclass of all classes in Java?", + "options": ["Object", "Class", "Main", "System"], + "correctAnswer": "Object" + }, + { + "question": "What is the extension of a compiled Java file?", + "options": [".java", ".class", ".javac", ".exe"], + "correctAnswer": ".class" + }, + { + "question": "Which keyword is used to inherit a class in Java?", + "options": ["extend", "extends", "inherit", "implements"], + "correctAnswer": "extends" + }, + { + "question": "What is a collection of methods with no implementation called?", + "options": ["Class", "Abstract Class", "Interface", "Object"], + "correctAnswer": "Interface" + }, + { + "question": "Which exception is thrown when a file is not found?", + "options": ["IOException", "FileNotFoundException", "Exception", "RuntimeException"], + "correctAnswer": "FileNotFoundException" + }, + { + "question": "Which of these is used to perform all input and output operations in Java?", + "options": ["Streams", "Variables", "Data Types", "Classes"], + "correctAnswer": "Streams" + }, + { + "question": "Which of the following is not a type of loop in Java?", + "options": ["for", "while", "do-while", "foreach"], + "correctAnswer": "foreach" + }, + { + "question": "What does the `final` keyword do when applied to a class?", + "options": ["Allows inheritance", "Prevents inheritance", "Allows method overriding", "Prevents method overriding"], + "correctAnswer": "Prevents inheritance" + }, + { + "question": "What is the default value of a String variable in Java?", + "options": ["empty string", "null", "undefined", "0"], + "correctAnswer": "null" + } +] diff --git a/Quiz-app/src/QuizApp.java b/Quiz-app/src/QuizApp.java new file mode 100644 index 0000000..47377f4 --- /dev/null +++ b/Quiz-app/src/QuizApp.java @@ -0,0 +1,217 @@ +import javax.swing.*; +import java.awt.*; +import java.nio.file.*; +import java.sql.Array; +import java.util.*; +import com.google.gson.*; +import java.util.List; + +class QuizApp extends JFrame { + private int i= 0; + private int j = 0; + private int count = 0; + private List questionList; + private JLabel questionLabel; + private JLabel questionText; + private JRadioButton option1, option2, option3, option4; + public ButtonGroup optionsGroup; + private JButton nextButton; + + QuizApp() { + loadJson(); + + Font questionFont = new Font("Futura", Font.BOLD, 32); + Font optionsFont = new Font("Calibri", Font.PLAIN, 28); + + questionLabel = new JLabel("Question", JLabel.CENTER); + questionLabel.setFont(questionFont); + questionLabel.setForeground(new Color(255, 255, 240)); + + questionText = new JLabel("", JLabel.CENTER); + questionText.setFont(optionsFont); + questionText.setForeground(new Color(255, 255, 240)); + + option1 = new JRadioButton(); + option2 = new JRadioButton(); + option3 = new JRadioButton(); + option4 = new JRadioButton(); + + option1.setFont(optionsFont); + option2.setFont(optionsFont); + option3.setFont(optionsFont); + option4.setFont(optionsFont); + + option1.setBackground(new Color(100, 180, 250)); + option2.setBackground(new Color(65, 105, 225)); + option3.setBackground(new Color(100, 180, 250)); + option4.setBackground(new Color(65, 105, 225)); + + option1.setForeground(Color.WHITE); + option2.setForeground(Color.WHITE); + option3.setForeground(Color.WHITE); + option4.setForeground(Color.WHITE); + + optionsGroup = new ButtonGroup(); + optionsGroup.add(option1); + optionsGroup.add(option2); + optionsGroup.add(option3); + optionsGroup.add(option4); + + nextButton = new JButton("Next"); + nextButton.setFont(optionsFont); + nextButton.setBackground(new Color(50, 205, 50)); + nextButton.setForeground(Color.WHITE); + nextButton.setFocusPainted(false); + nextButton.setBorder(BorderFactory.createLineBorder(new Color(34, 139, 34))); + + + + Container c = getContentPane(); + c.setLayout(null); + c.setBackground(new Color(25, 40, 112)); + + questionLabel.setBounds(110, 30, 600, 50); + questionText.setBounds(110, 80, 600, 50); + option1.setBounds(150, 160, 500, 50); + option2.setBounds(150, 220, 500, 50); + option3.setBounds(150, 280, 500, 50); + option4.setBounds(150, 340, 500, 50); + nextButton.setBounds(250, 420, 300, 50); + + c.add(questionLabel); + c.add(questionText); + c.add(option1); + c.add(option2); + c.add(option3); + c.add(option4); + c.add(nextButton); + + nextButton.addActionListener( + a-> + { + checkAnswer(); + loadnextquestion(); + + } + ); + + loadnextquestion(); + + setVisible(true); + setSize(800, 600); + setLocationRelativeTo(null); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Quiz Application"); + + + } + + void loadJson() + { + String filename = "Quiz.json"; + Path p = Paths.get(filename); + try { + if (Files.exists(p)) + { + String result = new String(Files.readAllBytes(p)); + Gson gson = new Gson(); + Questions[] obj1 = gson.fromJson(result,Questions[].class); + questionList = new ArrayList<>(Arrays.asList(obj1)); + + } else + { + JOptionPane.showMessageDialog(null, "File doesn't exist"); + } + + } + catch(Exception e) + { + JOptionPane.showMessageDialog(null,e.getMessage()); + } + } + + void loadnextquestion() + { + + if(i