Sunday, 4 March 2012

Online Objective type exam system using Java.

import java.util.Scanner;
import java.util.Random;
public class Main {
    /**
     * This class reference for object scanner is used to take input from user
     * and is used in several methods of class
     */
    static Scanner in=new Scanner(System.in);
    /**
     * This class reference for object Random is used to generate random numbers
     * to take random question set from the list of question sets at the beginning
     * of the quiz.
     */
    static Random rand=new Random();
    /**
     * There are several arrays (question sets) in which question objects are
     * stored. At the beginning of the quiz program chooses the random array
     * and start using question objects in it. This class field is for the number
     * which is used to identify each question set.
     */
    static int questionSetNumber;
    /**
     * This method is used to make a question set (array). It takes the question
     * set from the QuestionSets class. This class has a method which returns
     * the question set which is asked from it to return by a random number
     * (questionSetNumber)
     * @return the array which is filled with the question objects.
     */
    public static QuestionClass []makeQuestion()
    {
        QuestionClass []ques1=QuestionSets.questionSetChoice(questionSetNumber);
        return ques1;
    }
    /**
     * The main class in which the quiz is run.
     * @param args the command line arguments
     */
   
    public static void main(String[] args) {
        //This is the random number which will help to take random question set
        questionSetNumber=rand.nextInt(2);
        //This is used to store one of the question sets
       
        QuestionClass []ques1=makeQuestion();
        String answer;
        int marksGot;
        int []marksArray=QuestionSets.Marks();
        //quiz starts running from this loop and this is the loop which again
        //repeats the quiz if asked to do so
       
        while(true)
        {
            marksGot=0;
            for(int i=0; i<ques1.length; i++)
            {
                //Calls the method to prints the question i from the question set
                //and this same method asks user to give input
               
                answer=ques1[i].questionPrint();
                //Once the input is given it is sent to the following method
                //to check if answer is invalid. This method also does several
                //other tasks. (See its documentation)
                int ans=checkValidAnswer(answer, i);
                //Here option (answer) from 4 options of the question i from
                //the question set is checked if it equals the option 0 of the
                //question i. Option 0 contains the correct answer.
                if(ques1[i].option[ans].equals(ques1[i].option[0]))
                {
                    System.out.println();
                    marksGot=marksGot+marksArray[i];
                }
                else
                {
                    System.out.println("\nWrong answer!!");
                    System.out.println();
                    break;
                }
            }//end of question set loop
            //When the question set is completed, results are shown here and user
            //is asked if he/she wants to attempt again
           
            System.out.println("You Got " + marksGot +" Marks \n\nDo you want to attempt " +
                    "again? Press any button to continue and N to stop");
            answer=in.next();
            if(answer.equals("N") || answer.equals("n"))
            {
                System.out.println("Good Attempt!!!!");
                break;
            }
            //If user opts to attempt again, again a question set is randomly
            //selected from its list and life line is activated again
           
            questionSetNumber=rand.nextInt(2);
            ques1=makeQuestion();
            System.out.println();
           
           
        }//end of quiz loop
    }
   
    public static int checkValidAnswer(String answer, int questionSetNum)
    {
        //checks if answer is not valid
        while(!answer.equals("1") &&
        !answer.equals("2") &&
        !answer.equals("3") &&
        !answer.equals("4") &&
        !answer.equals("5") &&
        !answer.equals("F") &&
        !answer.equals("f"))
         {
            System.out.print("Invalid Choice! \nEnter your Answer here: ");
            answer=in.next();
         }//end of loop to check validity of answer
        
        return Integer.parseInt(answer);
    }
 
}



////////////////////////////////////////////////////////////////////////
public class QuestionSets {
public static QuestionClass [] questionSetChoice(int randomNum)
   {
       if(randomNum==0)
       {
           return questionSet1();
       }
       return questionSet1();
   }
public static QuestionClass [] questionSet1()
   {
       QuestionClass [] questionList=new QuestionClass[9];
       System.out.println("@@@ " + "PRESS 1, 2, 3, 4 OR 5 TO CHOOSE YOUR OPTION AND PRESS ENTER TO CHECK YOUR" + " ANSWER"+ " @@@");
       questionList[0]=new QuestionClass("Q1. A constructor","Must have the same name as the class it is declared within.","Is used to create objects.","May be declared private ","1 & 3","All of these","All of these");
       questionList[1]=new QuestionClass("Q2. What are the two parts of a value of type double?", "significant digits ","length ","exponent  ","1 & 3 ", "None Of these","1 & 3 ");
       questionList[2]=new QuestionClass("Q3. What does the following line of code mean? \n double table[]; ","table is a variable that refers to an array ","table is a variable to refers to a real number ","table is a variable that refers to two numbers ","It is not legal Java code ","table is a variable to refers to a complex number","table is a variable that refers to an array ");
       questionList[3]=new QuestionClass("Q4. The following is NOT an example of a data type.","int ","public ","button ","type ","float","public ");
       questionList[4]=new QuestionClass("Q5. The data type for numbers such as 3.14159 is: ","float ","String ","int ","real ","double ","double ");
       questionList[5]=new QuestionClass("Q6. SQL stands for? ","System Query language ","Structured Query language ","Systematic Query language ","Serial Query language ","None of the above ","Structured Query language ");
       questionList[6]=new QuestionClass("Q7. Database Management System is a?","Software ","Hardware ","Both are true ","Malware ","None of these","Software ");
       questionList[7]=new QuestionClass("Q8. In object oriented software design ‘inheritence’ is a kind of","optimization ","testing ","relationship ","instance ","module ","relationship ");
       questionList[8]=new QuestionClass("Q9. In relational database the column of relation is called: ","table ","relation ","entity ","attribute ","object","attribute ");
       return questionList;
   }

public static int []Marks()
   {
       int [] Point=new int[9];
       Point[0]=4;
       Point[1]=4;
       Point[2]=4;
       Point[3]=4;
       Point[4]=4;
       Point[6]=4;
       Point[7]=4;
       Point[8]=4;
       return Point;
   }
}
////////////////////////////////////////////////////////////////////////import java.util.Scanner;
public class QuestionClass {
String questionLine;
String []option=new String[6];
static Scanner in=new Scanner(System.in);
public QuestionClass(String quest, String opt1, String opt2,
            String opt3, String opt4, String opt5, String correct)
    {
        questionLine=quest;
        option[1]=opt1;
        option[2]=opt2;
        option[3]=opt3;
        option[4]=opt4;
        option[5]=opt5;
        option[0]=correct;
    }
public String questionPrint()
    {
       
        System.out.println(questionLine);
        System.out.println();
        System.out.println("1. "+ option[1]);
        System.out.println("2. "+ option[2]);
        System.out.println("3. "+ option[3]);
        System.out.println("4. "+ option[4]);
        System.out.println("5. "+ option[5]);
        System.out.println();
        String answer=in.next();
        return answer;
    }
}