Hi purplepit!
I have your code ready for you. It does the things you specify, and
should be fairly easy to modify if you wish to add features or
functionality.
Although I have included it here also, I'd recommend getting it from
this link to preserve formatting.
http://xult.org/purplepit/SimpleForm2.java
I've included the first four of your questions in the code, so you'll
have a chance to test it rapidly. The manner of adding more questions
should be obvious: just replace the number (q4, q5, etc).
I hope this answers your question! Please feel free to request
clarification before rating this answer, if anything's unclear.
All the best,
--seizer
------------- CODE FOLLOWS -----------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SimpleForm2 extends Applet implements ActionListener
{
// set up
Button submitButton;
TextField questionField;
CheckboxGroup theRadioGroup = new CheckboxGroup();
TextArea answerArea = null;
Question curQ = null;
int score;
int qNum = 0;
Vector questions = new Vector();
Vector cboxes = new Vector();
// start timing
long startTime = System.currentTimeMillis();
public void start()
{
// questions
Question q1 = new Question("Who during the 1998 World Cup scored
the first sudden death golden goal to decide a match?");
q1.addAnswer(new Answer("Laurent Blanc", true));
q1.addAnswer(new Answer("Ronaldo", false));
q1.addAnswer(new Answer("Emmanuel Petit", false));
questions.add(q1);
Question q2 = new Question("In which sport did Audrey Cooper
and Amanada Glover represent Great Britain at the Olympics in Sydney
in 2000?");
q2.addAnswer(new Answer("Archery", false));
q2.addAnswer(new Answer("Swimming", false));
q2.addAnswer(new Answer("Beach volleyball", true));
questions.add(q2);
Question q3 = new Question("Name the current Premiership manager
who scored the winning goal in the 1980 FA Cup Final");
q3.addAnswer(new Answer("Peter Reid", false));
q3.addAnswer(new Answer("Glenn Hoddle", false));
q3.addAnswer(new Answer("Trevor Brooking", true));
questions.add(q3);
Question q4 = new Question("Who has recently been named both PFA
Player of the Year and FOotballer of the Year by the Football Writer's
Association?");
q4.addAnswer(new Answer("Ruud Van Nistelrooy", false));
q4.addAnswer(new Answer("Thierry Henry", true));
q4.addAnswer(new Answer("Paul Scholes", false));
questions.add(q4);
submitButton = new Button("Submit Answer");
add(submitButton);
submitButton.addActionListener(this);
questionField = new TextField("", 100);
questionField.setEditable(false);
add(questionField);
answerArea = new TextArea(5, 50);
answerArea.setEditable(false);
add(answerArea);
setLayout(new java.awt.FlowLayout());
CheckboxGroup theRadioGroup = new CheckboxGroup();
// start the quiz!
doQuestion();
}
public void doQuestion()
{
// pick current question from vector
curQ = (Question)questions.elementAt(qNum);
// set text with it
questionField.setText(curQ.getQuestion());
// set answer checkboxes
for (int i=0; i < curQ.answers.size(); i++)
{
Checkbox tmpC = new
Checkbox(((Answer)curQ.answers.elementAt(i)).getAnswer(),
theRadioGroup, false);
cboxes.add(tmpC);
}
// add them to the screen
for (int p=0; p < cboxes.size(); p++) {
add((Checkbox)cboxes.elementAt(p));
// and make them actually show up
validate();
}
}
public void actionPerformed(ActionEvent evt)
{
if ( evt.getSource( ) == submitButton )
{
Checkbox selectedBox = theRadioGroup.getSelectedCheckbox();
String selectedAnswer = selectedBox.getLabel();
boolean correct=false;
// see if we have a good answer
for (int k=0; k < curQ.answers.size(); k++)
{
Answer tmp = (Answer)curQ.answers.elementAt(k);
if (tmp.getAnswer().equals(selectedAnswer) && tmp.isCorrect())
{
//store chosen answer
curQ.setChosen(k);
correct = true;
}
}
// score appropriately
if (correct)
score += 2;
else
score--;
}
qNum++;
if (qNum >= questions.size())
// we're finished
{
// throw away old checkboxes now
for (int l=0; l < cboxes.size(); l++) {
remove((Checkbox)cboxes.elementAt(l));
}
doScore();
} else
// next question please
{
// throw away old checkboxes now
for (int l=0; l < cboxes.size(); l++) {
remove((Checkbox)cboxes.elementAt(l));
}
cboxes = new Vector();
doQuestion();
}
}
public void doScore()
{
// hide the submit button
submitButton.setVisible(false);
// show score
questionField.setText("Your score: " + Integer.toString(score));
// some colours for good/bad result
if (score<0) answerArea.setBackground(Color.red);
if (score > 5) answerArea.setBackground(Color.green);
// give user results - question by question
for (int i=0; i < questions.size(); i++)
{
Question tmp = (Question)questions.elementAt(i);
answerArea.append(Integer.toString(i) + ": You picked " +
((Answer)tmp.answers.elementAt(tmp.getChosen())).getAnswer() + " which
was ");
if (((Answer)tmp.answers.elementAt(tmp.getChosen())).isCorrect())
{
answerArea.append("correct! (+2 points)\n");
} else
{
answerArea.append("wrong! (-1 point)\n");
}
}
// show them how long it took
long stopTime = System.currentTimeMillis();
answerArea.append("\nAnd it took you " + Long.toString((stopTime -
startTime)/1000) + " seconds to complete!\n");
}
}
class Question
{
String qtext;
int chosen = 0;
public Vector answers = new Vector();
Question(String q) {
qtext = q;
}
String getQuestion() {
return qtext;
}
void addAnswer(Answer a) {
answers.add(a);
}
void setChosen(int c) {
chosen = c;
}
int getChosen() {
return chosen;
}
}
class Answer
{
String answer;
boolean correct;
Answer(String a, boolean c) {
answer = a;
correct = c;
}
String getAnswer() {
return answer;
}
boolean isCorrect() {
return correct;
}
} |
Request for Answer Clarification by
purplepit-ga
on
10 May 2003 11:29 PDT
Hi my friend,
No I haven't tried it yet, as I cant do it at home!! I was just
wondering, if the only info I need to add are the questions
themselves, after your code for question 4!!!
Plus with this piece of the code....."// some colours for good/bad
result
if (score<0) answerArea.setBackground(Color.red);
if (score > 5) answerArea.setBackground(Color.green);....................
Without having tried it,will this or anything need to be altered with
the TEN questions, as opposed to the FOUR!!!
Thanks
Purplepit-ga
|