So i have this assignment,

Create a quiz game that displays, in turn five questions about any topic of your choice. All five questions should have the same three possible multiple-choice answers. For example, you might ask trivia question about Australia states for which the correct response is Victoria, NSW or WA. After each question is displayed allow the user to choose one, two, or all three answers by selecting JCheckBox. In other words if the user is sure of an answer, he will select just one box, but he is uncertain, he might select two or three boxes. When the user is ready to submit the answer(s), he clicks a button. If the user’s answer to the question is correct but has selected two boxes, award 2 points. If the user has selected all three boxes award 1 point. If the user selected fewer than three boxes but incorrect, the user receives 0 points. A total of 25 points is possible. If the user has accumulated more than 21 points at the end of the quiz, display the message” Fantastic!” If the user has accumulated more than 15 points, display a message “Very Good”, and if the user has accumulated fewer points, display “OK”.

So far i have this:

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

   public class HedgeYourBet extends JFrame implements ActionListener
   {
   //Class Main
      JLabel wcL, dirL;
      JButton playB, exit1B;

   //Class Quiz
      JPanel mainP, q1P, q2P, q3P, q4P, q5P, buttonP;
      JLabel q1L, q2L, q3L, q4L, q5L;
      JCheckBox q11, q12, q13, q21, q22, q23, q31, q32, q33, q41, q42, q43, q51, q52, q53;
      private int qnum1 = 0;
      JButton submitB, exit2B;
      QButton qb = new QButton();   
      public HedgeYourBet()
      {
         super("Quiz Game");
         setVisible(true);
         setSize(300,200);
         setLocation(500,280);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLayout(new GridLayout());
        //**********************
        //**DEFINING J-OBJECTS**
        //**********************
        //JPANEL    
         JPanel mainP = new JPanel(new GridLayout(3,1));
         mainP.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"WELCOME"));
         JPanel botP = new JPanel(new GridLayout());
         JPanel bot2P = new JPanel(new GridLayout(1,2));
        //JLABEL
         wcL = new JLabel("HELLO THERE! THIS IS A SIMPLE QUIZ GAME", JLabel.CENTER); 
         dirL = new JLabel("Click \"PLAY\" to Start the Game.", JLabel.CENTER);     
        //JBUTTON
         playB = new JButton("PLAY");
         exit1B = new JButton("EXIT");

        //***********************
        //**ADDING ON THE FRAME**
        //***********************

         add(mainP);
         mainP.add(wcL);
         mainP.add(dirL);
         mainP.add(botP);
         botP.add(bot2P);
         bot2P.add(playB);
         bot2P.add(exit1B);

        //*******************
        //**ADDING LISTENER**
        //*******************

         playB.addActionListener(this);
         exit1B.addActionListener(this);
      }

      public void actionPerformed(ActionEvent ae)
      {
         Object o = ae.getSource();

         if(o == playB)
         {
            this.setVisible(false);
            Quiz q = new Quiz();
            q.setVisible(true);
         }
         if(o == exit1B)
         {
            System.exit(0);
         }
      }

      public static void main(String[]args)
      {
         HedgeYourBet test = new HedgeYourBet();
      }

      class Quiz extends JFrame implements ItemListener
      {

         public Quiz()
         {
            super("Quiz Game");
            setVisible(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400,800);
            setLocation(500,280);
            setLayout(new GridLayout());

            //**********************
            //**DEFINING J-OBJECTS**
            //**********************

            //JPANELS
            mainP = new JPanel(new GridLayout(0,1));
            mainP.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"START!!!"));
            q1P = new JPanel(new GridLayout(4,1));
            q1P.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Question 1"));
            q2P = new JPanel(new GridLayout(4,1));
            q2P.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Question 2"));
            q3P = new JPanel(new GridLayout(4,1));
            q3P.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Question 3"));
            q4P = new JPanel(new GridLayout(4,1));
            q4P.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Question 4"));
            q5P = new JPanel(new GridLayout(4,1));
            q5P.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Question 5"));           
            buttonP = new JPanel(new GridLayout(1,2));

            //JLABELS
            q1L = new JLabel("What is the first name of the founder of \"www.FACEBOOK.com\"?");
            q2L = new JLabel("What is the first name of the founder of \"APPLE Inc.\"?");
            q3L = new JLabel("What is the latest i-series processor of INTEL?");
            q4L = new JLabel("What beverage does the cup in the logo of JAVA contains?");
            q5L = new JLabel("What is Adrian's nickname?");

            //JCHECKBOXES
            //question 1
            q11 = new JCheckBox("Marc",false);
            q12 = new JCheckBox("Mark",false);
            q13 = new JCheckBox("Marco",false);
            //
            q21 = new JCheckBox("Steve",false);
            q22 = new JCheckBox("Steven",false);
            q23 = new JCheckBox("Steff",false);
            //
            q31 = new JCheckBox("i3",false);
            q32 = new JCheckBox("i9",false);
            q33 = new JCheckBox("i7",false);
            //
            q41 = new JCheckBox("Buko Juice",false);
            q42 = new JCheckBox("Kopiko",false);
            q43 = new JCheckBox("Palamig",false);
            //
            q51 = new JCheckBox("Aids",false);
            q52 = new JCheckBox("Eds",false);
            q53 = new JCheckBox("Aidz",false);

            //JBUTTON
            submitB = new JButton("SUBMIT");
            exit2B = new JButton("EXIT");

            //***********************
            //**ADDING ON THE FRAME**
            //***********************
            add(mainP);
            mainP.add(q1P);
            q1P.add(q1L);
            q1P.add(q11);
            q1P.add(q12);
            q1P.add(q13);

            mainP.add(q2P);
            q2P.add(q2L);
            q2P.add(q21);
            q2P.add(q22);
            q2P.add(q23);  

            mainP.add(q3P);
            q3P.add(q3L);
            q3P.add(q31);
            q3P.add(q32);
            q3P.add(q33);

            mainP.add(q4P);
            q4P.add(q4L);
            q4P.add(q41);
            q4P.add(q42);
            q4P.add(q43);

            mainP.add(q5P);
            q5P.add(q5L);
            q5P.add(q51);
            q5P.add(q52);
            q5P.add(q53);

            mainP.add(buttonP);
            buttonP.add(submitB);
            buttonP.add(exit2B);

            //Adding Listener
            //for JCheckboxes
            q11.addItemListener(this);
            //for JButtons
            submitB.addActionListener(qb);
            exit2B.addActionListener(qb);
         }

         public void itemStateChanged(ItemEvent ie)
         {
            Object o = ie.getSource();

            if(o == q11)
            {
               int select = ie.getStateChange();

               if(select == ie.SELECTED)
               {
                  qnum1 += 1;
               }
               else
               {
                  qnum1 = qnum1;
               }
            }
         }

      }
      class QButton implements ActionListener
      {
         public void actionPerformed(ActionEvent ae)
         {
            Object o = ae.getSource();

            if(o == submitB)
            {
                JOptionPane.showMessageDialog(null,""+qnum1);
            }
            if(o == exit2B)
            System.exit(0);
         }
      }
   }

Please help me, i want to know how many check boxes are checked by the user. And i want to know if the user checked the correct answer. I cannot figure it out, so please help me experts :(

Because all your check boxes are in individual variables you will have to code a load of "if" tests to test each box to see if its checked and increment a count as required. It's an easy idea, but very tedious to code.
If you put all your check boxes into an array you could shorten the code a lot.

thanks for the reply jamescherill, can you show me a simple example of having an array of check boxes and how to test if its checked?

JCheckBox[] row = new JCheckBox[5];

the rest: iterate over the row, check each element whether it is checked or not.

i got the point on the array, but i have no idea how i can test if it is checked..?
please help me

how i can test if it is checked

You would use a syntax like this to call a check box's method:

referenceToCheckBox.methodThatSaysIfChecked()

an element in an array is a reference to an object and can be called the same way:

array[ix].aMethod()

See the API doc for the JCheckBox class for the method to call.

NormR1, can you give me the specific method to test if how many checkbox are check?

seriously. click on the 'this' link in my previous post and read what that page says. use that particular method.

The JCheckBox class extends another class. All the methods in the extended class are part of the JCheckBox class.

When reading the API doc after reading the methods for the main clas you need to continue reading the Methods inherited from for any classes that are extended.

Edited 4 Years Ago by NormR1

thanks for the replies but i can't get any idea or point on reading the API
im just a newbie on java programming, a student. so please clarify everything

point on reading the API

The API doc is where all the methods for a class are defined. To see what methods a class has and how to use them, you must read the API doc for the class.
Learning how to read the API doc is VERY IMPORTANT for a begining programmer.

For the JCheckBox (and for many other classes) you may also need to read the API doc the classes that JCheckBox extends.
The doc for the extended classes is in the section with this header:
Fields inherited from <the classname>

There may be more than one class that is extended. For each class that is extended, click on the link to <the classname> to go to the API doc for that class.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.