(1)前言
题目集6只有一个课程成绩统计程序-1,难点总体来说中等,考察的也是对java类的定义和使用,以及如何设计能使程序往后修改方便,可以根据给出的类图来进行设计。
题目集7中有上一次程序的进阶版课程成绩统计程序-2,相比于之前添加了实验这一课程性质,总的来说改变不大,只需要在原来的基础上进行一些修改即可,另外还有几道题目涉及了hashmap的用法,需要对其进行学习使用,对于java知识有更深一步的了解。
题目集8中的课程成绩统计程序同样承接课程成绩统计程序-2,有一些改变,也只需要在原来的代码中进行修改,总体难度不大,但是对于一些测试点还是存在问题,难以发现问题所在,其它几题就是考察了java的基本知识,以及面向对象基础和进阶,难度不大,需要自行去查找资料学习。
(2)设计与分析
首先分析其他简单的编程题如第7次中的hashmap容器,与arraylist不同的是,这个容器实际上是一个映射关系,每个对象都有一个对应的标记,可以通过那个标记直接找到对象,我们要做的就是将数据存入容器,以及学习对容器的增删查改的操作,
public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); HashMap<String,Student> hashMap = new HashMap<>(); String str=sc.nextLine(); while(!str.equals("end")) { String[] data=str.split(" "); Student student=new Student(data[0],data[1],Integer.parseInt(data[2])); hashMap.put(data[0],student); str=sc.nextLine(); } List<HashMap.Entry<String, Student>> list = new ArrayList<>(hashMap.entrySet()); Collections.sort(list, new Comparator<HashMap.Entry<String, Student>>() { @Override public int compare(HashMap.Entry<String, Student> o1, HashMap.Entry<String, Student> o2) { return o2.getKey().compareTo(o1.getKey()); } }); for (Map.Entry<String, Student> entry : list) { String num = entry.getKey(); String name = entry.getValue().getName(); int score = entry.getValue().getScores(); System.out.println(num + " " + name + " " + score); } } }
以上即为题目的代码,其中用到了hashmap容器以及对该容器进行排序并且遍历输出。
第八次题目集则是出了对java面向对象进阶的题目,如自定义接口来用于存放栈,这里就是需要去学习一下栈的定义(出栈入栈),才能知道该如何编写程序,代码如下
import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); IntegerStack stack = new ArrayIntegerStack(n); int m = scanner.nextInt(); for (int i = 0; i < m; i++) { int item = scanner.nextInt(); Integer result = stack.push(item); System.out.println(result != null ? result : "null"); } System.out.print(stack.peek()+","); System.out.println(stack.empty() + "," + stack.size()); System.out.println(Arrays.toString(((ArrayIntegerStack) stack).data)); int x = scanner.nextInt(); for (int i = 0; i < x; i++) { Integer result = stack.pop(); System.out.println(result != null ? result : "null"); } System.out.print(stack.peek()+","); System.out.println(stack.empty() + "," + stack.size()); System.out.println(Arrays.toString(((ArrayIntegerStack) stack).data)); } } interface IntegerStack { Integer push(Integer item); Integer pop(); Integer peek(); boolean empty(); int size(); } class ArrayIntegerStack implements IntegerStack { int top; Integer[] data; public ArrayIntegerStack(int capacity) { top = -1; data = new Integer[capacity]; } @Override public Integer push(Integer item) { if (item == null || top + 1 >= data.length) { return null; // 如果元素为 null 或栈满了,返回 null } data[++top] = item; return item; } @Override public Integer pop() { if (empty()) { return null; // 如果栈为空,返回 null } return data[top--]; } @Override public Integer peek() { if (empty()) { return null; // 如果栈为空,返回 null } return data[top]; } @Override public boolean empty() { return top == -1; } @Override public int size() { return top + 1; } @Override public String toString() { return Arrays.toString(data); } }
还有第8次的覆盖,这里考察的是面向对象的基础,比如类里的构造方法,代码如下:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n1 = scanner.nextInt(); List<PersonOverride> persons1 = new ArrayList<>(); for (int i = 0; i < n1; i++) { persons1.add(new PersonOverride()); } int n2 = scanner.nextInt(); scanner.nextLine(); List<PersonOverride> persons2 = new ArrayList<>(); for (int i = 0; i < n2; i++) { String[] inputs = scanner.nextLine().split(" "); String name = inputs[0]; int age = Integer.parseInt(inputs[1]); boolean gender = Boolean.parseBoolean(inputs[2]); PersonOverride person = new PersonOverride(name, age, gender); if (!persons2.contains(person)) { persons2.add(person); } } for (PersonOverride person : persons1) { System.out.println(person.toString()); } for (PersonOverride person : persons2) { System.out.println(person.toString()); } System.out.println(persons2.size()); System.out.println(Arrays.toString(PersonOverride.class.getConstructors())); } } class PersonOverride { private String name; private int age; private boolean gender; public PersonOverride() { this("default", 1, true); } public PersonOverride(String name, int age, boolean gender) { this.name = name; this.age = age; this.gender = gender; } public String toString() { return name + "-" + age + "-" + gender; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } PersonOverride other = (PersonOverride) obj; return Objects.equals(name, other.name) && age == other.age && gender == other.gender; } public String getName() { return name; } public int getAge() { return age; } public boolean getGender() { return gender; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setGender(boolean gender) { this.gender = gender; } }
接下来就是对课程成绩统计程序进行分析,首先是课程程序统计-1:
根据题目描述以及给出的类图首先可以知道,输入的格式分别为课程,课程成绩两类信息,所以要根据他们的不同来判断输入的为哪种信息,首先是用输入的数据长度以及某些字符来判断的,然后看到类图,我首先设计了抽象成绩类Grade 以及它的子类,考试成绩ExamGrade和考察成绩InvestigateGrade类,还有班级类class 还有里面的学生student类,和课程类course,因为这里既要统计各个科目的成绩,又要统计每个同学的成绩并且打印,于是我还设计了subject成绩类,用来记录学生的个人课程成绩,而course类里面则是该课程所有成绩,这样设计则方便最后统计成绩进行打印。最后则是设计了一个record类来记录输入的数据,将这些数据分别创建相应的对象来进行存入,最后在record里进行操作得到最后结果。
首先先放各个类代码:
Grade类:
abstract class Grade{ int usualGrade; int finalGrade; public Grade(){ } public Grade(int parseInt, int parseInt1) { usualGrade=parseInt; finalGrade=parseInt1; } abstract int getGrade(); }
这里就是包括了一些成绩属性和获得成绩的抽象方法。
examgrade和investigategrade类:
class ExamGrade extends Grade{ public ExamGrade(){ } public ExamGrade(int parseInt, int parseInt1) { super(parseInt,parseInt1); } int getGrade(){ return (int) (usualGrade * 0.3 + finalGrade * 0.7); } } class InvestigateGrade extends Grade{ public InvestigateGrade(){ } public InvestigateGrade(int parseInt, int parseInt1) { super(parseInt, parseInt1); } int getGrade(){ return finalGrade; } }
这里分别是对不同的成绩计算方式进行获得成绩的方法重写。
subject类:
class Subject{ String SubjectName; Grade grade; }
这里单纯的就是用在学生对象里面存入该科个人成绩的类;
student类
class Student{ String classNum; String name; Subject[] subject=new Subject[100]; int y=0; int getTotalGrade() { int totalGrade=0; for(int i=0;i<y;i++) { totalGrade+=subject[i].grade.getGrade(); } if(y!=0) return totalGrade/y; return -1; } void addSubject(String SubjectName,Grade grade) { subject[y]=new Subject(); subject[y].SubjectName = SubjectName; subject[y].grade = grade; y++; } Subject findSubject(String SubjectName) { for(int i=0;i<y;i++) { if(subject[i].SubjectName.equals(SubjectName)) { return subject[i]; } } return null; } }
这里就是学生类里面有班级,名字,以及各科成绩的属性,还有计算总平均分,添加个人学科成绩,查找学科的方法。
class类:
class Class{ String num; Student[] student=new Student[100]; int x=0; int getTotalAverageGrade() { int grade=0; for(int i=1;i<=x;i++) { grade+=student[i].getTotalGrade(); } return grade/x; } Student findAStudent(String name) { for(int i=1;i<=x;i++) { if(student[i].name.equals(name)) { return student[i]; } } return null; } boolean addAStudent(String classNum, String name, String SubjectName,Grade grade) { if(findAStudent(name)==null) { x++; student[x] = new Student(); student[x].classNum = classNum; student[x].name = name; if(student[x].findSubject(SubjectName)==null) { student[x].addSubject(SubjectName, grade); return false; } } else if(findAStudent(name)!=null) { if(student[x].findSubject(SubjectName)==null) { findAStudent(name).addSubject(SubjectName, grade); return false; } } return true; } void addAIllegalStudent(String classNum,String name) { if(findAStudent(name)==null) { x++; student[x] = new Student(); student[x].classNum = classNum; student[x].name = name; } } }
这里则是在班级类里面定义学生属性,以及班级号,然后有添加学生,查找学生,计算总的班级平均成绩的方法,另外由于题目给出的样例要求,如果学生输入时有问题,成绩无法录入但是这个学生对象依然可以录入所以我加了一个添加非法录入学生的方法。
course类:
class Course{ String CourseName; String type; String AssessmentMethod; int usualScore=0; int finalScore=0; int studentNum=0; void addScore(ExamGrade examGrade) { finalScore+=examGrade.finalGrade; usualScore+=examGrade.usualGrade; studentNum++; } void addScore(InvestigateGrade investigateGrade) { finalScore+=investigateGrade.finalGrade; studentNum++; } int getAverageUsualScore() { return (int)(usualScore/studentNum); } int getAverageFinalScore() { return (int)(finalScore/studentNum); } int getAverageScore() { if(type.equals("必修")) return (int)(getAverageUsualScore()*0.3+getAverageFinalScore()*0.7); else if(type.equals("选修")&&AssessmentMethod.equals("考察")) return getAverageFinalScore(); else if(type.equals("选修")&&AssessmentMethod.equals("考试")) return (int)(getAverageUsualScore()*0.3+getAverageFinalScore()*0.7); return 0; } }
这里则是课程类,其中有属性课程名字,课程类型,考察方式属性,其中这里我计算的是总的课程成绩,所以我还设计了几个int类型的属性来记录每添加一个学生则添加进的总成绩,还写了计算各个平均成绩的方法和添加成绩的方法。
然后就是设计record类的代码:
class Record{ Class[] theClass = new Class[100]; static int n=0; Course[] courses = new Course[100]; static int m=0; Class findAClass(String num) { for(int i=0;i<n;i++) { if(theClass[i].num.equals(num)) return theClass[i]; } return null; } Course findACourse(String courseName) { for(int i=0;i<m;i++) { if(courses[i].CourseName.equals(courseName)) return courses[i]; } return null; } void addAClass(String num) { theClass[n]=new Class(); theClass[n].num=num; n++; } void addACourse(String CourseName,String type,String AssessmentMethod ) { if(findACourse(CourseName)==null) { if (CourseName.length() <= 10 && CourseName.length() > 0&&(type.equals("必修")||type.equals("选修"))&&(AssessmentMethod.equals("考察")||AssessmentMethod.equals("考试"))) { courses[m] = new Course(); courses[m].CourseName = CourseName; courses[m].type = type; courses[m].AssessmentMethod = AssessmentMethod; m++; } else System.out.println("wrong format"); } else if(findACourse(CourseName)!=null) { if (CourseName.length() > 10) { System.out.println("wrong format"); } } } void getStudentScore() { Collator collator = Collator.getInstance(Locale.getDefault()); Arrays.sort(theClass,0,n, (c1, c2) -> collator.compare(c1.num, c2.num)); for(int i=0;i<n;i++) { Arrays.sort(theClass[i].student,1,theClass[i].x+1, (c1, c2) -> collator.compare(c1.classNum, c2.classNum)); } for(int i=0;i<n;i++) { for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0) System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" "+theClass[i].student[j].getTotalGrade()); else System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" did not take any exams"); } } } void getClassScore() { for(int i=0;i<n;i++) { int temp=0; for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0){ temp=1; break; } } if(temp==0) { System.out.println(theClass[i].num + " has no grades yet"); break; } if(temp==1) System.out.println(theClass[i].num+" "+theClass[i].getTotalAverageGrade()); } } void getCourseScore() { Collator collator = Collator.getInstance(Locale.CHINA); Arrays.sort(courses,0,m, (c1, c2) -> collator.compare(c1.CourseName, c2.CourseName)); for(int i=0;i<m;i++) { if(courses[i].type.equals("必修")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageUsualScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考察")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageFinalScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考试")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageUsualScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } } } }
这里则是有班级,课程属性,分别来记入录入的课程和课程成绩信息,然后定义了查找和添加的方法,以及最后计算各个总平均成绩的方法。这里则用到了collator类里的排序按照题目要求,对最后得到的结果进行排序输出。
最后则是设计主函数main:
public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); Record record=new Record(); String str = sc.nextLine(); while (!str.equals("end")) { int i=InputMatching.matchingInput(str); String[] data=str.split(" "); if(i==0) { System.out.println("wrong format"); } else if(i==1) { if((data.length==3&&data[1].equals("必修")&&data[2].equals("考试"))) { record.addACourse(data[0],data[1],data[2]); } else if(data.length==3&&data[1].equals("选修")&&(data[2].equals("考试")||data[2].equals("考察"))) { record.addACourse(data[0],data[1],data[2]); } else if(data.length==3&&data[1].equals("必修")&&data[2].equals("考察")) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } } else if(i==2) { if(data.length==4&&!(Integer.parseInt(data[3])>=0)&&!(Integer.parseInt(data[3])<=100)) { System.out.println("wrong format"); continue; } else if(data.length==5&&!(Integer.parseInt(data[3])>=0)&&!(Integer.parseInt(data[3])<=100)&&!(Integer.parseInt(data[4])>=0)&&!(Integer.parseInt(data[4])<=100)) { System.out.println("wrong format"); continue; } if(record.findACourse(data[2])!=null){ if(record.findACourse(data[2]).type.equals("必修")&&data.length==5) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4])); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore((ExamGrade) grade); } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==4&&record.findACourse(data[2]).AssessmentMethod.equals("考察")) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade = new InvestigateGrade(); grade.finalGrade=Integer.parseInt(data[3]); boolean b=record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) { record.findACourse(data[2]).addScore((InvestigateGrade) grade); } } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==5&&record.findACourse(data[2]).AssessmentMethod.equals("考试")){ if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4])); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore((ExamGrade) grade); } else{ if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0, 6)).addAIllegalStudent(data[0], data[1]); System.out.println(data[0] + " " + data[1] + " " + ": access mode mismatch"); } } else if(record.findACourse(data[2])==null) { System.out.println(data[2]+" does not exist"); if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0,6)).addAIllegalStudent(data[0],data[1]); } } str=sc.nextLine(); } record.getStudentScore(); record.getCourseScore(); record.getClassScore(); } }
这里根据录入的数据的不同长度来判断是什么信息,然后存入对应的对象里,如输入课程时什么情况对应的会是什么结果,如果可以遵循题目所给要求则录入课程,如果不是则按题目要求输出类型不匹配,成绩信息也是如此,有很多情况,比如课程不存在,学生输入的课程成绩不对应之前输入的课程的类型,以及对于非法学生成绩的处理,最后打印全部成绩。
但是这里题目对于输入的合法性同样有要求,如果在main里对输入的字符串进行判断比较麻烦,所以直接使用正则表达式类来对输入字符串进行判断以下是代码:
class InputMatching { static String stuNumMatching = "\\d{8}";//8个0-9的数字 static String stuNameMatching = "[^ \\t]{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "[^ \\t]{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修)"; static String checkcourseTypeMatching = "(考试|考察)"; static String scoreMatching1 = "(\\s([1-9]?[0-9]|100))?"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + scoreMatching1; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { return s.matches(scoreInput); } }
根据不同格式返回不同整数。
以下为样例
以下为类图
接下来是课程成绩统计程序-2,这里我相比于1做出的改变就是在grade类添加一个子类ExperimentGrade类用来记入实验成绩,然后按照题目要求对main类进行修改,以及相应的record和course里面计算成绩可能出现的情况,以下为代码
ExperimentGrade类:
class ExperimentGrade extends Grade{ int number; int TotalExperimentGrade=0; public ExperimentGrade(){ } public ExperimentGrade(int parseInt, int parseInt1,int parseInt2,int number) { super(parseInt,parseInt1); this.TotalExperimentGrade = parseInt2; this.number=number; } void addTotalExperimentGrade(int number){ TotalExperimentGrade+=number; } int getGrade(){ return TotalExperimentGrade/number; } }
这里直接将main里直接进行计算得到成绩存入其中。
course类:
class Course{ String CourseName; String type; String AssessmentMethod; int usualScore=0; int finalScore=0; int averageScore=0; int studentNum=0; void addScore(ExamGrade examGrade) { finalScore+=examGrade.finalGrade; usualScore+=examGrade.usualGrade; averageScore+=(int)(examGrade.finalGrade*0.7+examGrade.usualGrade*0.3); studentNum++; } void addScore(InvestigateGrade investigateGrade) { finalScore+=investigateGrade.finalGrade; averageScore+=finalScore; studentNum++; } void addScore(ExperimentGrade experimentGrade) { averageScore+=experimentGrade.getGrade(); studentNum++; } int getAverageUsualScore() { return (int)(1.0*usualScore/studentNum); } int getAverageFinalScore() { return (int)(1.0*finalScore/studentNum); } int getAverageScore() { if(type.equals("必修")) return averageScore/studentNum; else if(type.equals("选修")&&AssessmentMethod.equals("考察")) return averageScore/studentNum; else if(type.equals("选修")&&AssessmentMethod.equals("考试")) return averageScore/studentNum; else if(type.equals("实验")) { return averageScore/studentNum; } return 0; } }
最后获得平均成绩多了一种情况。
record类:
class Record{ Class[] theClass = new Class[100]; static int n=0; Course[] courses = new Course[100]; static int m=0; Class findAClass(String num) { for(int i=0;i<n;i++) { if(theClass[i].num.equals(num)) return theClass[i]; } return null; } Course findACourse(String courseName) { for(int i=0;i<m;i++) { if(courses[i].CourseName.equals(courseName)) return courses[i]; } return null; } void addAClass(String num) { theClass[n]=new Class(); theClass[n].num=num; n++; } void addACourse(String CourseName,String type,String AssessmentMethod ) { if(findACourse(CourseName)==null) { courses[m] = new Course(); courses[m].CourseName = CourseName; courses[m].type = type; courses[m].AssessmentMethod = AssessmentMethod; m++; } else if(findACourse(CourseName)!=null) { if (CourseName.length() > 10) { System.out.println("wrong format"); } } } void getStudentScore() { Collator collator = Collator.getInstance(Locale.getDefault()); Arrays.sort(theClass,0,n, (c1, c2) -> collator.compare(c1.num, c2.num)); for(int i=0;i<n;i++) { Arrays.sort(theClass[i].student,1,theClass[i].x+1, (c1, c2) -> collator.compare(c1.classNum, c2.classNum)); } for(int i=0;i<n;i++) { for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0) System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" "+theClass[i].student[j].getTotalGrade()); else System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" did not take any exams"); } } } void getClassScore() { for(int i=0;i<n;i++) { int temp=0; for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0){ temp=1; break; } } if(temp==0) { System.out.println(theClass[i].num + " has no grades yet"); continue; } System.out.println(theClass[i].num+" "+theClass[i].getTotalAverageGrade()); } } void getCourseScore() { Collator collator = Collator.getInstance(Locale.CHINA); Arrays.sort(courses,0,m, (c1, c2) -> collator.compare(c1.CourseName, c2.CourseName)); for(int i=0;i<m;i++) { if(courses[i].type.equals("必修")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageUsualScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考察")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageFinalScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考试")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageUsualScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("实验")&&courses[i].AssessmentMethod.equals("实验")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " "+ courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } } } }
这里也是在获得平均成绩时多的几种情况。
以及最后的main类:
public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); Record record=new Record(); while (true) { String str=sc.nextLine(); if(str.equals("end")) break; int i=InputMatching.matchingInput(str); String[] data=str.split(" "); if(i==0) { System.out.println("wrong format"); } else if(i==1) { if((data.length==3&&data[1].equals("必修")&&data[2].equals("考试"))) { record.addACourse(data[0],data[1],data[2]); } else if(data.length==3&&data[1].equals("选修")&&(data[2].equals("考试")||data[2].equals("考察"))) { record.addACourse(data[0],data[1],data[2]); } else if(data.length==3&&data[1].equals("实验")&&data[2].equals("实验")) { record.addACourse(data[0],data[1],data[2]); } else if(data.length==3&&data[1].equals("必修")&&(data[2].equals("考察")||data[2].equals("实验"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } else if(data.length==3&&data[1].equals("选修")&&(data[2].equals("实验"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } else if(data.length==3&&data[1].equals("实验")&&(data[2].equals("考试")||data[2].equals("考察"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } } else if(i==2) { if(record.findACourse(data[2])!=null) { if(data.length==5&&record.findACourse(data[2]).type.equals("实验")) { System.out.println("wrong format"); continue; } else if(data.length>5&&record.findACourse(data[2]).type.equals("实验")) { if(Integer.parseInt(data[3])<4||Integer.parseInt(data[3])>9) { System.out.println("wrong format"); continue; } } } if(record.findACourse(data[2])!=null){ if(record.findACourse(data[2]).type.equals("必修")&&data.length==5) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4])); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore((ExamGrade) grade); } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==4&&record.findACourse(data[2]).AssessmentMethod.equals("考察")) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade = new InvestigateGrade(); grade.finalGrade=Integer.parseInt(data[3]); boolean b=record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) { record.findACourse(data[2]).addScore((InvestigateGrade) grade); } } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==5&&record.findACourse(data[2]).AssessmentMethod.equals("考试")) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4])); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore((ExamGrade) grade); } else if(record.findACourse(data[2]).type.equals("实验")&&(Integer.parseInt(data[3])== data.length-4)) { if(record.findAClass(data[0].substring(0,6))==null){ record.addAClass(data[0].substring(0, 6)); } int studentExperimentGrade=0; for(int ExperimentScoreNum=4;ExperimentScoreNum<data.length;ExperimentScoreNum++) { studentExperimentGrade+=Integer.parseInt(data[ExperimentScoreNum]); } Grade grade=new ExperimentGrade(0,0,studentExperimentGrade,Integer.parseInt(data[3])); boolean b=record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore((ExperimentGrade) grade); } else { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0, 6)).addAIllegalStudent(data[0], data[1]); System.out.println(data[0] + " " + data[1] + " " + ": access mode mismatch"); } } else if(record.findACourse(data[2])==null) { if((Integer.parseInt(data[3])<4||Integer.parseInt(data[3])>9)&&data.length>5&&Integer.parseInt(data[3])!=data.length) { System.out.println("wrong format"); } else { System.out.println(data[2] + " does not exist"); if (record.findAClass(data[0].substring(0, 6)) == null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0, 6)).addAIllegalStudent(data[0], data[1]); } } } } record.getStudentScore(); record.getCourseScore(); record.getClassScore(); } }
这里也是对可能出现的实验课情况的增加。其他基本不变。
以下为部分样例结果:
样例全部通过,此次作业完成。
接下来是第八次的课程成绩-3,这次是基于课程成绩-2的修改就是对于课程成绩权重变成了不确定,需要用户输入,而且实验课和必修课的课程输入格式与之前不同了,成绩信息也发生了变化,所以要对代码作出较多的变化,同时正则表达式的格式也要发生改变,首先我对course类里面加了权重属性因为实验课程的成绩数量是不确定的所以我用的是arraylist来记入各个权重,而grade类也由继承变为了组合关系,同时对于main函数里的输入情况不同,也对原代码做出了改变,按照不同的输入长度以及部分区别特征来执行相应操作,同时按照题目要求,对于各部分按权重求平均分的方法进行了重新设计,对于结果重新计算,以下为整个程序代码
import java.text.Collator; import java.util.ArrayList; import java.util.Locale; import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); Record record=new Record(); while (true) { String str=sc.nextLine(); if(str.equals("end")) break; int i=InputMatching.matchingInput(str); String[] data=str.split(" "); if(i==0) { System.out.println("wrong format"); } else if(i==1) { if(data[1].equals("必修")&&data[2].equals("考试")&&record.findACourse(data[0])==null) { if(data.length==5) { if((Float.parseFloat(data[3])+Float.parseFloat(data[4])==1.0)) { record.addACourse(data[0], data[1], data[2]); record.findACourse(data[0]).usualScoreWeight=Float.parseFloat(data[3]); record.findACourse(data[0]).finalScoreWeight=Float.parseFloat(data[4]); } else System.out.println(data[0]+" : weight value error"); } } else if(data[1].equals("选修")&&(data[2].equals("考试")||data[2].equals("考察"))&&record.findACourse(data[0])==null) { if(data[2].equals("考察")) { record.addACourse(data[0], data[1], data[2]); } else { if((Float.parseFloat(data[3])+Float.parseFloat(data[4])==1.0)) { record.addACourse(data[0], data[1], data[2]); record.findACourse(data[0]).usualScoreWeight=Float.parseFloat(data[3]); record.findACourse(data[0]).finalScoreWeight=Float.parseFloat(data[4]); } else System.out.println(data[0]+" : weight value error"); } } else if(data[1].equals("实验")&&data[2].equals("实验")&&record.findACourse(data[0])==null) { if(data.length-4==Integer.parseInt(data[3])) { float num = 0; for(int temp=4;temp<data.length;temp++) { num+=Float.parseFloat(data[temp]); } if(num==1.0) { record.addACourse(data[0], data[1], data[2]); for (int temp1=4;temp1< data.length;temp1++) { record.findACourse(data[0]).weight.add(Float.parseFloat(data[temp1])); } }else System.out.println(data[0]+" : weight value error"); } else System.out.println(data[0]+" : number of scores does not match"); } else if(data[1].equals("必修")&&(data[2].equals("考察")||data[2].equals("实验"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } else if(data[1].equals("选修")&&(data[2].equals("实验"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } else if(data[1].equals("实验")&&(data[2].equals("考试")||data[2].equals("考察"))) { System.out.println(data[0] + " : " + "course type & access mode mismatch"); } } else if(i==2) { if(record.findACourse(data[2])!=null) { if((data.length<7||data.length>12)&&record.findACourse(data[2]).type.equals("实验")) { System.out.println("wrong format"); continue; } } if(record.findACourse(data[2])!=null) { if(record.findACourse(data[2]).type.equals("必修")&&data.length==5) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } ExamGrade examGrade=new ExamGrade(Integer.parseInt(data[3]),record.findACourse(data[2]).usualScoreWeight,Integer.parseInt(data[4]),record.findACourse(data[2]).finalScoreWeight); Grade grade=new Grade(examGrade); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore(examGrade); } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==4&&record.findACourse(data[2]).AssessmentMethod.equals("考察")) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } InvestigateGrade investigateGrade=new InvestigateGrade(Integer.parseInt(data[3])); Grade grade = new Grade(investigateGrade); boolean b=record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) { record.findACourse(data[2]).addScore(investigateGrade); } } else if(record.findACourse(data[2]).type.equals("选修")&&data.length==5&&record.findACourse(data[2]).AssessmentMethod.equals("考试")) { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } ExamGrade examGrade=new ExamGrade(Integer.parseInt(data[3]),record.findACourse(data[2]).usualScoreWeight,Integer.parseInt(data[4]),record.findACourse(data[2]).finalScoreWeight); Grade grade=new Grade(examGrade); boolean b =record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore(examGrade); } else if(record.findACourse(data[2]).type.equals("实验")&&(record.findACourse(data[2]).weight.size()== data.length-3)) { if(record.findAClass(data[0].substring(0,6))==null){ record.addAClass(data[0].substring(0, 6)); } float studentExperimentGrade=0; for(int ExperimentScoreNum=3 ;ExperimentScoreNum<data.length;ExperimentScoreNum++) { studentExperimentGrade+=Integer.parseInt(data[ExperimentScoreNum])*record.findACourse(data[2]).weight.get(ExperimentScoreNum-3); } ExperimentGrade experimentGrade=new ExperimentGrade((int)studentExperimentGrade); Grade grade=new Grade(experimentGrade); boolean b=record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade); if(!b) record.findACourse(data[2]).addScore(experimentGrade); } else { if(record.findAClass(data[0].substring(0,6))==null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0, 6)).addAIllegalStudent(data[0], data[1]); System.out.println(data[0] + " " + data[1] + " " + ": access mode mismatch"); } } else if(record.findACourse(data[2])==null) { System.out.println(data[2] + " does not exist"); if (record.findAClass(data[0].substring(0, 6)) == null) { record.addAClass(data[0].substring(0, 6)); } record.findAClass(data[0].substring(0, 6)).addAIllegalStudent(data[0], data[1]); } } } record.getStudentScore(); record.getCourseScore(); record.getClassScore(); } } class Record{ Class[] theClass = new Class[100]; static int n=0; Course[] courses = new Course[100]; static int m=0; Class findAClass(String num) { for(int i=0;i<n;i++) { if(theClass[i].num.equals(num)) return theClass[i]; } return null; } Course findACourse(String courseName) { for(int i=0;i<m;i++) { if(courses[i].CourseName.equals(courseName)) return courses[i]; } return null; } void addAClass(String num) { theClass[n]=new Class(); theClass[n].num=num; n++; } void addACourse(String CourseName,String type,String AssessmentMethod ) { if(findACourse(CourseName)==null) { courses[m] = new Course(); courses[m].CourseName = CourseName; courses[m].type = type; courses[m].AssessmentMethod = AssessmentMethod; m++; } } void getStudentScore() { Collator collator = Collator.getInstance(Locale.getDefault()); Arrays.sort(theClass,0,n, (c1, c2) -> collator.compare(c1.num, c2.num)); for(int i=0;i<n;i++) { Arrays.sort(theClass[i].student,1,theClass[i].x+1, (c1, c2) -> collator.compare(c1.classNum, c2.classNum)); } for(int i=0;i<n;i++) { for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0) System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" "+theClass[i].student[j].getTotalGrade()); else System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" did not take any exams"); } } } void getClassScore() { for(int i=0;i<n;i++) { int temp=0; for(int j=1;j<=theClass[i].x;j++) { if(theClass[i].student[j].y>0){ temp=1; break; } } if(temp==0) { System.out.println(theClass[i].num + " has no grades yet"); continue; } System.out.println(theClass[i].num+" "+theClass[i].getTotalAverageGrade()); } } void getCourseScore() { Collator collator = Collator.getInstance(Locale.CHINA); Arrays.sort(courses,0,m, (c1, c2) -> collator.compare(c1.CourseName, c2.CourseName)); for(int i=0;i<m;i++) { if(courses[i].type.equals("必修")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考察")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " "+ courses[i].getAverageFinalScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("选修")&&courses[i].AssessmentMethod.equals("考试")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " " + courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } else if(courses[i].type.equals("实验")&&courses[i].AssessmentMethod.equals("实验")) { if(courses[i].studentNum>0) { System.out.println(courses[i].CourseName + " "+ courses[i].getAverageScore()); }else System.out.println(courses[i].CourseName+" has no grades yet"); } } } } class Class{ String num; Student[] student=new Student[100]; int x=0; int getTotalAverageGrade() { int grade=0; int people=0; for(int i=1;i<=x;i++) { if(student[i].y!=0) { grade += student[i].getTotalGrade(); people++; } } return grade/people; } Student findAStudent(String name) { for(int i=1;i<=x;i++) { if(student[i].name.equals(name)) { return student[i]; } } return null; } boolean addAStudent(String classNum, String name, String SubjectName,Grade grade) { if(findAStudent(name)==null) { x++; student[x] = new Student(); student[x].classNum = classNum; student[x].name = name; if(student[x].findSubject(SubjectName)==null) { student[x].addSubject(SubjectName, grade); return false; } } else if(findAStudent(name)!=null) { if(student[x].findSubject(SubjectName)==null) { findAStudent(name).addSubject(SubjectName, grade); return false; } } return true; } void addAIllegalStudent(String classNum,String name) { if(findAStudent(name)==null) { x++; student[x] = new Student(); student[x].classNum = classNum; student[x].name = name; } } } class Student{ String classNum; String name; Subject[] subject=new Subject[100]; int y=0; int getTotalGrade() { int totalGrade=0; for(int i=0;i<y;i++) { totalGrade+=subject[i].grade.getGrade(); } if(y!=0) return totalGrade/y; return -1; } void addSubject(String SubjectName,Grade grade) { subject[y]=new Subject(); subject[y].SubjectName = SubjectName; subject[y].grade = grade; y++; } Subject findSubject(String SubjectName) { for(int i=0;i<y;i++) { if(subject[i].SubjectName.equals(SubjectName)) { return subject[i]; } } return null; } } class Subject{ String SubjectName; Grade grade; } class Course{ String CourseName; String type; String AssessmentMethod; int usualScore=0; float usualScoreWeight; int finalScore=0; float finalScoreWeight; int averageScore=0; int studentNum=0; ArrayList<Float> weight = new ArrayList<>(); void addScore(ExamGrade examGrade) { finalScore+=examGrade.finalGrade; usualScore+=examGrade.usualGrade; averageScore+=(int)(examGrade.finalGrade*finalScoreWeight+examGrade.usualGrade*usualScoreWeight); studentNum++; } void addScore(InvestigateGrade investigateGrade) { finalScore+=investigateGrade.finalGrade; averageScore+=finalScore; studentNum++; } void addScore(ExperimentGrade experimentGrade) { averageScore+=experimentGrade.getGrade(); studentNum++; } int getAverageUsualScore() { return (int)(1.0*usualScore/studentNum); } int getAverageFinalScore() { return (int)(1.0*finalScore/studentNum); } int getAverageScore() { if(type.equals("必修")) return (int)averageScore/studentNum; else if(type.equals("选修")&&AssessmentMethod.equals("考察")) return (int)averageScore/studentNum; else if(type.equals("选修")&&AssessmentMethod.equals("考试")) return (int)averageScore/studentNum; else if(type.equals("实验")) { return (int)averageScore/studentNum; } return 0; } } class Grade{ ExamGrade examGrade; int examGradeTip=0; InvestigateGrade investigateGrade; int investigateGradeTip=0; ExperimentGrade experimentGrade; int experimentGradeTip =0; public Grade(){} public Grade(ExamGrade examGrade) { this.examGrade=examGrade; examGradeTip++; } public Grade(InvestigateGrade investigateGrade) { this.investigateGrade=investigateGrade; investigateGradeTip++; } public Grade(ExperimentGrade experimentGrade) { this.experimentGrade=experimentGrade; experimentGradeTip++; } int getGrade() { if(examGradeTip!=0) { return examGrade.getGrade(); }else if(investigateGradeTip!=0){ return investigateGrade.getGrade(); }else if(experimentGradeTip!=0) { return experimentGrade.getGrade(); } return 0; } } class ExamGrade { int usualGrade; float usualGradeWeight; int finalGrade; float finalGradeWeight; public ExamGrade(){} public ExamGrade(int usualGrade,float usualGradeWeight,int finalGrade,float finalGradeWeight){ this.finalGradeWeight=finalGradeWeight; this.usualGradeWeight=usualGradeWeight; this.usualGrade=usualGrade; this.finalGrade=finalGrade; } int getGrade(){ return (int) (usualGrade * usualGradeWeight + finalGrade * finalGradeWeight); } } class InvestigateGrade { int finalGrade; public InvestigateGrade(){} public InvestigateGrade(int finalGrade) { this.finalGrade=finalGrade; } int getGrade(){ return finalGrade; } } class ExperimentGrade { int TotalExperimentGrade=0; public ExperimentGrade(){} public ExperimentGrade(int TotalExperimentGrade){ this.TotalExperimentGrade=TotalExperimentGrade; } int getGrade(){ return TotalExperimentGrade; } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String scoreMatching1 = "([1-9]\\d{0,1}|0|100)( ([1-9]\\d{0,1}|0|100)){1,9}"; static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修|实验)"; static String courseTypeMatching1 = "(选修|必修)"; static String checkcourseTypeMatching = "(考试|考察|实验)"; static String experimentNumber = "[4-9]"; static String point = "\\d+\\.\\d+"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching ; static String courseInput0 = courseNameMatching + " " + courseTypeMatching + " " + "考试"+ " " + point + " " + point; static String courseInput1 = courseNameMatching + " " + "实验" + " " + "实验" + " " + experimentNumber + " " + point + "( " + point + "){0,}"; static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching; static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching +" "+scoreMatching1; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return (s.matches(courseInput)||s.matches(courseInput1)||s.matches(courseInput0)); } private static boolean matchingScore(String s) { return (s.matches(scoreInput)||s.matches(scoreInput1)); } }
以下为样例结果:
样例均可通过,自此代码完成。
以下为类图:
(3)踩坑心得
这几次作业中会遇到问题的就是遇到一些没有学习过的知识,需要花时间去学习了解,比如hashmap的使用,对于它的增删查改不了解,以及前面对应关系的使用不是很了解,去查找了很多资料,然后进行了很多尝试,然后才能将题目完成,还有一些面向对象的基础和进阶知识,比如那个栈和覆盖,概念很好理解,但是用起来还是费点心思,比如这个hashmap里面对象的排序如何去排序,也是需要去查找资料的,然后就是对于成绩统计系统中遇到的坑也挺多的,首先在没有使用正则表达式之前,对于输入字符串合法性的判断只能通过在主函数里的选择条件里面判断,这样很繁琐麻烦,而且最后运行会发现很多测试点不过,因为在一开始输入的合法性我不能根据题目给出的要求判断好,然后后面用了正则表达式之后,可以直接对输入的字符串进行判断,然后解决了问题,然后就过了测试点,这个过程中,我也遇到了问题,我要去学习什么是正则表达式,然后要去用它,去一点一点试怎么用,这个过程也出了很多错误,其次就是在-2中,我再过测试点的时候,在最后一个测试点过不了,然后我举了很多样例,最终发现是因为,当学生和课程和班级很多的时候,我输入的学生是录的非法学生,此时计算成绩时的人数不应该增加,但是我却加了,导致最后的结果不对,我发现很多处都存在这样的问题,然后花了很多时间去找它,最后添加了判断,如果是非法学生添加,则总成绩人数那里不增加。然后就过了这个测试点。以及在-3中,由于权重得到的结果都是小数,所以最后的答案总是不准确,会多一或者少一,所以这个过程中就要我自己对于全部的计算平均成绩的方法重新看一遍,按照题目给的要求进行修改,最终得到正确答案。
(4)改进意见
在编写课程成绩统计程序的时候,我发现代码有很多地方可以改进,如在-1时我一开始写没有过很多测试点,因为我的逻辑出了问题,在main函数里,我的条件判断顺序出了错误,比如先判断是否有课再判断班再判断成绩,结果我把顺序搞乱了,导致逻辑出现错误过不了测试点,最后花了很多时间去查找调试,最终解决了问题,而且在一开始的时候题目说什么我就写什么,没有对代码进行进一步的思考,然后写到最后发现代码超出长度了,于是我就开始看代码精简代码,发现很多重复冗杂的代码,然后我就将它们缩改,最后的代码就简洁了很多,然后就是在record类里面的对最后结果进行排序输出,一开始我用的单纯就是冒泡法对他们进行排序,而且在每个方法里都写了冒泡法,导致代码长度超了,然后后面我发现可以根据那个输出顺序就只需要在一个方法里进行排序,后面的方法则是直接打印排好序了的结果,而且我还将冒泡法改为了java自带的collator类排序的方法,这样代码又精简了很多很多,然后在-3时,一开始我看到会变化的成绩数量感到没有头绪,不知道怎么设计修改代码,后面思考了一番,然后在course的课程中添加了权重的arraylist然后将成绩一一对应传入计算得到最后的成绩,而且对于这里的正则表达式也是进行了很多次测试才找到一个如何可以判断这么多种情况的正则表达式。并且将grade类改成了组合关系,这样可以比原来的继承关系更好地使用这三种考核成绩的计算方法,最后就是对于一些无关程序的代码进行了删除,让代码更加简明。
(5)总结
在这几次中,我对于java的使用相比于1-3次熟练了很多,对于类的构造更加的清晰,而且对于问题的排除也熟练不少,其实这次的成绩统计程序和之前的菜单计价程序区别不大,经过之前的练习,我对于这3次成绩统计程序的编写更加的得心应手,而且这个过程中还是很有收获的,比如学习了正则表达式,以及对于代码逻辑的清晰度也有提升,在编写代码过程中遇到了问题让我多加思考,锻炼我的能力,比如说在录课程成绩的时候,course类里面不好记录每个学生信息,而学生类里面也不好记录course类里的信息,所以我就再设计了一个subject类来重新记录学生个人成绩,course类则用来记录总的成绩,然后在设计grade类的时候则用了继承和组合两种关系,然后感受这两种关系各自的优缺点,再是main函数里面各个数据对应的操作入口,这里我花了时间去琢磨应该如何设计,然后这个过程中也发现了自身很多问题,也让我深有感触(认识到要对自己的代码逻辑思考清楚),巩固了java面向对象基础,同时也学习了一些面向对象的进阶知识,如栈。还学习到了hashmap的用法,高效的查找和插入:HashMap使用哈希表实现,在平均情况下可以提供查找和插入操作。这使得HashMap在处理大量数据时非常高效。键唯一性:HashMap的键是唯一的,即同一个HashMap中不能存在两个相同的键。当尝试插入具有相同键的值时,新的值将会替换旧的值。然后这个程序感觉就是综合在此之前我所学习过的全部java的知识,让我全部融汇运用进去,写完之后感觉对于这些知识的使用熟练度大大增加,而且在这个过程中还需要我自己多思考,如何实现各个功能,如何让我各个部分的代码更加精简,更加合理,如何将这些类调用连接起来,让我的思维能力也得到了提升。但是这个过程我感觉还是很艰难的,需要借助外部力量,多问同学,多学习不同的思想,从中获得灵感。同时也感觉这样的菜单计价程序出的挺不错,因为层层递进,让我可以适应其难度。然后将所学的知识融会贯通,更好地去理解java这门语言。现在再去写这三个程序则简单多了,以后我会继续学习,对于出现过的问题多加总结,提升自己的编程能力。
标签:总结,题目,courses,int,equals,PTA,System,record,data From: https://www.cnblogs.com/lztnchu/p/17512405.html