OOP 1-3次作業總結

源颖發表於2024-04-20

23201636-曾繁榕
目錄

  1. 前言
  2. 設計與分析
  3. 踩坑心得
  4. 改進建議
  5. 總結
    前言
    本次作業知識點:類與物件的基本使用,透過介面實現類重寫(繼承),日期類的基本使用,還主要考察了正規表示式的運用,字串的提取分割;
    個人體會:本次作業難度適中,題量適中,但有些比較複雜的類設計上容易出現錯誤,且部分知識未學習的情況下需要我們短時間內自行掌握給解決題目造成困難;
    大題原始碼
    第一次作業
    答題判題程式-1
import java.util.Scanner;
class Answer{
    int num;
    String answer;
    String question;
    public Answer(int num,String answer,String question){
        this.num=num;
        this.answer=answer;
        this.question=question;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        String s;
        String[] str;
        String[] str1=new String[4];
        input.nextLine();
        Answer a[]=new Answer[num];
        for(int i=0;i<num;i++)
        {
            s=input.nextLine();
            str=s.split("#");
            str1[0]=str[1].replace("N:","");
            char c0='0';
            for(int j=0;j<str1[0].length()-1;j++)
            {
                char c=str1[0].charAt(j);
                if(c!=' ')
                {
                    c0=str1[0].charAt(j);
                    break;
                }
                else
                    continue;
            }
            int n=c0-'0';
            str1[1]=str[2].replace("Q:","");
            str1[2]=str1[1].trim();
            str1[3]=str[3].replace("A:","~");
            String ss=str[3].replace("A:","");
            String ss1=ss.trim();
            String ss2="A:";
            String ss3=ss2+ss1;
            a[i]=new Answer(n,ss3,str1[2]);
            // StringBuffer s1 = new StringBuffer();
            // for(int j=2;j<str.length;j++)
            // {
            //     s1.append(str1[j]);
            // }
            // System.out.println(s1);
        }
        String[] str2;
        s=input.nextLine();
        str2=s.split("#");
        for(int i=1;i<=str2.length-1;i++)
        {
            String s1=str2[i].replace("A:","");
            String s2=s1.trim();
            String s3="~"+s2;
            for(int j=0;j<str2.length-1;j++)
            {
                int n=a[j].num;
                if(n==i)
                    System.out.printf("%s%s\n",a[j].question,s3);
            }
        }
        for(int i=1;i<str2.length-1;i++)
        {
            String b0=str2[i].replace("A:","");
            String b="A:"+b0.trim();
            for(int j=0;j<str2.length-1;j++)
            {
                int n=a[j].num;
                if(n==i)
                {
                if(a[j].answer.equals(b))
                    System.out.printf("true ");
                else
                    System.out.printf("false ");
                }
            }
        }
        String b0=str2[str2.length-1].replace("A:","");
        String b="A:"+b0.trim();
        for(int i=0;i<num;i++)
        {
            if(a[i].num==num)
            {
                if(a[i].answer.equals(b))
                    System.out.printf("true");
                else
                    System.out.printf("false");
            }
        }
        if(num!=0)
            input.nextLine();
    }
}

第二次作業
答題判題程式-2

import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
class Question{
    String num;
    String question;
    String answer;
    Question(){
        
    }
    Question(String num,String question,String answer){
        this.num = num;
        this.question = question;
        this.answer = answer;
    }
    public String toString(){
        return question;
    }
}
class Test{
    String tnum;
    String qnum[];
    int score[];
    Test(String tnum){
        this.tnum=tnum;
    }
    public void add(String num,int s){
        if(qnum==null&&score==null)
        {
            String a[] = new String[1];
            int t[] = new int[1];
            a[0] = num;
            t[0] = s;
            qnum = a;
            score = t;
        }
        else{
            String a[] = new String[qnum.length+1];
            for(int i=0;i<qnum.length;i++)
            {
                a[i]=qnum[i];
            }
            a[qnum.length] = num;
            qnum = a;
            int t[] = new int[score.length+1];
            for(int i=0;i<score.length;i++)
            {
                t[i]=score[i];
            }
            t[score.length] = s;
            score = t;
        }
    }
}
class StuTest{
    String snum;
    String answer[];
    StuTest(String snum){
        this.snum = snum;
    }
    public void addanswer(String ans){
        if(answer==null)
        {
            String a[] = new String[1];
            a[0] = ans;
            answer = a;
        }
        else{
            String a[] = new String[answer.length+1];
            for(int i=0;i<answer.length;i++)
            {
                a[i]=answer[i];
            }
            a[answer.length] = ans;
            answer = a;
        }
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num1=0,num2=0,num3=0;
        Question q[] = new Question[10];
        Test t[] = new Test[5];
        StuTest stu[] = new StuTest[5];
        String s = sc.nextLine();
        String regex = "#N:\\s*(\\d*)\\s+#Q:(.*?)\\s+#A:(.*)";
        String regex1 = "#T:\\s*(\\d*)\\s+(\\d+-\\d+\\s*)+";
        String regex2 = "#S:\\s*(\\d*)\\s*(#A:\\s*.*\\s*)+";
        Pattern pattern = Pattern.compile(regex);
        while(true){
            if(s.equals("end"))
                break;
            if(Pattern.matches(regex,s))
            {
                Matcher m = pattern.matcher(s);
                if(m.find())
                {
                    q[num1] = new Question(m.group(1).trim(),m.group(2).trim(),m.group(3).trim());
                }
                // System.out.println(q[num1].toString());
                num1++;
            }
            else if(Pattern.matches(regex1,s))
            {
                String str[] = s.split(" ");
                String ss = str[0].replace("#T:","");
                // System.out.println(ss);
                t[num2] = new Test(ss);
                for(int i=1;i<str.length;i++)
                {
                    String str1[] = str[i].split("-");
                    int x = Integer.parseInt(str1[1]);
                    t[num2].add(str1[0],x);
                }
                num2++;
            }
            else if(Pattern.matches(regex2,s))
            {
                String str[] = s.split(" ");
                String ss = str[0].replace("#S:","");
                stu[num3] = new StuTest(ss);
                for(int i=1;i<str.length;i++)
                {
                    String sss = str[i].replace("#A:","");
                    stu[num3].addanswer(sss);
                }
                num3++;
            }
            s = sc.nextLine();
        }
        for(int i=0;i<num2;i++)
        {
            int sum = 0;
            for(int j=0;j<t[i].score.length;j++)
            {
                sum = sum + t[i].score[j];
            }
            if(sum!=100)
                System.out.println("alert: full score of test paper"+t[i].tnum+" is not 100 points");
        }
        for(int i=0;i<num3;i++)
        {
            int index = -1;
            int sum = 0;
            for(int j=0;j<num2;j++)
            {
                if(stu[i].snum.equals(t[j].tnum))
                    index = j;
            }
            if(index==-1)
            {
                System.out.println("The test paper number does not exist");
            }
            else{
            for(int j=0;j<stu[i].answer.length;j++)
            {
                for(int k=0;k<num1;k++)
                {
                    boolean b = false;
                    if(t[index].qnum[j].equals(q[k].num))
                    {
                        if(q[k].answer.equals(stu[i].answer[j]))
                            b = true;
                        System.out.println(q[k].question+"~"+stu[i].answer[j]+"~"+b);
                        if(b)
                        {
                            sum = sum + t[index].score[j];
                        }
                    }
                }
            }
            for(int j=0;j<t[index].qnum.length-stu[i].answer.length;j++)
            {
                System.out.println("answer is null");
            }
            for(int j=0;j<stu[i].answer.length;j++)
            {
                for(int k=0;k<num1;k++)
                {
                    boolean b = false;
                    if(t[index].qnum[j].equals(q[k].num))
                    {
                        if(q[k].answer.equals(stu[i].answer[j]))
                            b = true;
                        if(b)
                        {
                            if(j==t[index].qnum.length-1)
                                System.out.printf("%d",t[index].score[j]);
                            else
                                System.out.printf("%d ",t[index].score[j]);
                        }
                        else{
                            if(j==t[index].qnum.length-1)
                                System.out.printf("0");
                            else
                                System.out.printf("0 ");
                        }
                    }
                }
            }
                for(int j=0;j<t[index].qnum.length-stu[i].answer.length;j++)
                {
                    if(j==t[index].qnum.length-stu[i].answer.length-1)
                        System.out.printf("0");
                    else
                        System.out.printf("0 ");
                }
                System.out.printf("~%d\n",sum);
        }
        }
    }
}

第三次作業
答題判題程式-3

import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
class Question{
    String num;
    String question;
    String answer;
    boolean isDelete=false;
    Question(){
        
    }
    Question(String num,String question,String answer){
        this.num = num;
        this.question = question;
        this.answer = answer;
    }
    public String toString(){
        return question;
    }
}
class Test{
    String tnum;
    String qnum[];
    int score[];
    Test(String tnum){
        this.tnum=tnum;
    }
    public void add(String num,int s){
        if(qnum==null&&score==null)
        {
            String a[] = new String[1];
            int t[] = new int[1];
            a[0] = num;
            t[0] = s;
            qnum = a;
            score = t;
        }
        else{
            String a[] = new String[qnum.length+1];
            for(int i=0;i<qnum.length;i++)
            {
                a[i]=qnum[i];
            }
            a[qnum.length] = num;
            qnum = a;
            int t[] = new int[score.length+1];
            for(int i=0;i<score.length;i++)
            {
                t[i]=score[i];
            }
            t[score.length] = s;
            score = t;
        }
    }
}
class StuTest{
    String snum;
    String stuid;
    int anum[];
    String answer[];
    StuTest(String snum,String stuid){
        this.snum = snum;
        this.stuid = stuid;
    }
    public void addanswer(int anumber,String ans){
        if(anum==null&&answer==null)
        {
            int num[] = new int[1];
            String a[] = new String[1];
            num[0] = anumber;
            a[0] = ans;
            anum = num;
            answer = a;
        }
        else{
            int num[] = new int[anum.length+1];
            String a[] = new String[answer.length+1];
            for(int i=0;i<anum.length;i++)
                num[i] = anum[i];
            for(int i=0;i<answer.length;i++)
            {
                a[i]=answer[i];
            }
            num[anum.length] = anumber;
            a[answer.length] = ans;
            anum = num;
            answer = a;
        }
    }
}
class Student{
    String stuID;
    String sname;
    Student(String stuID,String sname){
        this.stuID = stuID;
        this.sname = sname;
    }
    public String toString(){
        return stuID+" "+sname+": ";
    }
}
class Delete{
    String dnum;
    Delete(String dnum){
        this.dnum = dnum;
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num1=0,num2=0,num3=0,num4=0,num5=0;
        Question q[] = new Question[10];
        Test t[] = new Test[5];
        StuTest stu[] = new StuTest[5];
        Student st[] = new Student[5];
        Delete d[] = new Delete[5];
        String s = sc.nextLine();
        String regex = "#N:\\s*(\\d*)\\s+#Q:(.*?)\\s+#A:(.*)";
        String regex1 = "#T:\\s*(\\d*)\\s*(\\d+-\\d+\\s*)*";
        String regex2 = "#S:\\s*(\\d*)\\s*(\\d*)\\s*(#A:(\\s*\\d+-.*\\s*)*\\s*)*";
        String regex3 = "#X:\\s*(\\d*)\\s*(.*?)";
        String regex4 = "#D:\\s*N-(\\d+\\s*)";
        Pattern pattern = Pattern.compile(regex);
        while(true){
            if(s.equals("end"))
                break;
            if(Pattern.matches(regex,s))
            {
                Matcher m = pattern.matcher(s);
                if(m.find())
                {
                    q[num1] = new Question(m.group(1).trim(),m.group(2).trim(),m.group(3).trim()+"");
                }
                // System.out.println(q[num1].toString());
                num1++;
            }
            else if(Pattern.matches(regex1,s))
            {
                String str[] = s.split(" ");
                String ss = str[0].replace("#T:","");
                // System.out.println(ss);
                t[num2] = new Test(ss);
                if(str.length>1){
                    for(int i=1;i<str.length;i++)
                    {
                        String str1[] = str[i].split("-");
                        int x = Integer.parseInt(str1[1]);
                        t[num2].add(str1[0],x);
                    }
                }
                num2++;
            }
            else if(Pattern.matches(regex2,s))
            {
                String str[] = s.split(" ");
                String ss = str[0].replace("#S:","");
                String ss1 = str[1];
                stu[num3] = new StuTest(ss,ss1);
                if(str.length>2){
                    String sstr[] = new String[str.length-2];
                    for(int i=0;i<sstr.length;i++)
                    {
                        sstr[i] = str[i+2];
                    }
                    Arrays.sort(sstr);
                    for(int i=0;i<sstr.length;i++)
                    {
                        String sss = sstr[i].replace("#A:","");
                        if(!sss.equals("")){
                        String str1[] = sss.split("-");
                        if(str1.length==1){
                            int x = Integer.parseInt(str1[0]);
                            stu[num3].addanswer(x,"");
                    }
                        else if(str1.length==2){
                            int x = Integer.parseInt(str1[0]);
                            stu[num3].addanswer(x,str1[1]);
                        }
                        }
                    }
                }
                num3++;
            }
            else if(Pattern.matches(regex3,s))
            {
                String str[] = s.replace("#X:","").split("-");
                for(int i=0;i<str.length;i++){
                    String str1[] = str[i].split(" ");
                    st[num4] = new Student(str1[0],str1[1]);
                    num4++;
                }
            }
            else if(Pattern.matches(regex4,s))
            {
                String str = s.replace("#D:N-","");
                d[num5] = new Delete(str);
                num5++;
            }
            else
                System.out.println("wrong format:"+s);
            s = sc.nextLine();
        }
        for(int i=0;i<num1;i++)
        {
            for(int j=0;j<num5;j++)
            {
                if(q[i].num.equals(d[j].dnum))
                    q[i].isDelete = true;
            }
        }
        for(int i=0;i<num2;i++)
        {
            int sum = 0;
            for(int j=0;j<t[i].score.length;j++)
            {
                sum = sum + t[i].score[j];
            }
            if(sum!=100)
                System.out.println("alert: full score of test paper"+t[i].tnum+" is not 100 points");
        }
        for(int i=0;i<num3;i++)
        {
            int index = -1;
            int sum = 0;
            for(int j=0;j<num2;j++)
            {
                if(stu[i].snum.equals(t[j].tnum))
                    index = j;
            }
            int l = 0;
            if(stu[i].answer!=null){
                l = stu[i].answer.length;
            }
            if(index==-1)
            {
                System.out.println("The test paper number does not exist");
            }
            else
            {
                int x = 0,y = 0;
                for(int j=0;j<l;j++)
                {
                    int z = stu[i].anum[j]-1;
                    if(j<t[index].score.length){
                    for(int k=0;k<num1;k++)
                    {
                        boolean b = false;
                        if(t[index].qnum[z].equals(q[k].num)&&!q[k].isDelete)
                        {
                            if(q[k].answer.equals(stu[i].answer[j]))
                            b = true;
                            System.out.println(q[k].question+"~"+stu[i].answer[j]+"~"+b);
                            if(b)
                            {
                                sum = sum + t[index].score[z];
                            }
                        }
                    }
                    }
                }
                for(int j=0;j<t[index].qnum.length;j++)
                {
                    boolean c = true;
                    for(int k=0;k<num1;k++)
                    {
                        if(q[k].num.equals(t[index].qnum[j]))
                            c = false;
                    }
                    if(c)
                    {
                        x++;
                    }
                }
                if(x!=0&&l>0)
                    System.out.println("non-existent question~0");
                for(int j=0;j<l;j++)
                {
                    if(j<t[index].score.length){
                    for(int k=0;k<num1;k++)
                    {
                        boolean b = false;
                        if(t[index].qnum[j].equals(q[k].num)&&q[k].isDelete)
                        {
                            System.out.println("the question "+q[k].num+" invalid~0");
                        }
                    }
                    }
                }
                for(int j=0;j<t[index].qnum.length-l;j++)
                {
                    System.out.println("answer is null");
                    y++;
                }
                boolean found = false;
                for(int j=0;j<num4;j++)
                {
                    if(stu[i].stuid.equals(st[j].stuID))
                    {
                        System.out.print(st[j].toString());
                        found = true;
                    }
                }
                if(found){
                for(int j=0;j<l;j++)
                {
                    int z = stu[i].anum[j]-1;
                    if(j<t[index].score.length){
                    for(int k=0;k<num1;k++)
                    {
                        boolean b = false;
                        if(t[index].qnum[z].equals(q[k].num))
                        {
                            if(q[k].answer.equals(stu[i].answer[j]))
                                b = true;
                            if(b&&!q[k].isDelete)
                            {
                                if(j==t[index].qnum.length-1){
                                    System.out.printf("%d",t[index].score[z]);
                                }
                                else{
                                    System.out.printf("%d ",t[index].score[z]);
                                }
                            }
                            else{
                                if(x==0&&j==t[index].qnum.length-1){
                                    System.out.printf("0");
                                    // y++;
                                }
                                else{
                                    System.out.printf("0 ");
                                    // y++;
                                }
                            }
                        }
                    }
                    }
                }
                    // System.out.println(x+" "+y+" "+l);
                for(int j=0;j<y;j++)
                {
                    if(x==0&&j==y-1)
                        System.out.printf("0");
                    else
                        System.out.printf("0 ");
                }
                    if(l>0){
                for(int j=0;j<x;j++)
                {
                    if(j==x-1)
                        System.out.printf("0");
                    else
                        System.out.printf("0 ");
                }
                    }
                // for(int j=0;j<t[index].qnum.length-l;j++)
                // {
                //     if(x==0&&j==t[index].qnum.length-l-1)
                //         System.out.printf("0");
                //     else
                //         System.out.printf("0 ");
                // }
                System.out.printf("~%d\n",sum);
        }
                else
                    System.out.printf(stu[i].stuid+" not found");
            }
        }
    }
}

設計與分析
第一次作業
答題判題程式-1

第一次作業中的類較少,結構較為簡單,類間關係簡單,主要為兩個類,題目類與主函式類;
第二次作業
答題判題程式-2

從類圖上不難看出本次作業多了試卷類與答卷類,分別存放試卷資訊以及對應試卷號的答卷資訊;對類間關係的設計也更加複雜,相互呼叫時更容易產生錯誤,主函式中的各種重複呼叫也明顯增多;
第三次作業
答題判題程式-3

本次作業新加入了學生類與刪除題目類,答卷類中也加入學生的學號,使得整體設計更加複雜,類間關係更難設計,各種迴圈呼叫使得程式碼易出錯,除錯時出現各種問題;

踩坑心得
第一次作業
答題判題程式-1
在第一次作業中,題目較為簡單易懂,主要問題是對字串的處理出現多種問題,如輸入時容易將首行末尾回車當作第一個題目導致第一個題目為空,還有對對字串的拆分處理容易出錯,以及第一次作業中各樣例對多餘空格的處理較為複雜,容易出錯;
第二次作業
答題判題程式-2
第二次作業出現的問題主要包括對正規表示式的不熟練導致對多種資訊的處理不到位,還有當資訊輸入亂序導致輸出錯誤,多張答卷多張試卷導致非零返回等問題;
第三次作業
答題判題程式-3
第三次作業的問題主要體現在對空答案,空試卷的處理上,以及各種迴圈呼叫導致除錯難度高,輸出結果錯誤,對類間關係呼叫不足導致主函式程式碼複雜,使除錯更上一個臺階,對空答案處理錯誤導致極易出現非零返回等情況;
改進建議

  1. 對類的使用不熟練,建立類的數量少,可以增加類的數量改進程式碼
  2. 類間關係掌握不夠,很少使用類內呼叫另一個類,可以透過類間關係降低程式碼複雜度
  3. 第一次作業中未使用正規表示式,後面兩次正規表示式的表達可以修改
  4. 可以將部分程式碼改成方法或類內方法從而降低程式碼複雜度
  5. 註釋過少,後續程式碼可透過新增註釋令程式碼簡單易懂
  6. 程式碼迴圈套用太過複雜,容易出現各種問題

總結
對於剛接觸物件導向程式設計且大部分內容需要在短時間內掌握來說,本次習題難度偏大,閱讀題目的題目量較大,需要花費大量時間讀懂題目,並需要自行掌握一些課程內尚未提及的知識點,更加清楚物件導向設計過程中對需求的把控與對物件導向設計的過程的進一步熟悉;
透過本次訓練,我們對類的使用,類間關係設計更加熟悉,掌握了繼承、Java日期類的基本使用,透過三次大題訓練讓我們熟練使用正規表示式與String類的各種方法將字串分割處理,增強了我們對物件導向設計的能力,我們需要在日後的學習過程中鞏固提高對所學知識的應用;
此外,我也發現了諸多程式碼習慣的問題,如不習慣對程式碼進行註釋,對類間關係的運用不夠熟練,程式碼複雜度過高,未將部分程式碼轉換為方法形式從而使Main函式看起來較為複雜,這些需要我在日後的學習生活中進行學習與提高;

相關文章