0

i`m making Quiz program which asking State and you answer capital. here is code

import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.io.*;
import java.util.Random;

public class Quiz extends Applet {

private String textfile = null;
private TextPanel text_p = null;
private Vector question = new Vector();
private Vector answer = new Vector();

public Quiz(String textfile) throws IOException{
this.textfile = textfile;
BufferedReader reader = new BufferedReader(new FileReader(textfile));
String line = null;
while((line = reader.readLine()) != null) {
StringTokenizer parser = new StringTokenizer(line, "/");
String quest = (String)parser.nextToken();
String ans = (String)parser.nextToken();
question.add(quest.trim());
answer.add(ans.trim());
}
reader.close();
}

public void init() {
text_p = new TextPanel(question, answer);
add("Center", text_p);
}

public static void main(String args[]) throws IOException {
Frame f = new Frame("Quiz");
Quiz text = new Quiz("textfile.txt");
text.init();

f.add("Center", text);
f.setSize(400, 200);
f.show();
}
}

class TextPanel extends Panel implements ActionListener {
private TextField text = new TextField(30);
private Label lb = new Label();
private Button bt1 = new Button("enter");
private Button bt2 = new Button("start");
private TextArea area = new TextArea(1,30);
private int quest_no = 0;
private Vector question = null;
private Vector answer = null;

public TextPanel(Vector question, Vector answer) {
this.question = question;
this.answer = answer;

setLayout(new BorderLayout());
add("North", lb);
add("West",text);
add("East", bt2);
add("South", area);
add(bt1);

setSize(500,200);

bt1.addActionListener(this);
bt2.addActionListener(this);
}

public void actionPerformed(ActionEvent ev) {
String label = ev.getActionCommand();
if(label.equals("enter")) {
if(((String)answer.elementAt(quest_no)).equals(text.getText().trim()))
area.setText("Correct Answer.");
else
area.setText("Wrong Answer.");


}
else if(label.equals("next")) {
quest_no++;
if(quest_no == question.size())
quest_no = 0;
lb.setText((String)question.elementAt(quest_no));
text.setText("");
area.setText("");
}
else if(label.equals("start")) {
quest_no = 0;
lb.setText((String)question.elementAt(quest_no));
text.setText("");
area.setText("");
bt2.setLabel("next");
bt2.validate();
}

}
}

in the text file (textfile.txt) has

What is capital of Delaware? / Dover
What is capital of Pennsylvania? / Harrisburg
What is capital of New Jersey? / Trenton
What is capital of Georgia? /Atlanta
What is capital of Connecticut? /Hartford

these information (it's all 50 states)

i`m having two problem right now.
1) Question dose not come out ramdom
2) After 10 ramdom question i also need to print our result.
(how many correct answer or wrong answer)

I try to use ramdom access file, it keep comes out with error.

if anybody good on JAVA please help me.

Thank you.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by quuba
0

1. to generate random index, use public int nextInt(int n) method from java.util.Random, where n is number of questions.
2. introduce int varibles counters for correct and wrong answer. Check quest_no, and print results if is equal 10.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.