0

I am creating a quiz

I have a class which contains the GUI and code for a location to place questions that will be asked in my application. The class below will be used to hold a JLabel component (Among other UI elements), and should display a random question from a list of questions given to it. I have saved each of these 'question lists'as an ArrayList in another class.

I am aware you would have to use a random number generator to pick a line from the list of questions, by generating an index number, then using this to retrieve a line from the array to use as the text to display in the JLabel

I am unsure however how to do this when it comes to the actual code, please could anybody help.

My code is as follows:

package ZillionaireGUI;

import java.awt.Frame;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;


public class questionDialog extends JDialog {
private JLabel Question;
private JRadioButton answerThree;
private JRadioButton answerFour;
private JRadioButton answerTwo;
private JRadioButton answerOne;
public questionDialog(Frame parent) {
    super(parent);
}


public questionDialog(JFrame frame) {
    super(frame);
    initGUI();
}

private void initGUI() {
    try {

        getContentPane().setLayout(null);

        Question = new JLabel();
        getContentPane().add(Question);
        Question.setText("jLabel1");
        Question.setBounds(39, 127, 383, 29);

        answerOne = new JRadioButton();
        getContentPane().add(answerOne);
        answerOne.setText("jRadioButton1");
        answerOne.setBounds(26, 183, 93, 20);

        answerTwo = new JRadioButton();
        getContentPane().add(answerTwo);
        answerTwo.setText("jRadioButton1");
        answerTwo.setBounds(130, 183, 93, 20);

        answerThree = new JRadioButton();
        getContentPane().add(answerThree);
        answerThree.setText("jRadioButton1");
        answerThree.setBounds(247, 183, 93, 20);

        answerFour = new JRadioButton();
        getContentPane().add(answerFour);
        answerFour.setText("jRadioButton1");
        answerFour.setBounds(360, 183, 93, 20);

        ButtonGroup group = new ButtonGroup();
        group.add(answerOne);
        group.add(answerTwo);
        group.add(answerThree);
        group.add(answerFour);


        this.setSize(490, 393);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

import java.util.*;
public class Questions {
public static void main(String[] args) {
    ArrayList<String> SPOquestions = new ArrayList<String>(); // adding the sports questions for the quiz

    SPOquestions.add("Who won the 2005 Formula One World Championship?");
    SPOquestions.add("Which team has the most Formula One Constructors titles?");
    SPOquestions.add("In what year did Roger Federer win his first 'Grand Slam'?");
    SPOquestions.add("How many 'Grand Slams' has Rafael Nadal won?");
    SPOquestions.add("Who has scored the most amount of goals in the Premier League?");
    SPOquestions.add("Who has won the most World Cups in football?");
    SPOquestions.add("How many MotoGP titles does Valentino Rossi hold?");
    SPOquestions.add("Who was the 2013 MotoGP champion?");
    SPOquestions.add("Who won the 2003 Rugby World Cup?");
    SPOquestions.add("In rugby league, how many points are awarded for a try?");
    SPOquestions.add("Who is the youngest ever snooker World Champion?");
    SPOquestions.add("In snooker, what is the highest maximum possible break?");
    SPOquestions.add("How many majors has Tiger Woods won?");
    SPOquestions.add("In golf, what is the tournament between the USA and Europe called?");
    SPOquestions.add("How many World Championships has darts player Phil Taylor won?");
    SPOquestions.add("What is the maximum possible amount of points a player can earn from throwing three darts?");
    SPOquestions.add("How many gold medals did Michael Phelps win at the 2008 Beijing Olympics?");
    SPOquestions.add("Who won the 2012 Olympic 100 metres mens race?");
    SPOquestions.add("Which of these events are not a part of the heptathlon?");
    SPOquestions.add("When was the first modern Olympics held?");

    ArrayList<String> MUSquestions = new ArrayList<String>(); // adding the music questions

    MUSquestions.add("'Slash' was a member of which US rock band?");
    MUSquestions.add("Brian May was a member of which English rock band?");
    MUSquestions.add("What is the name of the music festival held annually in the town of Boom, Belgium?");
    MUSquestions.add("The rapper Tupac Shakuer '2Pac' died in which year?");
    MUSquestions.add("Which of these bands headlined the 2013 Glastonbury music festival?");
    MUSquestions.add("Which of these people designed the 'Les Paul' series of guitars?");
    MUSquestions.add("Keith Moon was a drummer for which English rock band?");
    MUSquestions.add("Kanye West has a total of how many Grammy awards?");
    MUSquestions.add("Beyonce Knowles was formally a member of which US group?");
    MUSquestions.add("In which US city was rapper 'Biggie Smalls' born?");
    MUSquestions.add("Michael Jackson's first number one single in the UK as a solo artist was what?");
    MUSquestions.add("The best selling album of all time in the UK is what?");
    MUSquestions.add("The best selling album of all time in the US is what?");
    MUSquestions.add("What is the artist known as 'Tiesto's real name?");
    MUSquestions.add("Which of these was not a member of The Beatles?");

    ArrayList<String> GENquestions = new ArrayList<String>(); // adding general knowledge questions

    GENquestions.add("Who was the second President of the United States?");
    GENquestions.add("The youngest son of Bob Marley was who?");
    GENquestions.add("In the film '8 Mile', the character portrayed by Eminem is known as what?");
    GENquestions.add("What is the capital city of New Zealand?");
    GENquestions.add("What is the capital city of Australia?");
    GENquestions.add("How many millilitres are there in an English pint?");
    GENquestions.add("What was the biggest selling game for the PS2 worldwide?");
    GENquestions.add("What is the last letter of the Greek alphabet?");
    GENquestions.add("Who created the television series Futurama?");
    GENquestions.add("A word which reads the same backwards as it does forwards is known as a what?");
    GENquestions.add("A 'baker's dozen' consists of how many items?");
    GENquestions.add("World War 1 officially occured on which date?");
    GENquestions.add("'Trouble and strife' is cockney rhyming slang for what?");
    GENquestions.add("Who was the last Prime Minister to hail from the labour party in the UK?");
    GENquestions.add("WalMart is the parent company of which UK based supermarket chain?");




}

}

3 Answers 3

2

First, you need to generate a random number based on the size of your array. Get a random value in the range of 0.0 - 1.0 from Math.random() and multiply it by the size of your questions array (for example, MUSquestions.size()) - that will give you your random index into the array.

Then. use the ArrayList get method, passing in the index, to get the string you want.

Sign up to request clarification or add additional context in comments.

4 Comments

So do I generate this random number in my arraylist class, or the class where the JLabel is?
Probably the class where the JLabel is. Something like, label.setText(MUSquestions.get(Math.random()*MUSquestions.size()));
Hmm, I tried this but it could not resolve the MUSquestions part?
Your main method is never creating a questionDialog - so you're not even going to see your user interface. You'll need to create your questionDialog from main. When you do, change the constructor of questionDialog to take the four questions you want to display. Then when you call the constructor, use part of the snippet I mentioned before.
2

I would definitely use the collections.shuffle this way : List arrlist = new ArrayList();

  // populate the list
  arrlist.add("A");
  arrlist.add("B");
  arrlist.add("C");  

  System.out.println("Initial collection: "+arrlist);

  // shuffle the list
  Collections.shuffle(arrlist);

  System.out.println("Final collection after shuffle: "+arrlist);

Comments

1

To get a random number:

Random rand = new Random();

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;

Read more here : How do I generate random integers within a specific range in Java?

To use it to access a random question

String randomQuestion=SPOquestions.get(randomNum);

And to show it in the label

Question.setText(randomQuestion);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.