//Quiz.java
//First draft of quiz program.

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

public class Quiz extends Applet implements ActionListener{

    // Objects the whole class needs to know about
    private Label instructions, feedback1, feedback2;
    private TextField answer1, answer2, answer3, score, name;
    private int a,b;

   // output Quiz to screen
   public void init () {

       // Request for name in upper left
       add (new Label ("Name:     "));
       // Plain text title in upper right
       // Short answer field for student's name
       name = new TextField("", 30 );
       add( name );
       
       add(new Label("1. Under which menu on the menu bar    "));
       add(new Label("will you find the print option?        "));
       
       add(new Label("a) the edit menu, b) the style menu    "));
       add(new Label("c) the file menu, or d) the print menu "));
       answer1 = new TextField("", 40);
       add(answer1);

       add(new Label("2. Who is buried in Napoleon's tomb?      "));
       answer2 = new TextField("", 40);
       add(answer2);

       a = (int)(Math.random()*10+1);
       b = (int)(Math.random()*10+1);
       add(new Label("3. What is the product of "+a+" and "+b+"?    "));
       answer3 = new TextField("", 40);
       add(answer3);
       answer3.addActionListener( this );

       instructions = new Label("Hit enter in the third box when done.");
       add (instructions);

       add (new Label("Your score on the numerical portion is:"));
       score = new TextField("",40);
       score.setEditable(false);
       add(score);

       feedback1 = new Label("Your results on the short answer portion");
       add(feedback1);
       feedback2 = new Label("of the quiz will appear right here when you're done.");
       add(feedback2);

   }
    
    public void actionPerformed (ActionEvent e)
    {
	String ans1, ans2;
	int ans3, sum=0;
	
	// Determine the answers
	ans1 = answer1.getText().trim();
	ans2 = answer2.getText().trim();
	ans3 = Integer.parseInt( answer3.getText() );

	// Say something encouraging.
	instructions.setText("Thank you "+name.getText()+" for taking this quiz.");
	
	// Grade the numerical portion and report the grade.
	if (ans3 == a*b) sum++;
	score.setText( Integer.toString( sum ) );

	// Give feedback on the short answer and multiple choice part.
	feedback1.setText("1) Your answer: "+ans1+"; correct answer: c.");
	feedback2.setText("2) Your answer: "+ans2+"; correct answer: Napoleon.");
    }

}
