KSUFans Archives

Fan Life => The Endzone Dive => Topic started by: badke on April 30, 2006, 10:44:23 PM

Title: Anybody know Java?
Post by: badke on April 30, 2006, 10:44:23 PM
I'm new with Java and programming all together. I'm trying to put together a quiz program using JOptionPane. I want the questions to be random and the answers random as well. So that when a user takes the test the questions are never in the same order, and the answers to each question are never in the same order. But I'm lost on how to do it, this is the code I have so far:

Code: [Select]
import javax.swing.JOptionPane;

class Quiz
{
    public static String [] question;
    public static String [][] answer;
    public static int qCount = 0;

    public static String [] loadQuestions( )
    {
        String [] q = new String [4];
        // The questions
        q[0] = "What is...?";
        q[1] = "Where is...?";
        q[2] = "Who is...?";
        q[3] = "Why is...?";

        return q;
    }

    public static String [][] loadAnswers( )
    {
        String [][] a = new String[4][];
        // The answers
        a[0] = new String [] { "A", "B", "C", "D" };
        a[1] = new String [] { "A", "B", "C", "D" };
        a[2] = new String [] { "A", "B", "C", "D" };
        a[3] = new String [] { "A", "B", "C", "D" };

        return a;
    }

    public static String askOne( int item )
    {
    Object userAnswer;
    userAnswer = JOptionPane.showInputDialog( null, question[item], "Question " + ++qCount,  JOptionPane.QUESTION_MESSAGE, null, answer[item], answer[item][0] );
    if ( userAnswer == null )
    System.exit( 0 );
    return userAnswer.toString( );
    }

    public static void main ( String [] args )
    {
    question = loadQuestions( );
            answer = loadAnswers( );
            for ( int k = 0; k<4; k++ )
                System.out.println( askOne( k ) );
    }
}

How in the hell do I make the questions and each question's answers display randomly? :confused:
Title: Re: Anybody know Java?
Post by: swish1 on May 01, 2006, 01:17:20 AM
i took cis 200 5 years ago and have effectively blocked it from my memory so i dont think id be any help.  i do remember things about for loops and strings and random pointless crap that for some reason the alcohol hasnt rid me of yet.
Title: Re: Anybody know Java?
Post by: fatty fat fat on May 01, 2006, 04:39:17 AM
I just crapped my pants.
Title: Re: Anybody know Java?
Post by: Saulbadguy on May 01, 2006, 06:46:59 AM
JavaCat is a good guy.
Title: Re: Anybody know Java?
Post by: jeffy on May 01, 2006, 10:10:49 AM
JavaCat is a good guy.

ASH was cooler.
Title: Re: Anybody know Java?
Post by: kougar24 on May 01, 2006, 12:02:37 PM
pbadke, I'll help you out when I get some time here in awhile.
Title: Re: Anybody know Java?
Post by: badke on May 01, 2006, 01:29:07 PM
Excellent, thanks.
Title: Re: Anybody know Java?
Post by: badke on May 01, 2006, 01:56:43 PM
Ok, so I think I have the basic idea down. This is the code:

Code: [Select]
import javax.swing.JOptionPane;

class Quiz
{
    public static String [] question;
    public static String [][] answer;
    public static int qCount = 0;

    public static String [] loadQuestions( )
    {
        // instantiate the question array
        String [] q = new String [4];

        // store the questions
        q[0] = "What is...?";
        q[1] = "Where is...?";
        q[2] = "Who is...?";
        q[3] = "Why is...?";

        return q;
    }

    public static String [][] loadAnswers( )
    {
        // instantiate the row vector
        String [][] a = new String[4][];

        // store the answers
        a[0] = new String [] { "Q1 A", "Q1 B", "Q1 C", "Q1 D" };
        a[1] = new String [] { "Q2 A", "Q2 B", "Q2 C", "Q2 D" };
        a[2] = new String [] { "Q3 A", "Q3 B", "Q3 C", "Q3 D" };
        a[3] = new String [] { "Q4 A", "Q4 B", "Q4 C", "Q4 D" };

        return a;
    }

    public static String askOne( int item )
    {
    Object userAnswer;
    userAnswer = JOptionPane.showInputDialog( null, question[item], "Question " + ++qCount,  JOptionPane.QUESTION_MESSAGE, null, answer[item], answer[item][0] );
    if ( userAnswer == null )
    System.exit( 0 );
    return userAnswer.toString( );
    }

    public static void main ( String [] args )
    {
    question = loadQuestions( ); // Load the questions
            answer = loadAnswers( ); // Load the answers
   
    /* Randomize the questions in the quiz */
    int lastQuestion = question.length-1;
    while( lastQuestion > 0 )
    {
    int r = (int) (Math.random() * (lastQuestion - 0 + 1) ) + 0;
    question[r] = question[lastQuestion];
    answer[r] = answer[lastQuestion];
    lastQuestion--;
    }

    /* Ask the questions */
            for ( int k = 0; k<4; k++ )
                System.out.println( askOne( k ) );
    }
}

It randomizes the questions. But it repeats one of the questions and doesn't include another one. So I'll have one question that's repeated and another question that is never asked.
Title: Re: Anybody know Java?
Post by: badke on May 01, 2006, 02:00:18 PM
Umm, and now looking at my random number, I dunno what I did there lol.
Title: Re: Anybody know Java?
Post by: Super PurpleCat on May 01, 2006, 04:35:49 PM
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.

Title: Re: Anybody know Java?
Post by: kougar24 on May 01, 2006, 06:51:40 PM
Java.util (http://java.sun.com/j2se/1.5.0/docs/api/java/util/package-summary.html)
Title: Re: Anybody know Java?
Post by: AzCat on May 01, 2006, 06:54:30 PM
That's the trouble with kids these days, they need a library to do all of their work for them.   :jerkoff:
Title: Re: Anybody know Java?
Post by: fatty fat fat on May 01, 2006, 08:32:54 PM
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.



spring 2005 = a long time back?  :)
Title: Re: Anybody know Java?
Post by: badke on May 02, 2006, 01:20:16 PM
I figured out a way to do it just by swapping both the question and answer arrays at the same time.

Now I'm trying to figure out how to tally points in the quiz. One way I could think of was to just put \n in front of the correct answer in the answer array. Then search the user's answer to see if \n is in it, if it is, give them a point. However, I'm not sure how to search for \n exactly, any ideas?

Code: [Select]
import javax.swing.JOptionPane;
import java.util.Random;

class Quiz
{
    public static String [] question;
    public static String [][] answer;
    public static int qCount = 0;
    public static int points = 0;
    public static int attempted = 0;

    public static String [] loadQuestions( )
    {
        // instantiate the question array
        String [] q = new String [4];

        // store the questions
        q[0] = "What is...?";
        q[1] = "Where is...?";
        q[2] = "Who is...?";
        q[3] = "Why is...?";

        return q;
    }

    public static String [][] loadAnswers( )
    {
        // instantiate the row vector
        String [][] a = new String[4][];

        // store the answers
        a[0] = new String [] { "\nQ1 A", "Q1 B", "Q1 C", "Q1 D" };
        a[1] = new String [] { "\nQ2 A", "Q2 B", "Q2 C", "Q2 D" };
        a[2] = new String [] { "\nQ3 A", "Q3 B", "Q3 C", "Q3 D" };
        a[3] = new String [] { "\nQ4 A", "Q4 B", "Q4 C", "Q4 D" };

        return a;
    }

    public static String askOne( int item )
    {
    Object userAnswer;
    userAnswer = JOptionPane.showInputDialog( null, question[item], "Question " + ++qCount,  JOptionPane.QUESTION_MESSAGE, null, answer[item], answer[item][0] );
    if ( userAnswer == null )
System.exit( 0 );
    if ( userAnswer == "\n*" )
points++;
attempted++;
    return userAnswer.toString( );
    }

    public static void main ( String [] args )
    {
// Load the questions and answers
question = loadQuestions( );
answer = loadAnswers( );
   
// Randomize the questions in the quiz
int lastQuestion = question.length-1;
while( lastQuestion > 0 )
{
int r = (int) (Math.round(Math.random()*lastQuestion));
String tempQuestion = question[r]; question[r] = question[lastQuestion]; question[lastQuestion] = tempQuestion;
String [] tempAnswer = answer[r]; answer[r] = answer[lastQuestion]; answer[lastQuestion] = tempAnswer;
lastQuestion--;
}

// Randomize the order of the answers to each question
for( int k = 0; k<answer.length; k++)
{
int lastAnswer = question.length-1;
while( lastAnswer > 0 )
{
int r = (int) (Math.round(Math.random()*lastAnswer));
String temp = answer[k][r]; answer[k][r] = answer[k][lastAnswer]; answer[k][lastAnswer] = temp;
lastAnswer--;
}
}

// Ask the questions
for ( int k = 0; k<question.length; k++ )
System.out.println( askOne( k ) );
JOptionPane.showMessageDialog( null, "You attempted a total of " + attempted + " questions. You got " + points + " correct. ", "Results Of Your Quiz", JOptionPane.PLAIN_MESSAGE );
    }
}

It'd be nice of Java had a wildcard function so I could just use:

Code: [Select]
if ( userAnswer == "\n*" )
points++;

So, anybody know how I search for \n in userAnswer?
Title: Re: Anybody know Java?
Post by: kougar24 on May 02, 2006, 07:33:42 PM
I would advise against trying to manipulate an JOptionPane for the user's answer. Strings or ints are much easier to manipulate.
Title: Re: Anybody know Java?
Post by: Super PurpleCat on May 02, 2006, 08:13:55 PM
If I remember right (been a looong time since I did any Java work), java.util contains a class you can use to easily shuffle a collection.  I mean, that's all you're trying to do here is reorder a simple one-dimensional array. 

So my advice, do some googling on java.util and see what you come up with for shuffling an array.



spring 2005 = a long time back?  :)

I know English is like your third language, but "Java work" is pretty clear I believe.