Saturday 24 December 2016

MY CODE TO PERFORM QUIZ TYPE FORMAT CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
class  Quiz extends JFrame implements ActionListener{
            JPanel pan;
            JPanel panres;
            JRadioButton ch1;
            JRadioButton ch2;
            JRadioButton ch3;
            JRadioButton ch4;
            ButtonGroup bg;
            JLabel lb;
            JButton jbt;
            String[][] qa;
            String[][] qb;
            int q;
            HashMap<Integer, String> map;
           
            Quiz(){
                      initializedata();
                      setTitle("Quiz Program");
                      setDefaultCloseOperation(EXIT_ON_CLOSE);
                      setSize(850,550);
                      setLocation(300,100);
                      setResizable(false);
                      Container cont=getContentPane();
                      cont.setLayout(null);          
                      cont.setBackground(Color.GREEN);
                    bg=new ButtonGroup();    
                    ch1=new JRadioButton("Ch1");
                    ch2=new JRadioButton("Ch2");
                    ch3=new JRadioButton("Ch3");
                    ch4=new JRadioButton("Ch4");
                    bg.add(ch1);
                    bg.add(ch2);
                    bg.add(ch3);
                    bg.add(ch4);
                    lb=new JLabel("CHOOSE YOUR ANSWER");
                    lb.setForeground(Color.BLUE);
                    lb.setFont(new Font("Times New Roman", Font.BOLD, 12));
                    jbt=new JButton("Next");
                    jbt.setForeground(Color.GREEN);                
                    jbt.addActionListener(this);
                    pan=new JPanel();
                    pan.setBackground(Color.LIGHT_GRAY);
                    pan.setLocation(10,10);
                    pan.setSize(800,500);
                    pan.setLayout(new GridLayout(6,2));
                    pan.add(lb);
                    pan.add(ch1);
                    pan.add(ch2);
                    pan.add(ch3);
                    pan.add(ch4);
                    pan.add(jbt);
                    cont.add(pan);
                    setVisible(true);
                    q=0;
                    readqa(q);
                     }
           public void actionPerformed(ActionEvent e){
           
                        if(jbt.getText().equals("Next")){
                                    if(q<9){
                                               
                                                map.put(q,getSelection());
                                                q++;
                                                readqa(q);
                                                }
                                    else {
                                                map.put(q,getSelection());
                                                jbt.setText("Show answers");
                                               
                                             }
                                    }
                        else if(jbt.getText().equals("Show answers"))
                                    new Report();
                       
                       
            }
 public void initializedata(){
                        //qa stores pairs of question and its possible answers
                        qa=new String[20][5];

                        qa[0][0]="How to run Java program on the command prompt?";
                        qa[0][1]="javac JavaProgram";
                        qa[0][2]="java JavaProgram";
                        qa[0][3]="javac JavaProgram.java";
                        qa[0][4]="No one";

                        qa[1][0]="What is the use of the println method?";
                        qa[1][1]="It is used to print text on the screen.";
                        qa[1][2]="It is used to print text on the screen with the line break.";
                        qa[1][3]="It is used to read text from keyboard.";
                        qa[1][4]="It is used to read text from a file.";
                       
                        qa[2][0]="How to read a character from the keyboard?";
                        qa[2][1]="char c=System.read()";
                        qa[2][2]="char c=System.in.read()";
                        qa[2][3]="char c=(char)System.read()";
                        qa[2][4]="char c=(char)System.in.read()";

                        qa[3][0]="Which one is a single-line comment?";
                        qa[3][1]="/...";
                        qa[3][2]="//...";
                        qa[3][3]="/*...";
                        qa[3][4]="/*...*/";

                        qa[4][0]="How do you declare an integer variable x?";
                        qa[4][1]="int x";
                        qa[4][2]="x as Integer";
                        qa[4][3]="Int[] x";
                        qa[4][4]="No one is correct.";

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

                        qa[6][0]="What is the value of x? int x=3>>2";
                        qa[6][1]="1";
                        qa[6][2]="0";
                        qa[6][3]="3";
                        qa[6][4]="-3";

                        qa[7][0]="How to do exit a loop?";
                        qa[7][1]="Using exit";
                        qa[7][2]="Using break";
                        qa[7][3]="Using continue";
                        qa[7][4]="Using terminate";

                        qa[8][0]="What is the correct way to allocate one-dimensional array?";
                        qa[8][1]="int[size] arr=new int[]";
                        qa[8][2]="int arr[size]=new int[]";
                        qa[8][3]="int[size] arr=new int[size]";
                        qa[8][4]="int[] arr=new int[size]";

                        qa[9][0]="What is the correct way to allocate two-dimensional array?";
                        qa[9][1]="int[size][] arr=new int[][]";
                        qa[9][2]="int arr=new int[rows][cols]";
                        qa[9][3]="int arr[rows][]=new int[rows][cols]";
                        qa[9][4]="int[][] arr=new int[rows][cols]";


                        //qb stores pairs of question and its correct answer
                        qb=new String[20][2];
                       
                        qb[0][0]="How to run Java program on the command prompt?";
                        qb[0][1]="java JavaProgram";

                        qb[1][0]="What is the use of the println method?";
                        qb[1][1]="It is used to print text on the screen with the line break.";

                        qb[2][0]="How to read a character from the keyboard?";
                        qb[2][1]="char c=(char)System.in.read()";

                        qb[3][0]="Which one is a single-line comment?";
                        qb[3][1]="//...";


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

                        qb[5][0]="How do you convert a string of number to a number?";
                        qb[5][1]="int num=Integer.parseInt(str_num)";

                        qb[6][0]="What is the value of x? int x=3>>2";
                        qb[6][1]="0";
                       
                        qb[7][0]="How to do exit a loop?";
                        qb[7][1]="Using break";
                       
                        qb[8][0]="What is the correct way to allocate one-dimensional array?";
                        qb[8][1]="int[] arr=new int[size]";

                        qb[9][0]="What is the correct way to allocate two-dimensional array?";
                        qb[9][1]="int[][] arr=new int[rows][cols]";
                       
                       
                        //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 q){
                        lb.setText("  "+qa[q][0]);
                        ch1.setText(qa[q][1]);
                        ch2.setText(qa[q][2]);
                        ch3.setText(qa[q][3]);
                        ch4.setText(qa[q][4]);
                        //c1.setSelected(true);
            }
            public void reset(){
                        q=0;
                        map.clear();
                        readqa(q);
                        jbt.setText("Next");
                        }
            public int calCorrectAnswer(){
                        int qnum=20;
                        int count=0;
                        for(int q=0;q<qnum;q++)
                                    if(qb[q][1].equals(map.get(q)))
                                       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+". "+qb[i][0], x,y);
                                                            y+=30;          
                                                            g.setFont(new Font("Arial",Font.PLAIN,12));                            
                                                            g.drawString("      Correct answer:"+qb[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();
 
            }


}

FAQs of BCG

These are the some FAQS of BCG PLEASE USE BELOW LINK Details and FAQS of BCG