找回密码
 立即注册
首页 业界区 业界 oop前三次作业总结

oop前三次作业总结

琴丁辰 2025-6-9 08:58:33
前言:

这是我第一次写oop的设计作业,其中不乏踩坑,乱设计,瞎设计,但三次题目都能很好的提高个人能力,这是我跨入面向对象的第一步,只有第一步走踏实了,后面的课中,才能走的更快更稳,更稳更远。

  • 写前三次作业的过程中,每个题目都要求合理正确设计各种类,要求实现类的封装性,合理设计类间关系,符合类的单一职责原则,熟悉正则表达式。
    第一次题目集较为简单,需要设计的类较少,即可完成题目。
    第二次题目集前三题仍然比较简单,第四题在第一次题目集最后一题的基础上进行迭代,增加类的数量,题目难度较第一次加大,总体来说,不算太难。
    第三次题目集共三题,前两题题目内容较少,稍花时间设计即可顺利完成,最后一题依旧在前俩次题目的基础上进行进一步迭代,难度中等。
设计与分析:


  • 对于第一次作业最后一题:
点击查看代码[code]class Question {    private int number;    private String content;    private String standardAnswer;    public Question(int number, String content, String standardAnswer) {        this.number = number;        this.content = content;        this.standardAnswer = standardAnswer;    }    public int getNumber() {        return number;    }    public String getContent() {        return content;    }    public String getStandardAnswer() {        return standardAnswer;    }    public boolean checkAnswer(String answer) {        return answer.equals(standardAnswer);    }}class Paper {    private List questions = new ArrayList();    public void addQuestion(Question question) {        questions.add(question);    }    public Question getQuestion_Number(int number) {        for (Question question : questions) {            if (question.getNumber() == number) {                return question;            }        }        return null;    }}class Answer {    private Paper paper;    private ArrayList answers = new ArrayList();    private ArrayList results = new ArrayList();    public Answer(Paper paper) {        this.paper = paper;    }    public void saveOne_Your_answer(String answer) {        answers.add(answer);    }    public void saveAnswers() {        for (int i = 0; i < answers.size(); i++) {            Question question = paper.getQuestion_Number(i + 1);            if (question != null) {                results.add(question.checkAnswer(answers.get(i)));            }        }    }    public void outputResults() {        for (int i = 0; i < answers.size(); i++) {            Question question = paper.getQuestion_Number(i + 1);            if (question != null) {                System.out.println(question.getContent() + "~" + answers.get(i));            }        }        int i=0;        for (boolean result : results) {            if(i!=0){                System.out.print(" " + result);            } else {                System.out.print(result);            }            i++;        }    }}public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int questionNumber = sc.nextInt();        sc.nextLine();        Paper paper = new Paper();        Answer answerSheet = new Answer(paper);        for(int i=0;i答卷卷子    public Student() {    }    public void setId(String id) {        this.id = id;    }    public void setName(String name) {        this.name = name;    }    public String getId() {        return id;    }    public String getName() {        return name;    }}class Output{    HashMap id_for_answer = new HashMap();    ArrayList student_order = new ArrayList();    ArrayList students = new ArrayList();    HashMap papers = new HashMap();    Question_Set question_set = new Question_Set();    ArrayList  un_question_set = new ArrayList();    public Output() {    }    public void save_paper_set(Integer paper_num,Paper paper){        papers.put(paper_num,paper);    }    public void save_student_answer(String id,Integer paper_num){        id_for_answer.put(id,paper_num);        student_order.add(id);    }    public void output_question_and_result(){        for(String student : student_order){//确定答题学生            int []score  = new int[105];            Arrays.fill(score, 0);            int count_answer_question = 0;            for(Student new_student : students){//寻找该学生                if(new_student.getId().equals(student)){                    int answer_paper_num = id_for_answer.get(student);//寻找所答试卷编号                    Paper paper = papers.get(answer_paper_num);                    if(paper!=null){                        for(Integer answer_num : paper.answer_order){//找所答题目    T                            for(Question question : paper.questions){//2    N                                if(question!=null) {                                    if(question.getQuestion_num()==0){                                        continue;                                    }//                                    System.out.println(question.getQuestion_num());                                    if (question.getQuestion_num() == answer_num) {                                        if (question.isIf_effective() && !question.getQuestion_content().equals("null_content")) {//题目有效                                            System.out.print(question.getQuestion_content() + "~");//输出题目内容                                            String your_answer = new_student.students_to_answersheet.get(student).answers.get(count_answer_question+1).getYour_answer();//你的答案                                            System.out.println(your_answer + "~" + question.judge_right_or_wrong(your_answer));//结果                                            if (question.judge_right_or_wrong(your_answer)) {//题目正确                                                score[count_answer_question] = paper.questions_score.get(question.getQuestion_num());                                            }                                        } else {//题目无效                                            if (answer_num > new_student.students_to_answersheet.get(student).getTotal_answer_number()) {                                                System.out.println("answer is null");                                            } else {//                                                int Num = new_student.students.get(student).answers.get(answer_num).getOrder_num();//                                            System.out.println(Num);//                                            System.out.print("*");System.out.println(answer_num);                                                if (question.getQuestion_content().equals("null_content")||question.getQuestion_standardAnswer().equals("null_answer")) {                                                    System.out.println("non-existent question~0");                                                } else {                                                    System.out.print("the question ");                                                    System.out.print(question.getQuestion_num());                                                    System.out.println(" invalid~0");                                                }                                            }                                        }                                        count_answer_question++;                                        break;                                    } else{                                        for(Integer a : un_question_set){                                            if(Objects.equals(a, answer_num)){                                                System.out.println("non-existent question~0");                                                count_answer_question++;                                                break;                                            }                                        }                                    }                                }                            }                        }                        System.out.print(new_student.getId() + " " + new_student.getName() + ":");                        int sum_score = 0;                        for(int i=0;i
您需要登录后才可以回帖 登录 | 立即注册