View Single Post
  #5 (permalink)  
Old 01-20-2008, 12:57 PM
mcal mcal is offline
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Thanks. That helped a lot. I just got a problem. Because, i don't know why but the for loop isn't working. So the counter isn't being incremented and when time is over it still continues displaying the questions. It doesn't even check whether the answers are correct or not. Do i have something wrong in the for loop? Thanks a lot for your help. :-)

This is the code:

Code:
import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener.*; import java.util.*; import java.io.*; import javax.swing.*; import java.util.Calendar; import java.awt.image.*; import javax.imageio.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.EOFException; public class GQ extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 1024; private static final int FRAME_HEIGHT = 768; private static final int FRAME_X_ORIGIN = 0; private static final int FRAME_Y_ORIGIN = 0; private JLabel prompt; private JPanel image; private JLabel response; private JMenu fileMenu; private JMenu HelpMenu; private JMenu OptionMenu; static final String dataFile1 = "plate_tectonics.dat"; static final String dataFile2 = "rivers.dat"; static final String dataFile3 = "rocks.dat"; AnswerStore answerStore = new AnswerStore(); boolean timeForMore; String question; public GQ() { Container contentPane; contentPane = getContentPane(); JButton button1, button2, button3, button4, button5; setSize (FRAME_WIDTH, FRAME_HEIGHT); setTitle("Geography Quiz"); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); createFileMenu(); createHelpMenu(); createOptionMenu(); JMenuBar menuBar= new JMenuBar(); setJMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(HelpMenu); menuBar.add(OptionMenu); response=new JLabel(); response.setBounds(50, 50, 250, 50); contentPane.add(response); response=new JLabel(); response.setBounds(50, 50, 250, 50); contentPane.add(response); 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 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); new GQ(); GQ File = new GQ(); } String ac = event.getActionCommand(); String[] questions = null; String[] answers = null; if (ac.equals("b1")) { final JPF1 appRef = new JPF1(); JFrame frame = new JFrame("CountDown"); MP panel = new MP(); panel.addCountDownListener(appRef); panel.setVisible(true); frame.setSize(panel.getSize()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); questions = readFile1("plate_tectonics.dat"); answers = answerStore.tectonicAnswers; askQuestions(questions, answers); // here its isn't reading the file } else if(ac.equals("b2")) { final JPF1 appRef = new JPF1(); JFrame frame = new JFrame("CountDown"); MP panel = new MP(); panel.addCountDownListener(appRef); panel.setVisible(true); frame.setSize(panel.getSize()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); questions = readFile2("rivers.dat"); answers = answerStore.riverAnswers; askQuestions(questions, answers); } else if(ac.equals("b3")) { final JPF1 appRef = new JPF1(); JFrame frame = new JFrame("CountDown"); MP panel = new MP(); panel.addCountDownListener(appRef); panel.setVisible(true); frame.setSize(panel.getSize()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(panel); frame.setVisible(true); questions = readFile3("rocks.dat"); answers = answerStore.rockAnswers; askQuestions(questions, answers); } else if (ac.equals("b4")) { System.exit(0); } } public void stopAndShowResults() { timeForMore = false; // used for countdown. } private String[] readFile1(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile1))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } private String[] readFile2(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile2))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } private String[] readFile3(String path) { StringBuilder sb = new StringBuilder(); String separator = "\n"; String question; DataInputStream in = null; try { in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile3))); String desc; try { while (true) { desc = in.readUTF(); JOptionPane.showInputDialog(null, desc); } } catch (EOFException e) { } } catch(IOException e) { System.out.println( e.getMessage() ); System.exit(1); } return sb.toString().split("\\n"); } public void askQuestions(String[] questions, String[] answers) { int count = 0; int point = 0; for(int j = 0; j < questions.length; j++) { timeForMore = true; Random generator = new Random(); int randomIndex = generator.nextInt(questions.length); String input = JOptionPane.showInputDialog(null, questions[randomIndex]); if(answers[randomIndex].equalsIgnoreCase(input)) count++; // incrementing counter if entered answer is correct point++; if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); int percent = (count*100)/questions.length; JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + percent + " % "); } class AnswerStore { // storing answers of each quiz in arrays String[] tectonicAnswers = { "Hellenic", "destructive", "100km", "Italy", "Wegner", "Mariana", "Sicily", "created", "constructive", "Mediterranean", "ggg", "ggg", "ggg" }; 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", "", "", "" }; } }

And this is the code to write to a binary file. (This is only one of the files):

Code:
import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener.*; import java.util.*; import java.io.*; import javax.swing.*; import java.util.Calendar; import java.awt.image.*; import javax.imageio.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class PlateTectonics { static final String dataFile = "plate_tectonics.dat"; static final String[] plate_tectonics = { "When the continents were joined together they were called a) Hellenic b) Pangaea c) Mariana", " When continents move towards each other they are called a) Constructive b) Destructive c) Conservative", "At what depth does a subducted plate melt? a) 100km b) 300km c) 20km", "The Alps are found in a) Spain b) France c) Italy", "Who proposed the idea of Continental Drift a) Scientist Burges b) Scientist Wegner c) Scientist John", "Which is the deepest trench in the world? a) Hellenic b) Mariana c) Aegean", "The Volcanoe Etna is found in: a) Sicily b) Malta c) Cyprus", "At constructive plate margins crust is: a) Created b) Destroyed c) Conserved", "When the continents move away from each other they are called: a) Passive Margins b) Constructive Margins c) Destructive Margins", " Hellenic trench is found in: a) Indian Ocean b) Mediterranean c) Atlantic Ocean" }; public static void main(String[] args) throws IOException { DataOutputStream out = null; try { out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile))); for (int i = 0; i < plate_tectonics.length; i ++) { out.writeUTF(plate_tectonics[i]); } } finally { out.close(); } DataInputStream in = null; } }
Thanks again. :-)

Last edited by mcal : 01-20-2008 at 08:04 PM.
Reply With Quote