Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Quiz - Problem with random questions...
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goober99




PostPosted: Mon Apr 28, 2008 4:47 pm   Post subject: Java Quiz - Problem with random questions...

Hey, so im really new at Java and need a little assistance. I have a multiple choice program (may sound framiliar), and everything works great.... except ONE thing. To get queestions to come up in a random order every time, I converted my Q&A array into an array list, shuffled it, then converted back... The only problem is that on the second call of the question asking method, the questions come up blank in the dialogue box (no text). Im guessing that the array values get wiped on every call of the method, but I really have no idea. Oh yeah, and this suckers due tommorow, so please work with what I have, I know I'm close... I think Razz

code:

 
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

 
public class MainFrame extends JFrame{
    public static int[] ANS = {3,1,2,3};
    public static int cntr = 0;
    public static int score = 0;
    public String[][] X2 = new String[3][6];
    public MainFrame(int GO){
    super("Java Quiz");
     
     
     String[][] X = new String[3][6];
     
     String[] ANS = {"c","a","c"};
   
     String[] Qs = {"An object...","Objects preform actions by executing...","WHat does OOP stand for?"};
     
     String[] As = {"A) Represents useful data.","A) Methods.","A) Ontario Online Police." };
     
     String[] Bs = {"B) Preforms actions.","B) Actions","B) Output Organized Packets."};
       
     String[] Cs = {"C) Both A and B.","C) Procedures","C) Object Oriented Programming."};
           
     String[] Ds = {"D) None of the above.","D) Events","D) Online Organized Paraphenilia."};     
     
     for(int i=0; i<3; i++){
         X[i][0] = Qs[i];
         X[i][1] = As[i];
         X[i][2] = Bs[i];
         X[i][3] = Cs[i];
         X[i][4] = Ds[i];
         X[i][5] = ANS[i];
     }
     
                         
     java.util.List AQ = Arrays.asList(X);
     Collections.shuffle(AQ);
     System.out.println(GO);
     if(GO==0){
        X2 = (String[][]) AQ.toArray();
     }
         
      setLocationRelativeTo(null);
      setSize(364,150);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container content = getContentPane();
      JPanel panel2 = new JPanel();
      panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
      panel2.add(new JLabel(X2[cntr][0]));
      panel2.add(new JLabel(X2[cntr][1]));
      panel2.add(new JLabel(X2[cntr][2]));
      panel2.add(new JLabel(X2[cntr][3]));
      panel2.add(new JLabel(X2[cntr][4]));
      content.setLayout(new BorderLayout());
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
      content.add(panel2, BorderLayout.NORTH);
      JButton button1 = new JButton("Answer A");
      JButton button2 = new JButton("Answer B");
      JButton button3 = new JButton("Answer C");
      JButton button4 = new JButton("Answer D");
      panel.add(button1);
      panel.add(button2);
      panel.add(button3);
      panel.add(button4);
     
      if(X2[cntr][5] == "a"){
            button1.addActionListener( new ButtonListener1(this));
      }else{
            button1.addActionListener( new ButtonListener2(this));
      }
      if(X2[cntr][5] == "b"){
            button2.addActionListener( new ButtonListener1(this));
      }else{
            button2.addActionListener( new ButtonListener2(this));
      }
      if(X2[cntr][5] == "c"){
            button3.addActionListener( new ButtonListener1(this));
      }else{
            button3.addActionListener( new ButtonListener2(this));
      }
      if(X2[cntr][5] == "d"){
            button4.addActionListener( new ButtonListener1(this));
      }else{
            button4.addActionListener( new ButtonListener2(this));
      }
      content.add(panel, BorderLayout.SOUTH);
      content.add(panel2, BorderLayout.WEST);
      setVisible(true);
      setResizable(false);
     
   
}


   private class ButtonListener1 implements ActionListener
   {
      private JFrame parentComponent;
      ButtonListener1(JFrame parentComponent)
      {
         this.parentComponent=parentComponent;
      }

      public void actionPerformed(ActionEvent e)
      {
         dispose();
           
         score = score + 1;
         cntr = cntr + 1;
         if (cntr<3){
            MainFrame mf = new MainFrame(1);
        }else{
             JOptionPane.showMessageDialog(null,"Your score was "+score+", thats shit!");
        }
      }
   }
   
   private class ButtonListener2 implements ActionListener
   {
      private JFrame parentComponent;
      ButtonListener2(JFrame parentComponent)
      {
         this.parentComponent=parentComponent;
      }

      public void actionPerformed(ActionEvent e)
      {
         dispose();
         
         cntr = cntr + 1;
         if (cntr<3){
            MainFrame mf = new MainFrame(1);
        }else{
             JOptionPane.showMessageDialog(null,"Your score was "+score+", thats shit!");
        }
      }
   }
   

}
[/i]
Sponsor
Sponsor
Sponsor
sponsor
Barbarrosa




PostPosted: Mon Apr 28, 2008 6:54 pm   Post subject: Re: Java Quiz - Problem with random questions...

Always remember...

OBJECT-ORIENTED DESIGN is what you should aim for. This looks like functional programming, which does everything line-by-line. In object-oriented design, objects have methods, things they able to do. They also have their own personal environment, with objects in it that belong only to them.

Try doing the initializing of the arrays in one method, and the individual pieces of work in another. I know you don't have forever to change around your code, so I'll help you through a bit.


    Rename X2 to questionsAndAnswers & X to qAndA
    Instantiate the program within a main method without assigning it to a variable.
    Move into a private void method:
    code:

    String[][] questionsAndAnswers = new String[3][6];
    ...
    if(GO==0){
                            X2 = (String[][]) AQ.toArray();
                    }

    Make two button listeners & use them for all of the buttons.
    Use a switch statement & get the char value of X2[cntr][5].charAt(0) at the beginning of it.
    In the button listeners, remove the reference to the parent & that parameter.
    Also in the button listeners, remove the assignment of the new frame while still instantiating it.


Your problem will be fixed, and your code will be a little neater.
goober99




PostPosted: Mon Apr 28, 2008 9:53 pm   Post subject: Re: Java Quiz - Problem with random questions...

Yeah, the functional programming is due to our Compsci teacher teaching Turing for years, then trying out java...

I think I understand what your saying here, I should move all the array initialization stuff into a private void array... and that will prevent the array from restarting every time I recall the MainFrame method?
wtd




PostPosted: Mon Apr 28, 2008 10:27 pm   Post subject: RE:Java Quiz - Problem with random questions...

I think you mean "procedural" or "imperative" programming rather than functional programming.
Barbarrosa




PostPosted: Tue Apr 29, 2008 11:40 pm   Post subject: Re: Java Quiz - Problem with random questions...

goober99 wrote:

I think I understand what your saying here, I should move all the array initialization stuff into a private void array... and that will prevent the array from restarting every time I recall the MainFrame method?

Actually, it was the instruction to use the same button listener for all buttons that would really do the trick. The rest of it was mostly tidying up your code. I just thought that it made sense to initialize everything at the same time, in the same place, isolated from other parts of the code.

wtd wrote:

I think you mean "procedural" or "imperative" programming rather than functional programming.

I think procedural programming may be the correct term. Programming to complete tasks, without thinking in terms of objects that can complete those tasks... or something like that.
Barbarrosa




PostPosted: Wed Apr 30, 2008 4:07 pm   Post subject: Re: Java Quiz - Problem with random questions...

Hit Wall Dang, I didn't give you the right instructions before. you needed to hand over ANS[cntr] to the next MainFrame.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: