0% found this document useful (0 votes)
103 views

Quiz

This document defines a Java program that implements a quiz with multiple choice questions. It contains classes for displaying questions and answer options, tracking the user's selections, and generating a report to show the correct answers and user's responses. The Quiz class initializes the question and answer data, displays each question and gets the user input. It then generates a Report showing the results once all questions are completed. This provides an interactive multiple choice quiz with feedback on the correct answers.

Uploaded by

Dexter Alcantara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Quiz

This document defines a Java program that implements a quiz with multiple choice questions. It contains classes for displaying questions and answer options, tracking the user's selections, and generating a report to show the correct answers and user's responses. The Quiz class initializes the question and answer data, displays each question and gets the user input. It then generates a Report showing the results once all questions are completed. This provides an interactive multiple choice quiz with feedback on the correct answers.

Uploaded by

Dexter Alcantara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.awt.

*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

class Quiz extends JFrame implements ActionListener{

JPanel panel;
JPanel panelresult;
JRadioButton choice1;
JRadioButton choice2;
JRadioButton choice3;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
String[][] qpa;
String[][] qca;
int qaid;
HashMap<Integer, String> map;

Quiz(){
initializedata();
setTitle("Quiz Program");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(430,290);
setLocation(300,100);
setResizable(false);
Container cont=getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);

bg = new ButtonGroup();
choice1 = new JRadioButton("Choice1",false);
choice2 = new JRadioButton("Choice2",false);
choice3 = new JRadioButton("Choice3",false);

bg.add(choice1);
bg.add(choice2);
bg.add(choice3);

lblmess = new JLabel("Choose a correct anwswer");


lblmess.setForeground(Color.BLUE);
blmess.setFont(new Font("Arial", Font.BOLD, 11));
btnext = new JButton("Next");
btnext.setForeground(Color.GREEN);
btnext.addActionListener(this);

panel = new JPanel();


panel.setBackground(Color.LIGHT_GRAY);
panel.setLocation(10,10);
panel.setSize(400,300);
panel.setLayout(new GridLayout(6,2));

panel.add(lblmess);
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);

panel.add(btnext);
cont.add(panel);
setVisible(true);
qaid=0;
readqa(qaid);
}

public void actionPerformed(ActionEvent e){

if(btnext.getText().equals("Next")){if(qaid<9){

map.put(qaid,getSelection());
qaid++;readqa(qaid);
}
else {
map.put(qaid,getSelection());
btnext.setText("Show answers");
}
}

else if(btnext.getText().equals("Show answers"))


new Report();
}

public void initializedata(){

//qpa for questions.


qpa=new String[10][5];

qpa[0][0]="Who created JAVA?";


qpa[0][1]="a. Mark Apple";
qpa[0][2]="b. James Gosling";
qpa[0][3]="c. Joseph Cripto";

qpa[1][0]="When JAVA developed?";


qpa[1][1]="a. 1995";
qpa[1][2]="b. 1994";
qpa[1][3]="c. 1993";

qpa[2][0]="How to read a character from the keyboard?";


qpa[2][1]="a. char c=System.read()";
qpa[2][2]="b. char c=System.in.read()";
qpa[2][3]="c. char c=(char)System.read()";

qpa[3][0]="Which one is a single-line comment?";


qpa[3][1]="a. /...";
qpa[3][2]="b. //...";
qpa[3][3]="c. /*...";

qpa[4][0]="How do you declare an integer variable x?";


qpa[4][1]="a. int x";
qpa[4][2]="b. x as Integer";
qpa[4][3]="c. Int[] x";
qpa[5][0]="How do you convert a string of number to a number?";
qpa[5][1]="a. int num=Integer.parseInt(str_num)";
qpa[5][2]="b. int num=str_num.toInteger()";
qpa[5][3]="c. int num=(int)str_num";

qpa[6][0]="What is the value of x? int x=3>>2";


qpa[6][1]="a. 1";
qpa[6][2]="b. 0";
qpa[6][3]="c. 3";

qpa[7][0]="How to do exit a loop?";


qpa[7][1]="a. Using exit";
qpa[7][2]="b. Using break";
qpa[7][3]="c. Using continue";

qpa[8][0]="What is the correct syntax for main method of a JAVA class?";


qpa[8][1]="a. public int main(String[] args)";
qpa[8][2]="b. public static void main(String[] args)";
qpa[8][3]="c. None of the above";

qpa[9][0]="Can we have two public classes in one JAVA file?";


qpa[9][1]="a. True";
qpa[9][2]="b. False";
qpa[9][3]="c. Maybe";

//qca questions with answers.


qca=new String[10][2];

qca[0][0]="Who created JAVA?";


qca[0][1]="b. James Gosling";

qca[1][0]="When JAVA developed?";


qca[1][1]="a. 1995";

qca[2][0]="How to read a character from the keyboard?";


qca[2][1]="char c=(char)System.in.read()";

qca[3][0]="Which one is a single-line comment?";


qca[3][1]="b. //...";

qca[4][0]="How do you declare an integer variable x?";


qca[4][1]="a. int x";

qca[5][0]="How do you convert a string of number to a number?";


qca[5][1]="a. int num=Integer.parseInt(str_num)";

qca[6][0]="What is the value of x? int x=3>>2";


qca[6][1]="b. 0";

qca[7][0]="How to do exit a loop?";


qca[7][1]="Using break";

qca[8][0]="What is the correct syntax for main method of a JAVA class?";


qca[8][1]="b. public static void main(String[] args)";
qca[9][0]="Can we have two public classes in one JAVA file?";
qca[9][1]="a. True";

//create a map object to store pairs of question and selected answer


map=new HashMap<Integer, String>();
}

public String getSelection(){


String selectedChoice=null;
Enumeration<AbstractButton> buttons=bg.getElements();
while(buttons.hasMoreElements()) {
JRadioButton temp=(JRadioButton)buttons.nextElement();
if(temp.isSelected()) {
selectedChoice=temp.getText();
}
}
return(selectedChoice);
}

public void readqa(int qid){


lblmess.setText(" "+qpa[qid][0]);

choice1.setText(qpa[qid][1]);
choice2.setText(qpa[qid][2]);
choice3.setText(qpa[qid][3]);
choice1.setSelected(true);
}

public void reset(){


qaid=0;
map.clear();
readqa(qaid);
btnext.setText("Next");
}

public int calCorrectAnswer(){


int qnum=10;
int count=0;
for(int qid=0;qid<qnum;qid++)
if(qca[qid][1].equals(map.get(qid))) count++;
return count;
}

public class Report extends JFrame{


Report(){
setTitle("Answers");
setSize(850,550);
setBackground(Color.WHITE);
addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){


dispose();
reset();
}
});
Draw d=new Draw();
add(d);
setVisible(true);}

class Draw extends Canvas{


public void paint(Graphics g){
int qnum=10;
int x=10;
int y=20;
for(int i=0;i<qnum;i++){

//print the 1st column


g.setFont(new Font("Arial",Font.BOLD,12));
g.drawString(i+1+". "+qca[i][0], x,y);
y+=30;
g.setFont(new Font("Arial",Font.PLAIN,12));
g.drawString(" Correct answer:"+qca[i][1], x,y);
y+=30;
g.drawString(" Your answer:"+map.get(i), x,y);
y+=30;

//print the 2nd column


if(y>400)
{
y=20;
x=450;
}
}

//Show number of correct answers


int numc=calCorrectAnswer();
g.setColor(Color.BLUE);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Number of correct answers:"+numc,300,500);
}
}
}
}

public class QuizProgram{

public static void main(String args[]){


new Quiz();

}
}

You might also like