import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.Random;
public class Quiz extends JFrame implements ActionListener
{
public Font titleFont;
public Font buttonFont;
public Font normalFont;
public Image head;
public Button BA, BB,BC, BD, BNQ;
public int score = 0, questionsAsked = 0;
public Label scoreLabel;
public Label statusLabel;
public String scoreString;
public final int NUMQUESTIONS = 5; //this is a constant
public final int NUMLINES = 4; //also a constant
public String messageString[][]; //will be 6 msgs by 4 lines
public int currentQuestion = 0;
public int count = 0;
public char answer[];
public RandInt randNumGen; //random num generator
public void init()
{
setLayout(null);
setSize(640,550); //Size of screen
setBackground (Color.cyan);
titleFont = new Font ("SansSerif", Font.BOLD, 28);
buttonFont = new Font ("Dialog", Font.BOLD, 16);
normalFont = new Font ("SansSerif", Font.PLAIN, 16);
head = getImage(getCodeBase(), "head.gif");
BA = new Button("A"); //Button A
BA.setBounds(485,120,60,36);
BA.setFont(buttonFont);
BA.setBackground(Color.lightGray);
BA.addActionListener(this);
add(BA);
BB = new Button("B"); //Button B
BB.setBounds(485,180,60,36);
BB.setFont(buttonFont);
BB.setBackground(Color.lightGray);
BB.addActionListener(this);
add(BB);
BC = new Button("C"); //Button C
BC.setBounds(485,240,60,36);
BC.setFont(buttonFont);
BC.setBackground(Color.lightGray);
BC.addActionListener(this);
add(BC);
BD = new Button("D"); //Button D
BD.setBounds(485,300,60,36);
BD.setFont(buttonFont);
BD.setBackground(Color.lightGray);
BD.addActionListener(this);
add(BD);
BNQ = new Button("New Quiz"); //Button New Quiz
BNQ.setBounds(445,360,140,36);
BNQ.setFont(buttonFont);
BNQ.setBackground(Color.lightGray);
BNQ.addActionListener(this);
add(BNQ);
//initialise score and add to layout
scoreString = "Score: " + score + " / " + questionsAsked;
scoreLabel = new Label(scoreString);
scoreLabel.setBounds(20,240,240,28);
scoreLabel.setFont(normalFont);
add(scoreLabel);
statusLabel = new Label("");
statusLabel.setBounds(20,300,440,28);
statusLabel.setFont(normalFont);
add(statusLabel);
//initialise messages and answers
messageString = new String[NUMQUESTIONS+1][NUMLINES];
answer = new char[NUMQUESTIONS+1];
messageString[0][0] = "End of quiz";
messageString[0][1] = "Please click on the New";
messageString[0][2] = "Quiz button to restart";
messageString[0][3] = "";
answer[0] = 'Z';
messageString[1][0] = "Is a wombat (A) a quadruped Australian Mammal";
messageString[1][1] = "(B)a kind of flying mammal ";
messageString[1][2] = "(C)alien or (D)human";
messageString[1][3] = "";
answer[1] = 'A';
messageString[2][0] = "What is the cube root of 64?";
messageString[2][1] = "(A)8 (B)4";
messageString[2][2] = "(C)7 (D)12";
messageString[2][3] = "";
answer[2] = 'B';
messageString[3][0] = "Who made it to the South Pole First";
messageString[3][1] = "(A) Amundsen or (B) Scott?";
messageString[3][2] = "(C) Nelson or (D) Jonathan?";
messageString[3][3] = "";
answer[3] = 'A';
messageString[4][0] = "When did the first men walk on the moon?";
messageString[4][1] = "(A)1970 or (B)1969";
messageString[4][2] = "(C) 1965, or (D) 1980?";
messageString[4][3] = "";
answer[4] = 'B';
messageString[5][0] = "How many teeth does a human have?";
messageString[5][1] = "(A)42, or (B)50 ";
messageString[5][2] = "(C)100,or (D)45";
messageString[5][3] = "";
answer[5] = 'A';
//create random integer generator with numbers 1 - NUMQUESTIONS
randNumGen = new RandInt(NUMQUESTIONS);
//choose first question
currentQuestion = randNumGen.nextVal();
}
public void paint(Graphics g)
{
// display title
g.setColor(Color.red);
g.setFont(titleFont);
g.drawString("Quiz Applet", 20, 40);
g.drawImage(head, 280, 10, this);
//display the current question
g.setColor(Color.black);
g.setFont(normalFont);
for (int n = 0; n < NUMLINES; n++)
{
g.drawString(messageString[currentQuestion][n], 20, 120+n*26);
}
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals("New Quiz"))
{
score = questionsAsked = 0;
randNumGen.reSort();
currentQuestion = randNumGen.nextVal();
scoreString = "Score: " + score + " / " + questionsAsked;
scoreLabel.setText(scoreString);
repaint();
return;
}
else if (command.equals(Character.toString(answer[currentQuestion]))) {
score++;
questionsAsked++;
scoreString = "Score: " + score + " / " + questionsAsked;
scoreLabel.setText(scoreString);
statusLabel.setText(""); // or do statusLabel.setText("Correct!");
count = 0;
}
else {
count++;
if (count == 1) {
scoreString = "Score: " + score + " / " + questionsAsked;
scoreLabel.setText(scoreString);
statusLabel.setText("Wrong, try again.");
return;
}
else {
count = 0;
questionsAsked++;
scoreString = "Score: " + score + " / " + questionsAsked;
scoreLabel.setText(scoreString);
statusLabel.setText("Wrong again, correct answer to previous question is " + answer[currentQuestion]);
}
}
if (questionsAsked >= NUMQUESTIONS)
currentQuestion = 0;
else
currentQuestion = randNumGen.nextVal();
repaint();
}
}