Question

Using Java develop a GUI based simple quiz game. The questions and answers of the game...

Using Java develop a GUI based simple quiz game. The questions and answers of the game will be stored in a text file (i.e. our database). On the first page the users will be provided with some instructions on how to play the game and next button to go to the next page. On the next page will be a start button to either start the game and an exit button to exit the game. Once the game starts there will be approx. 10-20 general knowledge question with four options to select from. The options will have radio buttons so there can only be one answer to the given question. Once all the questions are answered the total score shall be displayed along with an option to play again or exit the game.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please run this as java application and check the result.

package javaapplication1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class ChoiceGame implements ActionListener {

JFrame fr;
JRadioButton rbtn1, rbtn2, rbtn3, rbtn4;
JButton btn1, btn2,btn3;
JLabel lb1, lb2;
ButtonGroup bg;
String ques[] = {"World Cancer Day is observed on which day?",
"World Radio Day is celebrated on:",
"Abraham Lincoln's Birthday is celebrated on..",
"In India, Taj Mahotsav is celebrated on which day?",
"20 February is observed as...",
"Central Excise Day is observed on:",
"What is the theme of World Cancer Day 2020?",
"13 February is celebrated as birth anniversary of which personality?",
"World NGO Day is observed on...",
" On which day, International Mother Language Day is celebrated?"};
String op1[] = {"3 February", "11 February","10 February","18 February","Word Scout Day","22 February","Not beyond us","Sarojini Naidu","20 February","15 February"};
String op2[] = {"4 February", "13 February","12 February","20 February","World Day of Social Justice","23 February","We can, I can","Mother Teresa","24 February","18 February"};
String op3[] = {"6 February", "15 February","14 February","22 February","World NGO Day","24 February","I Am and I Will","Anandibai Gopalrao Joshi","27 February","21 February"};
String op4[] = {"9 February", "18 February","16 February","None of the above","World Wildlife Day","25 February","None of the above","Reita Faria Powell","29 February","25 February"};
String ans[] = {"4 February", "13 February","12 February","18 February","World Day of Social Justice","24 February","I Am and I Will","Sarojini Naidu","27 February","21 February"};
int cn = 0;

ChoiceGame() {
fr = new JFrame();
fr.setLayout(null);
fr.setSize(600, 600);
Container c = fr.getContentPane();
c.setBackground(Color.cyan);
lb1 = new JLabel(ques[0]);
lb1.setBounds(50, 50, 1000, 30);
fr.add(lb1);
lb1.setFont(new Font("chiller", Font.BOLD, 30));
rbtn1 = new JRadioButton(op1[0]);
rbtn1.setBounds(100, 120, 100, 30);
fr.add(rbtn1);
rbtn2 = new JRadioButton(op2[0]);
rbtn2.setBounds(350, 120, 100, 30);
fr.add(rbtn2);
rbtn3 = new JRadioButton(op3[0]);
rbtn3.setBounds(100, 200, 100, 30);
fr.add(rbtn3);
rbtn4 = new JRadioButton(op4[0]);
rbtn4.setBounds(350, 200, 100, 30);
fr.add(rbtn4);
bg = new ButtonGroup();
bg.add(rbtn1);
bg.add(rbtn2);
bg.add(rbtn3);
bg.add(rbtn4);
rbtn1.addActionListener(this);
rbtn2.addActionListener(this);
rbtn3.addActionListener(this);
rbtn4.addActionListener(this);
btn1 = new JButton("Sumbit");
btn1.setBounds(100, 400, 100, 30);
fr.add(btn1);
btn2 = new JButton("Next");
btn2.setBounds(250, 400, 100, 30);
fr.add(btn2);
  
btn1.addActionListener(this);
btn2.addActionListener(this);
fr.setVisible(true);
}

public static void main(String s[]) {
new ChoiceGame();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String en = "";
if (rbtn1.isSelected()) {
en = rbtn1.getText();
}
if (rbtn2.isSelected()) {
en = rbtn2.getText();
}
if (rbtn3.isSelected()) {
en = rbtn3.getText();
}
if (rbtn4.isSelected()) {
en = rbtn4.getText();
}
if (en.equals(ans[cn])) {
JOptionPane.showMessageDialog(null, "Correct Answer ! score : 10!!!");
} else {
JOptionPane.showMessageDialog(null, "Wrong Answer! Please try again!!!");
}
}
  
if (e.getSource() == btn2) {
cn++;
lb1.setText(ques[cn]);
rbtn1.setText(op1[cn]);
rbtn2.setText(op2[cn]);
rbtn3.setText(op3[cn]);
rbtn4.setText(op4[cn]);
}
}
}

Add a comment
Know the answer?
Add Answer to:
Using Java develop a GUI based simple quiz game. The questions and answers of the game...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT