Google Answers Logo
View Question
 
Q: Java applett problem ( Answered 5 out of 5 stars,   0 Comments )
Question  
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
PLEASE)!!!!!!!!!!!!!!

Example Code

___________________________________________________________________________________________

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

public class SimpleForm2 extends Applet implements ActionListener,
ItemListener
{
    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);
	field.setEditable(false);
	finalMarkField = new TextField("Your final mark is: ",10);
	finalMarkField.setEditable(false);
	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(question1);
	
	add(box1);
	add(box2);
	add(submitButton);
	add (finalMarkField);
	
    }

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

    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>


----------------------------_____________________________________________________________________________________________

Questions

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

1)


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

  Laurent Blanc 
  Ronaldo 
  Emmanuel Petit 


Answer……..Laurent Blanc

*********************************************************************************************

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

  Archery 
  Swimming 
  Beach Volleyball 

Answer………. Beach volleyball

*********************************************************************************************

3)

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

********************************************************************************************

4)

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

*******************************************************************************************


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

*****************************************************************************************

6)

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

  Football 
  Rugby Union 
  Rugby League 

Answer= Rugby League

*********************************************************************************************

7)

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

*********************************************************************************************

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

  Isle of Man TT 
  RAC Rally 
  British Formula 1 Grand Prix 


Answer= Rac rally

********************************************************************************************


9)

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

************************************************************************************

10)

General. With which sport do you associate Ballyregan Bob? 

  WWF SmackDown 
  Horse Racing 
  Greyhound Racing 

Answer= Greyhound Racing

******************************************************************************
Answer  
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
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 09 May 2003 10:14 PDT
Hi,
Can I just ask you if this applet worked when you ran it?

Thanks

Purplepit-ga

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!

--seizer

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

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
example:

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

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

Or, you could do:

if (score < questions.size() / 2)
answerArea.setBackground(Color.blue);

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

Hope this helps!

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

Comments  
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 answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


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