0

I am creating a quiz with 15 questions that asks students what the area and perimeter are of a rectangle. I have to keep track of the answers from each question. I am having trouble figuring out how to ask two different questions randomly using JOptionPane.

Do I need two separate input dialog boxes for each question?

package proj3;
import javax.swing.JOptionPane; 

public class Project4App {
    public static void main( String args[ ] ) {

        Rectangle newRectangle = new Rectangle();

        String perimeterQuestion = new String("What is the perimeter of this rectangle?");
        String areaQuestion = new String("What is the area of this rectangle?");

        int rightAnswers = 0;
        int wrongAnswers = 0;
        String perimeterAnswer;
        int a = 0;


        int questionNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};



        for (int i = 0; i < questionNumber.length; i++ ) {
            newRectangle.dimensions();
            perimeterAnswer = JOptionPane.showInputDialog("Question #" + questionNumber[0] + "\n" + perimeterQuestion + "\n" 
        + newRectangle.toString());
            a = Integer.parseInt(perimeterAnswer);
            if (a == newRectangle.findPerimeter()) {
                rightAnswers += 1;
                JOptionPane.showMessageDialog(null, "Congratulations! You got it correct!");
            }
            else {
                wrongAnswers +=1;
                JOptionPane.showMessageDialog(null, "The correct answer was " + newRectangle.findPerimeter());
            }

            questionNumber[0] += 1; 
        }

    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 3
    *"I am having trouble figuring out how to ask two different questions randomly using JOptionPane."* What part are you stuck with? Making the questions random? Or placing both area and perimeter questions in the same dialog? – Frakcool Jun 16 '17 at 16:43

2 Answers2

2
  1. Create an Object that has two properties: question and answer
  2. Add all the Objects to an ArrayList
  3. Use Collections.sort(...) to sort the questions in random order
  4. Remove the question from index 0 and display the question in a new JOptionPane
  5. Validate the answer

Repeat steps 4-5 until finished.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • So I should but both the questions and answers in the same ArrayList? – Scott Zachary Jun 17 '17 at 15:40
  • @ScottZachary, that is NOT was I suggested. I suggested you create a custom Object with two properties. You know something like a Point object. When you create the Point object you pass in the x/y values as parameters. Then you have getX() and gety() method to access the data. Well you have the same situation except your two properties are question/answer. – camickr Jun 17 '17 at 16:16
1

The easiest solution is to put all of your answers into an array. Then get a random number in the range of the size of the array (How do I generate random integers within a specific range in Java?) and then proceed to pick that index out of the array of all your questions. Then set the value of the dialogue box to that value. This will create a randomization of your questions.

JoshKopen
  • 940
  • 1
  • 8
  • 22