1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

quiz problem.

Discussion in 'Java' started by saytri, Dec 29, 2007.

  1. saytri

    saytri New Member

    Joined:
    Dec 29, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am making a quiz where one can choose from 3 different titles: either plate tectonics, rivers or rocks. I typed the questions in a textfile and i stored the answers in an array. My problem is that i wish to display the questions randomly, and not always in the same format. What can i do? Thanks a lot for the help. I appreciate it a lot! :)

    This is the code i have written:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
     
    public class GQ extends JFrame implements ActionListener {
        private static final int FRAME_WIDTH = 140;
        private static final int FRAME_HEIGHT = 160;
        private static final int FRAME_X_ORIGIN = 70;
        private static final int FRAME_Y_ORIGIN = 50;
        AnswerStore answerStore = new AnswerStore();
     
        
        public static void main (String[] args) {
            
            
           
    
            JOptionPane.showMessageDialog(null, "This is a Geography Quiz");
            JOptionPane.showMessageDialog(null, "Good Luck");
     
            
     
                GQ frame = new GQ();
                frame.setVisible(true);
           
        }
     
        public GQ() {
            Container contentPane;
            JButton button1, button2, button3, button4, button5;
     
            setSize (FRAME_WIDTH, FRAME_HEIGHT);
            setTitle("Geography Quiz");
            setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
     
            contentPane = getContentPane();
            contentPane.setBackground(Color.pink);
            contentPane.setLayout(new FlowLayout());
     
            button1 = new JButton("Plate Tectonics");
            button2 = new JButton("Rivers");
            button3 = new JButton("Rocks");
            button4 = new JButton("Quit");
     
            contentPane.add(button1);
            contentPane.add(button2);
            contentPane.add(button3);
            contentPane.add(button4);
        
            button1.addActionListener(this);
            button1.setActionCommand("b1");
            button2.addActionListener(this);
            button2.setActionCommand("b2");
            button3.addActionListener(this);
            button3.setActionCommand("b3");
            button4.addActionListener(this);
            button4.setActionCommand("b4");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
      
         public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            String[] questions = null;
            String[] answers = null;
            if (ac.equals("b1")) {
                questions = readFile("plate_tectonics.txt");
                
                answers = answerStore.tectonicAnswers;
            } else if(ac.equals("b2")) {
                questions = readFile("rivers.txt");
                answers = answerStore.riverAnswers;
            } else if(ac.equals("b3")) {
                questions = readFile("rocks.txt");
                answers = answerStore.rockAnswers;
            } else if (ac.equals("b4")) {
                System.exit(0);
            }
            askQuestions(questions, answers);
        }
           
        
        
        
     
        private String[] readFile(String path) {
            Scanner s = null;
            StringBuilder sb = new StringBuilder();
            String separator = "\n";
            try {
                s = new Scanner(new BufferedReader(new FileReader(path)));
    //            s.useDelimiter(",\\s*");
                while (s.hasNext()) {
                    sb.append(s.nextLine() + separator);
    //                JOptionPane.showMessageDialog(null,s.next());
                }
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            } finally {
                if (s != null) 
                    s.close();
    //            break;
            }
           
           
        }
     
        private void askQuestions(String[] questions, String[] answers) {
            
            System.out.printf("questions = %s%nanswers = %s%n",
                               Arrays.toString(questions),
                               Arrays.toString(answers));
            int count = 0;
            int point = 0;
            for(int j = 0; j < questions.length; j++) {
                String input = JOptionPane.showInputDialog(null, questions[j]);
                if(answers[j].equals(input))
                  { 
                    count++;
                    point++;
                }   
                }
                JOptionPane.showMessageDialog(null, "You answered " + count +
                                          " out of " + questions.length +
                                          " questions correctly.");
                JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + ((point*100)/10) + " % ");
            
              if(point>=0 && point<=3)
              {
                 
                  JOptionPane.showMessageDialog(null, "You need to Improve");
               }
               
               
             if(point>=4 && point<=7)
              {
                 
                  JOptionPane.showMessageDialog(null, "Good");
               }
               
               if(point>=8 && point<=10)
              {
                 
                  JOptionPane.showMessageDialog(null, "You did Great");
               }
                                        
          }
        }
     
    class AnswerStore  {
        String[] tectonicAnswers = {
            "Hellenic", "destructive", "100km", "Italy", "Wegner",
            "Mariana", "Sicily", "created", "constructive", "Mediterranean"
        };
        
        String[] riverAnswers = {
            "Gorges", "Meanders", "Levees", "Yes", "Less Economic Developed Countries",
            "crescent shaped lakes", "More Economic Developed Countries", "No", "River Discharge", "No"
        };
        
        String[] rockAnswers = {
            "40km", "Igneous Rock", "Sedimentary", "Basalt", "Organic",
            "pressure", "Oolites", "Igneous", "dark black", "basalt"
        };
    
     
    
    }
    Thanks again! :)
     
  2. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    i don't know java but i think it is a logical question ..could u just post the fomat of your question files..
    i believe a solution can be found after that.
     

Share This Page