Google Answers Logo
View Question
Q: Java applett problem ( Answered 5 out of 5 stars,   0 Comments )
Subject: Java applett problem
Category: Computers > Programming
Asked by: purplepit-ga
List Price: $50.00
Posted: 06 May 2003 16:44 PDT
Expires: 05 Jun 2003 16:44 PDT
Question ID: 200358
Hi there!!!

I need to develop an interactive multiple choice quiz about sport.
Once the quiz has been completed, it gives the user a mark on how
he/she performed. This should be time limited and when finished should
also display whether the given answers were wrong or correct. The user
will get 2 marks for each correct answer, and -1 for each wrong
answer. The quiz should consist of 10 questions. The background color
of the page should change according to the awarded mark.

Please see the example code, which should be followed in style!!! Also
this code needs to be written WITHOUT an editor ( HARDCODED

Example Code


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleForm2 extends Applet implements ActionListener,
    Label question1;
    Button submitButton;
    TextField field;
    TextField finalMarkField;
    Checkbox box1;
    Checkbox box2;
    int mark = 0;
    CheckboxGroup theRadioGroup = new CheckboxGroup();

    public void start() 
	field = new TextField("Question 1: The first letter of the alphabet
is ",30);
	finalMarkField = new TextField("Your final mark is: ",10);
	box1 = new Checkbox("A", theRadioGroup, false);
	box2 = new Checkbox("B", theRadioGroup, false);
	box1.addItemListener( this );
	box2.addItemListener( this );
	question1 = new Label("Question 1: The first letter of the alphabet
is ");
	submitButton = new Button("Submit Answers");
	submitButton.addActionListener( this );

	setLayout(new java.awt.GridLayout(0,3));
	add (finalMarkField);

    public void actionPerformed( ActionEvent evt )
	if ( evt.getSource( ) == submitButton )
	    finalMarkField.setText("Your final mark is: "+ mark);
	    if (mark<0) question1.setBackground( );

    public void itemStateChanged( ItemEvent evt )
	if ( evt.getItemSelectable( ) == box1)
	    mark = 2;
	else if ( evt.getItemSelectable( ) == box2)
	    mark = -1;
The Applet can be displayed using the following Tags: 
<APPLET CODE="SimpleForm2.class" WIDTH=900 HEIGHT=200>



PLEASE NOTE.. the selection 1,2,3 boxes are NOT required in the


Football. Who during the 1998 World Cup scored the first sudden death
"golden goal" to decide a match?

  Laurent Blanc 
  Emmanuel Petit 

Answer……..Laurent Blanc


Olympic Games. In which sport did Audrey Cooper and Amanada Glover
represent Great Britain at the Olympics in Sydney in 2000?

  Beach Volleyball 

Answer………. Beach volleyball



Football. Name the current Premiership manager who scored the winning
goal in the 1980 FA Cup Final.

  Peter Reid 
  Glenn Hoddle 
  Trevor Brooking 

Answer = Trevor Brooking



Football. Who has recently been named both PFA Player of the Year and
Footballer of the Year by the Football Writers' Association?

  Ruud van Nistelrooy 
  Thierry Henry 
  Paul Scholes 

Answer= Thrierry Henry


Rowing. Who was Steve Redgrave's partner when he won gold in the Coxed
Pairs at the 1988 Olympic Games in Seoul?

  Matthew Pinsent 
  Andy Holmes 
  Martin Cross 

Answer=Andy Holmes



General. In which sport did Lebanon and the Cook Islands play a World
Cup match in October 2000 that ended in a draw?

  Rugby Union 
  Rugby League 

Answer= Rugby League



Boxing. What name did the British middleweight Fidel Castro Smith
adopt in mid-career?

  Barney McGee 
  Bill Casey 
  Slugger O'Toole 

Answer= Slugger 0’Toole


Motor Sport. In 1999 the largest sporting attendance in Britain was
estimated at 2 million people. What was the event they all turned out

  Isle of Man TT 
  RAC Rally 
  British Formula 1 Grand Prix 

Answer= Rac rally



Cricket. Who, on the Queen Mother's 100th birthday in August 2000,
scored 100 in his 100th Test match?

  Michael Atherton 
  Alec Stewart 
  Brian Lara 

Answer= Alec Stewart



General. With which sport do you associate Ballyregan Bob? 

  WWF SmackDown 
  Horse Racing 
  Greyhound Racing 

Answer= Greyhound Racing

Subject: Re: Java applett problem
Answered By: seizer-ga on 06 May 2003 20:21 PDT
Rated:5 out of 5 stars
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

Although I have included it here also, I'd recommend getting it from
this link to preserve formatting.

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,


------------- 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));

   	    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));

	    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));

	    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
            q4.addAnswer(new Answer("Ruud Van Nistelrooy", false));
            q4.addAnswer(new Answer("Thierry Henry", true));
            q4.addAnswer(new Answer("Paul Scholes", false));

	    submitButton = new Button("Submit Answer"); 
		questionField = new TextField("", 100);
		answerArea = new TextArea(5, 50);
		setLayout(new java.awt.FlowLayout()); 
		CheckboxGroup theRadioGroup = new CheckboxGroup();

		// start the quiz!			    	    
    	public void doQuestion() 
			// pick current question from vector
	    	curQ = (Question)questions.elementAt(qNum);
 			// set text with it
 			// set answer checkboxes
	    	for (int i=0; i < curQ.answers.size(); i++) 
	 			Checkbox tmpC = new
theRadioGroup, false);
 			// add them to the screen
			for (int p=0; p < cboxes.size(); p++) {
				// and make them actually show up
    	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
						correct = true;
 			// score appropriately
 			if (correct)
 				score += 2;

		if (qNum >= questions.size()) 
		// we're finished
			// throw away old checkboxes now
			for (int l=0; l < cboxes.size(); l++) {
		} else
		// next question please
			// throw away old checkboxes now
			for (int l=0; l < cboxes.size(); l++) {
			cboxes = new Vector();
	public void doScore() 
		// hide the submit button
		// show score
		questionField.setText("Your score: " + Integer.toString(score));
		// some colours for good/bad result
		if (score<0) answerArea.setBackground(; 
		if (score > 5) answerArea.setBackground(;
		// 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) {
	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 09 May 2003 10:14 PDT
Can I just ask you if this applet worked when you ran it?



Clarification of Answer by seizer-ga on 10 May 2003 05:19 PDT
It certainly does work when I run it. If you're having problems, let
me know the nature of them, and I'll do my best to work out what's
going wrong.

Don't forget to compile it, and don't forget to include the HTML file
that you already mentioned:

<APPLET CODE="SimpleForm2.class" WIDTH=900 HEIGHT=200> 

Hope this helps!


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
  if (score<0) answerArea.setBackground(;  
  if (score > 5) answerArea.setBackground(;....................
Without having tried it,will this or anything need to be altered with
the TEN questions, as opposed to the FOUR!!!

Clarification of Answer by seizer-ga on 10 May 2003 13:42 PDT
Yes, the only code you should need to add is the questions.

To change the colour code, simply add lines as appropriate. For

if (score==3) answerArea.setBackground(;

Would make the answer area red if the score was exactly 3.

Or, you could do:

if (score < questions.size() / 2)

To set the background to blue if the score is less than half the
amount of questions.

Hope this helps!

purplepit-ga rated this answer:5 out of 5 stars
Excellent thanks!!!!!!

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy