Here is my html code
And here is my java program
Code:
<html> <body> <applet code="OnlineQuiz.class" width="1500" height="2000"> <param name="count" value="8"> <param name="question0" value="Blue-ray is the standard in hi-defintion quality."> <param name="question1" value="The ps3 has a built in blue-ray player."> <param name="question2" value="The ps3 has 8 processors."> <param name="question3" value="You have to buy an wireless adaptor for your ps3."> <param name="question4" value="You can only play the ps3 on a sony tv."> <param name="question5" value="The ps3 does not overheat"> <param name="question6" value="The ps3 is being used for scientific research"> <param name="question7" value="You can play music on your ps3 while playing a game just like the xbox 360"> <param name="correctAnswer0" value="YES"> <param name="correctAnswer1" value="YES"> <param name="correctAnswer2" value="YES"> <param name="correctAnswer3" value="No"> <param name="correctAnswer4" value="No"> <param name="correctAnswer5" value="No"> <param name="correctAnswer6" value="Yes"> <param name="correctAnswer7" value="No"> <param name="feedback0" value="Blue-ray is the standard for high definition storing 5x as much as standard dvd"> <param name="feedback1" value="There is a built in blue-ray player"> <param name="feedback2" value="There are 8 processors giving it impressive processing power"> <param name="feedback3" value="Not true the ps3 comes with built in WIFI"> <param name="feedback4" value="No any tv with s-video or HD components"> <param name="feedback5" value="Unfortunately it does"> <param name="feedback6" value="YES believe it or not stanford univerisity is using their processing power for disease research"> <param name="feedback7" value="No this is one feature that sets the two consoles apart"> </applet> </body> </html>
Code:
// file: OnlineQuiz.java
import javax.swing.JOptionPane;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.FontMetrics;
public class OnlineQuiz extends Applet
{
int count; // number of quiz questions
String [] question; // question[i] holds (i+1)th question
String [] userAnswer; // userAnswer[i] holds user's answer
String [] correctAnswer;
String [] feedback; // to the (i+1)th question
public void init( )
{
initQuiz( );
giveQuiz( );
} // end init
private void initQuiz( )
// Initialize the quiz questions, correct answers and feedback from the
// applet parameters in HMTL.
{
// get the number of questions from the html 'count' parameter
count = Integer.parseInt( getParameter( "count" ) );
// create an array for questions
question = new String[count];
for ( int k=0; k<count; k++ ) // each parameter
{
// get data from the html parameters
question[k] = getParameter( "question"+k );
} // end for
correctAnswer = new String[count];
for ( int k=0; k<count; k++ ) // each parameter
{
// get data from the html parameters
correctAnswer[k] = getParameter( "correctAnswer"+k );
} // end for
feedback = new String[count];
for ( int k=0; k<count; k++ ) // each parameter
{
// get data from the html parameters
feedback[k] = getParameter( "feedback"+k );
} // end for
} // end initQuiz
private void giveQuiz( )
{
JOptionPane.showConfirmDialog(null,
"Welcome to my Quiz. Would you like to continue?", "choose one", JOptionPane.YES_NO_OPTION);
int response;
userAnswer = new String[count];
for ( int k=0; k<count; k++ )
{
response = JOptionPane.showConfirmDialog( null, question[k],
"Question "+(k+1), JOptionPane.YES_NO_OPTION );
switch ( response )
{
case JOptionPane.YES_OPTION:
userAnswer[k] = "YES";
break;
case JOptionPane.NO_OPTION:
userAnswer[k] = "NO";
break;
case JOptionPane.CLOSED_OPTION:
userAnswer[k] = "SKIP";
break;
} // end switch
}
} // end giveQuiz
public void paint( Graphics g )
{
super.paint( g );
g.setFont( new Font( "Courier New", Font.PLAIN, 12 ) );
// get the font metrics and height of the text
// note: getHeight = leading + ascent + descent
FontMetrics fm = g.getFontMetrics( );
int textHeight = fm.getHeight( );
// set base position for printing on y-axis
int yPos = 20;
// output title
String output;
g.drawString( "QUIZ RESULTS:", 10, yPos );
// output results
for ( int k=0; k<count; k++ )
{
yPos+=textHeight;
g.drawString( "Question "+(k+1), 10, yPos );
yPos+=textHeight;
g.drawString( question[k], 20, yPos );
yPos+=textHeight;
g.drawString( "You said " + userAnswer[k], 20, yPos );
if (correctAnswer[k]==userAnswer[k])
{
yPos+=textHeight;
g.drawString( "Correct!", 20, yPos );
}
else
{
yPos+=textHeight;
g.drawString( "Wrong!", 20, yPos );
yPos+=textHeight;
g.drawString( feedback[k], 20, yPos );
}
} // end for
} // end paint
} // end OnlineQuiz
Comment