Java design examination system

Explain

The examination system here is composed of students, teachers and examination institutions. Students log in to the examination machine by user name, password, and the examination machine randomly extracts a certain number of questions from the question bank to form a test paper. Students answer the test paper. Finally, the teacher corrects the test paper and gives the results.

Running environment

IDEA
JDK 1.7.0_75

Catalog

Code

There are some annotations in the code, so no further explanation is needed. Writing code is not to write a whole class before the next class, but to write the basic functions of a class and then to the next class, while writing to test whether this function can be used, good use and then write the next function, so some classes may write in a somewhat difficult order. If you can't understand it, you can see it. Here
Here the problem is treated as a separate class

package ExamSystem;

public class Question {
    private String title;//stem
    private String answer;//right key

    public Question(String title,String answer){
        this.title=title;
        this.answer=answer;
    }

    public String getTitle() {
        return this.title;
    }
    public String getAnswer(){
        return this.answer;
    }

    //The equals method and hashCode method should be rewritten to ensure that the title does not repeat in the set set set.
    @Override
    public boolean equals(Object obj) {
        if(this==obj)
            return true;
        if(obj instanceof Question){
            Question anotherQuestion=(Question)obj;
            //Are the comparative questions the same?
            if(this.getTitle().equals(anotherQuestion.getTitle()))
                return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return this.getTitle().hashCode();
    }
}

Look at the exam machines again.

package ExamSystem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;

/*
Examination machine, which stores the question bank, in order to prevent the repetition of questions in the question bank, set set set storage is selected. Remember that Question class rewrites hashCode() and equals() method.
 */
public class ExamMachine {
    //Record student accounts and passwords
    private HashMap<String,String> userBank=new HashMap<String,String>();
    {
        userBank.put("Zhang San","111");
        userBank.put("Li Si","222");
        userBank.put("Wang Wu","333");
    }
    //Login method
    public boolean login(String userName,String passWord){
        String realPassWord=userBank.get(userName);
        if(realPassWord!=null && realPassWord.equals(passWord))
            return true;
        return false;
    }

    //Using Set Set Set as Question Base
    private HashSet<Question> questionBank=new HashSet<Question>();
    //Add the title to the question bank
    {
        questionBank.add(new Question("Which of the following is a legal identifier?\n\tA.#_pound\n\tB._underscore\n\tC.5interstate\n\tD.class","B"));
        questionBank.add(new Question("Which of the following words is java Keyword of Language?\n\tA.sizeof\n\tB.abstract\n\tC.null\n\tD.String","B"));
        questionBank.add(new Question("Java in main()What is the return value of a function??\n\tA.String\n\tB.int\n\tC.char\n\tD.void","D"));
        questionBank.add(new Question("Which of the following is main()Legitimate parameters of functions?\n\tA.char args[]\n\tB.char args[][]\n\tC.String args[]\n\tD.String args","C"));
        questionBank.add(new Question("Which of the following methods defines the execution of a thread?\n\tA.start()\n\tB.init()\n\tC.run()\n\tD.main()","C"));
        questionBank.add(new Question("Which of the following constraints can be used to define member constants?\n\tA.static\n\tB.final\n\tC.abstract \n\tD.No modifier can be used","B"));
        questionBank.add(new Question("provide Java What are the packages for accessing databases?\n\tA.java.sql\n\tB.java.awt\n\tC.java.lang\n\tD.java.swing","A"));
        questionBank.add(new Question("Object's characteristics are expressed as variables in a class, which is called class's?\n\tA.object\n\tB.attribute\n\tC.Method\n\tD.data type","B"));
        questionBank.add(new Question("Which of the following cannot stop the thread execution?\n\tA.sleep();\n\tB.stop();\n\tC.wait();\n\tD.notify();","D"));
        questionBank.add(new Question("Which of the following is not String The legal method provided by the class?\n\tA.equals(String)\n\tB.trim()\n\tC.append()\n\tD.indexOf()","C"));
    }
    //Random Generation of Test Papers--ArrayList Storage
    public ArrayList<Question> getPaper(int questionNum){
        //Define a set to store the questions in the test paper to prevent repetition of the questions
        HashSet<Question> paper=new HashSet<Question>();
        //Randomly select questions from the question bank as test papers. In order to make the question bank orderly, first put the question bank into a List set, and select the questions through the orderliness of the list.
        ArrayList<Question> questionBank=new ArrayList<Question>(this.questionBank);
        //Randomly generated numbers to select the corresponding questions in the question bank
        while(paper.size()<questionNum){
            int index=new Random().nextInt(this.questionBank.size());
            paper.add(questionBank.get(index));
        }
        return new ArrayList<Question>(paper);
    }

}

Student class

package ExamSystem;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

public class Student {
    private String userName;
    private String passWord;

    public String getUserName() {
        return userName;
    }
    public String getPassWord(){
        return passWord;
    }

    public Student(String userName,String passWord){
        this.userName=userName;
        this.passWord=passWord;
    }
    //The method of students'exams needs examination papers and answers are returned after completion.
    public String[] exam(ArrayList<Question> paper){
        Scanner input=new Scanner(System.in);
        String[] answers=new String[paper.size()];
        for(int i=0;i<paper.size();i++){
            Question question=paper.get(i);
            System.out.println((i+1)+"."+question.getTitle());
            System.out.println("Please enter your answer:");
            answers[i]=input.nextLine();
        }
        return answers;
    }
}

Teacher category

package ExamSystem;

import java.util.ArrayList;

/*
**The teacher is responsible for correcting the papers.
 */
public class Teacher {

    //Teachers need a test paper with answers to correct the test paper and give marks after correcting.
    public int checkPaper(ArrayList<Question> paper,String[] answers){
        System.out.println("Rewriting takes time. Please wait.");
        int score=0;
        for(int i=0;i<paper.size();i++){
            Question question=paper.get(i);
            if(question.getAnswer().equalsIgnoreCase(answers[i]))//Ignore capitalization of options when comparing
                score+=100/paper.size();
        }
        return score;
    }
}

Main class

package ExamSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class TestMain {

    public static void main(String[] args){
        int questionNum=5;//Number of questions on the paper
        ExamMachine machine=new ExamMachine();//Creating Examination Machine
        ArrayList paper=machine.getPaper(questionNum);//Random Generation of Test Papers
        Teacher teacher=new Teacher();
        Scanner input=new Scanner(System.in);
        System.out.println("Welcome to the examination system, please enter the user name:");
        String userName=input.nextLine();
        System.out.println("Please input a password:");
        String passWord=input.nextLine();
        if(machine.login(userName,passWord)){
            Student student=new Student(userName,passWord);
            System.out.println("Login successfully!"+student.getUserName()+",The exam will begin soon. Please answer the questions:");
            String[] answers=student.exam(paper);
            int score=teacher.checkPaper(paper,answers);
            System.out.println(student.getUserName()+"The results are as follows:"+score);
        }else{
            System.out.println("Error in username or password");
        }
    }
}

Operation result



Tags: Java JDK SQL Attribute

Posted on Sat, 05 Oct 2019 23:37:57 -0700 by rash