/*
7. Design an UI using swings to display 3/5 objective type questions along with 4 options. If the user
presses the submit button then the answers must be evaluated and result must be displayed along
with correct answers in different colors.
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class Program7 extends JFrame implements ActionListener{
public int numberofQuestion = 3;
JFrame frame;
JLabel question,labelResult;
JRadioButton option1, option2, option3, option4;
JButton save,submit;
String Questions[][] = new String[numberofQuestion][5];
String Answers[][] = new String[numberofQuestion][1];
String pa[][] = new String[numberofQuestion][1];
String Username;
ButtonGroup options;
public static int i=0;
public static int count = 0;
public static int timer = 15;
public static int ans_given = 0;
public int score = 0;
public Program7( ) {
this.Username = Username;
frame = new JFrame();
question = new JLabel();
option1 = new JRadioButton();
option2 = new JRadioButton();
option3 = new JRadioButton();
option4 = new JRadioButton();
save = new JButton("Save & Next");
submit = new JButton("Submit");
question.setBounds(10, 10, 200, 50);
option1.setBounds(10, 60, 200, 50);
option2.setBounds(10, 110, 200, 50);
option3.setBounds(10, 160, 200, 50);
option4.setBounds(10, 210, 200, 50);
save.setBounds(40, 260, 80, 40);
submit.setBounds(140, 260, 80, 40);
options = new ButtonGroup();
options.add(option1);
options.add(option2);
options.add(option3);
options.add(option4);
try {
Questions[0][0]="Which keyword is used for accessing the features of a package?";
Questions[0][1]="package";
Questions[0][2]="import";
Questions[0][3]="extends";
Questions[0][4]="export";
Answers[0][0]="import";
/// question 2
Questions[1][0]="Which of the following is not a Java features?";
Questions[1][1]="Dynamic";
Questions[1][2]="Architecture Neutral";
Questions[1][3]="Use of pointers";
Questions[1][4]="Object-oriented";
Answers[1][0]="Use of pointers" ;
/// question 3
Questions[2][0]=" _____ is used to find and fix bugs in the Java programs.";
Questions[2][1]="JVM";
Questions[2][2]="JRE";
Questions[2][3]="JDK";
Questions[2][4]="JDB";
Answers[2][0]="JDB" ;
} catch (Exception ex) {
System.out.println(ex);
}
save.addActionListener(this);
submit.addActionListener(this);
add(question);
add(option1);
add(option2);
add(option3);
add(option4);
save.setBounds(50,400,200,80);
submit.setBounds(270,400,200,80);
add(save);
add(submit);
setLayout(null);
setSize(600, 600);
setVisible(true);
setTitle("Program7 is Running");
start(0);
}
public void actionPerformed(ActionEvent ae){
repaint();
if(ae.getSource() == save){
option1.setEnabled(true);
option2.setEnabled(true);
option3.setEnabled(true);
option4.setEnabled(true);
ans_given = 1;
if(options.getSelection() == null){
pa[count][0] = "";
}else {
pa[count][0] = options.getSelection().getActionCommand();
}
if(count == (numberofQuestion-2)){
save.setEnabled(false);
submit.setEnabled(true);
}
count++;
start(count);
}else if(ae.getSource() == submit){
ans_given = 1;
if(options.getSelection() == null){
pa[count][0] = "";
}else {
pa[count][0] = options.getSelection().getActionCommand();
}
for(int ii =0 ; ii < pa.length ; ii++){
System.out.println(ii);
if(pa[ii][0].equals(Answers[ii][0])){
score+=1;
}else{
score+=0;
}
}
System.out.println(score);
question.setText("Score= "+score);
option1. setVisible(false);
option2. setVisible(false);
option3. setVisible(false);
option4. setVisible(false);
}
}
public void start(int count){
/*for(int i =0;i< 10;i++)
{
System.out.println("Question " + Questions[i][0]);
System.out.println("Option1 "+Questions[i][1]);
System.out.println("Option2 "+Questions[i][2]);
System.out.println("Option3 "+Questions[i][3]);
System.out.println("Option4 "+Questions[i][4]);
System.out.println("Answer "+Answers[i][0]);
}*/
//qno.setText("" + (count + 1) + ". ");
question.setText(Questions[count][0]);
option1.setText(Questions[count][1]);
option1.setActionCommand(Questions[count][1]);
option2.setText(Questions[count][2]);
option2.setActionCommand(Questions[count][2]);
option3.setText(Questions[count][3]);
option3.setActionCommand(Questions[count][3]);
option4.setText(Questions[count][4]);
option4.setActionCommand(Questions[count][4]);
options.clearSelection();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Program7();
}
});
}
}