Write an application that creates a quiz, which contains at least 5 questions ab
ID: 3692047 • Letter: W
Question
Write an application that creates a quiz, which contains at least 5 questions about java question (Ex: who creat java ,what year java creat etc.). Each question should be a multiple choice question with at least four options. When a user answers a question correctly, display a congratulatory message. If a user responds to a question incorrectly, display an appropriate message as well as a correct answer. At the end of the quiz display the number of correct and incorrect answers and the percentage of correct answers. Save the file as Quiz.java. Also, I need to accomplish this with arrays. USE ARRAY DO THIS PROGRAM! Also The program should include more than one class.(Thx )
Explanation / Answer
//java code for for simple quiz
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 ch1;
JRadioButton ch2;
JRadioButton ch3;
JRadioButton ch4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
String[][] qp;
String[][] qc;
int qaid;
HashMap<Integer, String> map;
Quiz(){
initializedata();
setTitle("It's Quiz Program");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(430,350);
setLocation(300,100);
setResizable(false);
Container cont=getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
bg=new ButtonGroup();
ch1=new JRadioButton("Choice1",true);
ch2=new JRadioButton("Choice2",false);
ch3=new JRadioButton("Choice3",false);
ch4=new JRadioButton("Choice4",false);
bg.add(ch1);
bg.add(ch2);
bg.add(ch3);
bg.add(ch4);
lblmess=new JLabel("Choose a correct anwswer");
lblmess.setForeground(Color.BLUE);
lblmess.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(ch1);
panel.add(ch2);
panel.add(ch3);
panel.add(ch4);
panel.add(btnext);
cont.add(panel);
setVisible(true);
qaid=0;
readqa(qaid);
}
public void actionPerformed(ActionEvent e){
if(btnext.getText().equals("Next")){
if(qaid<4){
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(){
//qp stores pairs of question and its possible answers
qp=new String[5][5];
qp[0][0]="who created java?";
qp[0][1]="dennis ritch";
qp[0][2]="james gosling";
qp[0][3]="Edd jones";
qp[0][4]="adam smith";
qp[1][0]="which of the following year java program invented?";
qp[1][1]="1935";
qp[1][2]="1995";
qp[1][3]="1999";
qp[1][4]="1990";
qp[2][0]="How to read a character from the keyboard?";
qp[2][1]="char c=System.read()";
qp[2][2]="char c=System.in.read()";
qp[2][3]="char c=(char)System.read()";
qp[2][4]="char c=(char)System.in.read()";
qp[3][0]="Which of the following is a single-line comment?";
qp[3][1]="/...";
qp[3][2]="//...";
qp[3][3]="/*...";
qp[3][4]="/*...*/";
qp[4][0]="How to declare an integer variable x?";
qp[4][1]="int x";
qp[4][2]="x as Integer";
qp[4][3]="Int[] x";
qp[4][4]="No one is correct.";
//qc stores pairs of the question and correct answer
qc=new String[10][2];
qc[0][0]="who created java?";
qc[0][1]="james gosling";
qc[1][0]="which of the following year java program invented?";
qc[1][1]="1995";
qc[2][0]="How to read a character from the keyboard?";
qc[2][1]="char c=(char)System.in.read()";
qc[3][0]="Which of the following is a single-line comment?";
qc[3][1]="//...";
qc[4][0]="How to declare an integer variable x?";
qc[4][1]="int x";
//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 qidd){
lblmess.setText(" "+qp[qidd][0]);
ch1.setText(qp[qidd][1]);
ch2.setText(qp[qidd][2]);
ch3.setText(qp[qidd][3]);
ch4.setText(qp[qidd][4]);
ch1.setSelected(true);
}
public void reset(){
qaid=0;
map.clear();
readqa(qaid);
btnext.setText("Next");
}
public int calCorrectAnswer(){
int qnum=5;
int count=0;
for(int qid=0;qid<qnum;qid++)
if(qc[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=5;
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+". "+qc[i][0], x,y);
y+=30;
g.setFont(new Font("Arial",Font.PLAIN,12));
g.drawString(" Correct answer:"+qc[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;
}
}
//It Shows 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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.