JAVA第7-12次作业总结

前言

JAVA的学习就要告一个段落了,虽然学了很多,但总是感觉学的不精,以后还有很长的路要走,现对第三阶段的六次题目集做一个总结。

第七次题目集主要涉及到图形继承、多态、题量少,题目简单,算是期中考试前的复习。

第八次题目集主要涉及类的继承、多态、接口等,题量少,题目简单;

第九次作业是统计Java程序中关键词的出现次数,涉及到Map的相关用法,有一定难度。

第十,十一,十二次作业是课程成绩统计程序的一次类结构设计,是对其一次次的改进和加工,考察程序的完整性、结构性以及我们对代码改写的水平和方式,同时也设计到集合进阶的相关应用,是一次综合的大练习。

一:几次作业遇到的BUG(难点)

8-2 图形卡片分组游戏

类之间的关系和交互

接口的实现

逻辑判断和错误处理

9-1 统计Java程序中关键词的出现次数

正则表达式的使用

正则表达式的使用

Java关键字的定义和理解

代码逻辑的理解和掌握

10-1 课程成绩统计程序-1

理解和掌握面向对象的编程思想和相关的概念

对列表和对象的操作和使用

理解和掌握字符串的处理和切割、条件判断和循环等

综合分析

11-3 课程成绩统计程序-2

实验课程的加入

相关信息的约束

输出输入

整体架构的大局观

12-2 课程成绩统计程序-3

需要修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。

比较继承和组合关系的区别,对关系运用灵活与否的思考

二:代码思路与解决方法

8-2 图形卡片分组游戏

源码呈现:

复制代码
import java.lang.reflect.Array;
import java.util.*;
public class Main {
    //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>();

        int num = input.nextInt();
        if(num == 0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        while(num != 0){
            if(num < 0 || num > 4){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }

        DealCardList dealCardList = new DealCardList(list);

        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}
class Card implements Comparable<Card>{
    private Shape shape;

    public Card(Shape shape) {
        this.shape = shape;
    }
    @Override
    public int compareTo(Card card){
        if(card.getShape().getArea() == shape.getArea()){
            return 0;
        }else if(card.getShape().getArea() < shape.getArea()){
            return -1;
        }else{
            return 1;
        }
    }
    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }
}
class DealCardList {
    ArrayList<Card> cardList = new ArrayList<>();
    ArrayList<Card> CircleList = new ArrayList<>();
    ArrayList<Card> RectangleList = new ArrayList<>();
    ArrayList<Card> TriangleList = new ArrayList<>();
    ArrayList<Card> TrapezoidList = new ArrayList<>();

    public DealCardList(ArrayList<Integer> list) {
        for(Object A:list){
            if((int)A == 1){
                double R = Main.input.nextDouble();
                CircleList.add(new Card(new Circle(R)));
                cardList.add(new Card(new Circle(R)));
            }
            if((int)A == 2){
                double W,L;
                W = Main.input.nextDouble();
                L = Main.input.nextDouble();
                RectangleList.add(new Card(new Rectangle(W,L)));
                cardList.add(new Card(new Rectangle(W,L)));
            }
            if((int)A == 3){
                double a,b,c;
                a = Main.input.nextDouble();
                b = Main.input.nextDouble();
                c = Main.input.nextDouble();
                TriangleList.add(new Card(new Triangle(a,b,c)));
                cardList.add(new Card(new Triangle(a,b,c)));
            }
            if((int)A == 4){
                double sd,xd,gao;
                sd = Main.input.nextDouble();
                xd = Main.input.nextDouble();
                gao = Main.input.nextDouble();
                TrapezoidList.add(new Card(new Trapezoid(sd,xd,gao)));
                cardList.add(new Card(new Trapezoid(sd,xd,gao)));
            }
        }
    }

    public void showResult(){
        System.out.println("The original list:");
        System.out.printf("[");

        for(Card a:cardList){
            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }
        System.out.printf("]");
        System.out.println("\nThe Separated List:");
        System.out.print("[");
    for(Card a:CircleList){
        System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());
    }
        System.out.print("]");
        System.out.print("[");
        for(Card a:RectangleList){
            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());
        }
        System.out.print("]");
        System.out.print("[");
        for(Card a:TriangleList){

            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }
        System.out.print("]");
        System.out.print("[");
        for(Card a:TrapezoidList){

            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }
        System.out.print("]");
    System.out.println("\nThe Separated sorted List:");
    Collections.sort(CircleList);
    Collections.sort(RectangleList);
    Collections.sort(TriangleList);
    Collections.sort(TrapezoidList);
    Collections.sort(cardList);
        System.out.print("[");
    for(Card a:CircleList){

        System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

    }
        System.out.print("]");
        System.out.print("[");
        for(Card a:RectangleList){

            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }  System.out.print("]");System.out.print("[");
        for(Card a:TriangleList){

            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }System.out.print("]");  System.out.printf("[");
        for(Card a:TrapezoidList){

            System.out.printf("%s:%.2f ",a.getShape().getShapeName(),a.getShape().getArea());

        }System.out.print("]");
        double sum1=0;
        double sum2=0;
        double sum3=0;
        double sum4=0;

        for(Card a:CircleList){
            sum1 = sum1 + a.getShape().getArea();
        }
        for(Card a:RectangleList){
            sum2 = sum2 + a.getShape().getArea();
        }
        for(Card a:TriangleList){
            sum3 = sum3 + a.getShape().getArea();
        }
        for(Card a:TrapezoidList){
            sum4 = sum4 + a.getShape().getArea();
        }
        double max = 0;
        double[] areaList = new double[]{sum1,sum2,sum3,sum4};
        Arrays.sort(areaList);
    System.out.printf("\nThe max area:%.2f",areaList[3]);
    }
    public boolean validate(){
    boolean result = true;
    for(Card a:cardList){
        if(!a.getShape().validate()){
            return false;
        }
    }
    return result;
    }
}

abstract class Shape{
    private String shapeName;
    public Shape() {
    }
    public double getArea(){
        return 0;
    }
    public boolean validate(){
        return false;
    }

    public Shape(String shapeName) {
        this.shapeName = shapeName;
    }

    public String getShapeName() {
        return shapeName;
    }

    public void setShapeName(String shapeName) {
        this.shapeName = shapeName;
    }
}
class Circle extends Shape{
    private double radius;
    @Override
    public boolean validate(){
        if(radius <= 0){
            return false;
        }else{
            return true;
        }
    }

    public Circle(double radius) {
        this.radius = radius;
        super.setShapeName("Circle");
    }
    @Override
    public double getArea(){
        return Math.PI*radius*radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
}
class Rectangle extends Shape{
    private double width,length;
    @Override
    public boolean validate(){
    if(width > 0 && length >0){
        return true;
    }else{
        return false;
    }
}
    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
        super.setShapeName("Rectangle");
    }
    @Override
    public double getArea(){
        return width*length;
    }
    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }
}
class Triangle extends Shape{
    private double side1,side2,side3;
    @Override
    public boolean validate(){
        if(side1 > 0 && side2 > 0 && side3 > 0){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public double getArea(){
        double p = (side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }
    public double getSide1() {
        return side1;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public double getSide2() {
        return side2;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public double getSide3() {
        return side3;
    }

    public void setSide3(double side3) {
        this.side3 = side3;
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
        super.setShapeName("Triangle");
    }
}
class Trapezoid extends Shape{
    private double topSide;
    private double bottomSide;
    private double height;

    public Trapezoid(double topSide, double bottomSide, double height) {
        this.topSide = topSide;
        this.bottomSide = bottomSide;
        this.height = height;
        super.setShapeName("Trapezoid");
    }
    @Override
    public double getArea(){
        return (topSide+bottomSide)*height/2;
    }
    @Override
    public boolean validate(){
        if(topSide > 0 && bottomSide >0 && height > 0){
            return true;
        }else{
            return false;
        }
    }
    public double getTopSide() {
        return topSide;
    }

    public void setTopSide(double topSide) {
        this.topSide = topSide;
    }

    public double getBottomSide() {
        return bottomSide;
    }

    public void setBottomSide(double bottomSide) {
        this.bottomSide = bottomSide;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}
复制代码

源码分析:

代码包含三个类:Main,Card和DealCardList。
在Main类中,我定义了一个静态的Scanner对象用于输入,并且主要逻辑在main方法中。
创建了一个ArrayList<Integer>对象 list 用于存储输入的整数。
调用input.nextInt()来获取一个整数,并将其赋值给变量num。
如果num的值为0,则输出"Wrong Format"并终止程序。
进入一个循环,当num的值不为0时,执行以下步骤:
如果num小于0或者大于4,则输出"Wrong Format"并终止程序。
将num添加到list中。
其次,我调用input.nextInt()来获取下一个整数并将其赋值给num。
创建一个DealCardList对象 dealCardList,并将list作为参数传递给它。
如果dealCardList的validate方法返回false,则输出"Wrong Format"并终止程序。
调用dealCardList的showResult方法来展示结果。

Card类实现了Comparable<Card>接口,它包含一个私有的Shape对象shape,并提供了一些方法用于设置和获取shape。

构造方法接受一个Shape对象作为参数,并将其赋值给shape。
实现了compareTo方法用于比较两个Card对象的面积大小,返回一个整数。

DealCardList类用于处理卡片列表,它包含多个ArrayList<Card>对象用于存储不同类型的卡片。

构造方法接受一个ArrayList<Integer>对象作为参数。
使用for循环遍历list中的每个元素,如果元素的值为1,则会执行以下步骤:
使用Main.input.nextDouble()获取一个double类型的值,并将其赋值给变量R。
创建一个Circle对象并将其添加到CircleList中。
最终的结果取决于Shape、Circle和其他相关类的具体实现,这题较为简单,不做过多阐述。

9-1 统计Java程序中关键词的出现次数

源码呈现:

复制代码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String important="abstract  assert  boolean  break  byte  case  catch  char  class  const  continue  default  do " +
                " double  else  enum  extends  final  finally  float  for  goto  if  implements  import  instanceof  int  " +
                "interface  long  native  new  package  private  protected  public  return  strictfp  short  static  super  " +
                "switch  synchronized  this  throw  throws  transient  try  void  volatile  while  true  false  null";
        String []end=important.split("  ");//因为粘贴下来的关键字之间有两个空格,所以切开来预处理,再放入映射容器中    //除了下面2种排序方法,还有很多方法完成排序    /*     一:对end数组使用sort,在关键字放入映射之前就完成排序,使用LinkedMap,该容器可以按插入顺序排序     二:在新建map对象时,使用自定义比较器构建对象,(HashMap,LinkedMap不支持)     如new TreeMap<String,Integer>(String::compareTo) 这里使用了方法引用     或者 new TreeMap<String,Integer>((u,v)->{return u.compareTo(v);});//使用了Lamba表达式     .......    */
        Map<String,Integer> mp=new TreeMap<String,Integer>();
        for(String e:end)  mp.put(e,0);
        String s=null;
        int flag=0;//检查是否有关键字出现过
        StringBuilder t=new StringBuilder();//用来完成字符串拼接

        while(!(s=in.nextLine().trim()).equals("exit")){
            //有第一种注释符号,跳出
            if(s.matches(".*//.*")) continue;//当然这里并不严谨,毕竟//注释可以放在有效代码后面
            t.append(s+" ");//将该字符串与下一行字符串隔开
        }
        //替换/**/
        s=t.toString();
        s=change(s);//要过最后一个测试点的特殊处理
        Pattern p=Pattern.compile("/\\*(.*)?\\*/");
        Matcher m=p.matcher(s);
        while(m.find()){
            s=s.replace(m.group()," ");
            m=p.matcher(s);
        }
        //替换""字符串
        p=Pattern.compile("\"(.*?)\"");
        m=p.matcher(s);
        while(m.find()) {
            s = s.replace(m.group(), " ");
            m = p.matcher(s);
        }
        if(s.length()==0){ System.out.print("Wrong Format");System.exit(0);}
        s=s.replaceAll("\\p{P}"," ");//替换掉所有的标点符号,$,_可以保留,但此题可以不考虑

        //否则获得处理后的字符串
        String[] temp=s.split("\\s+");
        for(String e:temp)
            if(mp.containsKey(e)) { mp.put(e,mp.get(e)+1);flag=1; }//找到关键字,取出对应的值+1

        //对输出情况进行分类
        if(flag==0) System.exit(0);//有代码,但是没有关键字
        //map.entrySet()方法返回一个key,value键值对应的集合
        List<Map.Entry<String, Integer>> list= new ArrayList<Map.Entry<String, Integer>>((Collection<? extends Map.Entry<String, Integer> >) mp.entrySet());//排序方法一:使用匿名内部类简化继承Comparator泛型接口的实现
        list.sort((o1, o2) -> o2.getKey().compareTo(o1.getKey()));

        //打印有效关键字,如果使用了法二,下面的输出不需要
        for(int i=list.size()-1;i>=0;i--){
            if(list.get(i).getValue()==0) continue;
            System.out.printf("%d\t",list.get(i).getValue());
            System.out.println(list.get(i).getKey());
        }
    }
    //最后一个测试点
    static String []special = {"\\$","_","int\\(\\)","boolean\\(\\)","double\\(\\)","float\\(\\)","byte\\(\\)","long\\(\\)","short\\(\\)","\\*"};
    public static String change(String s){
        if (s.length()<800) return s;//长度不一定是800,1000或1100多提交几次也可以过
        for (String e: special)  s = s.replaceAll(e, "JRQ");
        return  s;
    }
}
复制代码

源码分析:

统计Java程序中关键词的出现次数。我的实现步骤大致如下:

创建一个Scanner对象in,用于获取用户的输入。
定义一个字符串important,包含所有的Java关键字。
将important字符串按照两个空格的方式切割成字符串数组end。
创建一个TreeMap<String, Integer>对象mp,用于存储关键字和出现次数的映射关系。
使用for循环遍历end数组,将每个关键字作为键,初始值为0作为值,存入mp中。
定义一个字符串变量s,用于存储用户输入的每一行代码。
定义一个整型变量flag,用于检查是否有关键字出现过。
创建一个StringBuilder对象t,用于拼接多行代码。
使用while循环读取用户输入的每一行代码,直到用户输入"exit"为止。
如果当前行包含"//"注释符号,则跳过当前循环,继续读取下一行。
将当前行拼接到t对象中,用空格隔开每一行代码。
将拼接好的字符串t赋值给s。
使用正则表达式替换字符串s中的"/.../"注释块为空格。
使用正则表达式替换字符串s中的""字符串为空格。
如果s的长度为0,则输出空行并继续读取下一行。
使用正则表达式匹配字符串s中的关键字,并将匹配到的关键字替换为空格。
输出每个关键字和对应的出现次数。
相关复杂度分析:

10-1课程成绩统计程序-1

源码展示:

复制代码
import java.text.Collator;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        //初始化一个Work类,开始读入数据
        Work work = new Work();
        //读入数据
        String message = in.nextLine();
        while (!message.equals("end")){   //当message不是end的时候
            work.manage(message);   //在Work类里面处理类
            message = in.nextLine();  //读入下一行
        }
        //输出学生
        work.show_Student();
        //输出课程
        work.show_Course();
        //输出班级
        work.show_Classroom();
    }
}
class Work {
    ArrayList<Student> listStudent = new ArrayList<>();   //学生表
    ArrayList<Classroom> listClassroom = new ArrayList<>();    //班级表
    ArrayList<Course> listCourse = new ArrayList<>();   //课程表
    ArrayList<Message_Box> listMessage_Box = new ArrayList<>();   //选课记录表
    InputMatching matching = new InputMatching();
    public void manage(String message) {
        String Line[] = message.split(" " );
        switch (matching.matchingInput(message)) {
            case 1 : {   //输入的是课程
                Course course = new Course(Line[0],Line[1],Line[2]);
                if (Error_Course((course))){
                    Message_Box messageBox = new Message_Box(null,course,null);
                    if (Exist_Course(course))
                        listCourse.add(course);
                }
                else {
                    System.out.println(course.getName() + " : course type & access mode mismatch");
                }
                break;
            }
            case 2: {   //输入的是学生
                Course course = find_CourseByName(Line[2]);
                Student student = new Student(Integer.parseInt(Line[0]),Line[1]);
                Classroom classroom = new Classroom(student.getId()/100);
                if (Exist_Student(student))
                    listStudent.add(student);
                if (Exist_Classroom(classroom))
                    listClassroom.add(classroom);
                if (course!= null) {
                    if (Line.length == 4) {
                        if (course.getGrade_Way().equals("考察")){
                            Grade grade = new Grade_Exam(Line[3]);
                            Message_Box messageBox = new Message_Box(student,course,grade);
                            if (Exist_Message(messageBox))
                                listMessage_Box.add(messageBox);
                        } else {
                            System.out.println(Line[0] + " " + Line[1] + " : " +"access mode mismatch");
                        }
                    } else {
                        if (course.getGrade_Way().equals("考试")) {
                            Grade grade = new Grade_daily(Line[3],Line[4]);
                            Message_Box messageBox = new Message_Box(student,course,grade);
                            if (Exist_Message(messageBox))
                                listMessage_Box.add(messageBox);
                        } 
                        
                    }
                }
                else {
                    System.out.println(Line[2] + " " + "does not exist");
                }
                break;
            }
            default: {
                System.out.println("wrong format");
            }
        }
    }

    private boolean Exist_Message(Message_Box messageBox) {
        for(Message_Box messageBox1 : listMessage_Box){
            if ((messageBox.student.getId() == messageBox1.student.getId()) && (messageBox1.course.getName().equals(messageBox.course.getName()))){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Classroom(Classroom classroom) {
        for(Classroom classroom1 : listClassroom){
            if (classroom1.getClass_ID()==classroom.getClass_ID()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Student(Student student) {
        for (Student student1 : listStudent){
            if (student1.getId() == student.getId()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Course(Course course) {
        for (Course course1 : listCourse){
            if (course1.getName().equals(course.getName())){
                return false;
            }
        }
        return true;
    }
    private Course find_CourseByName(String name) {
        for(int i = 0 ; i < listCourse.size() ; i++) {
            if (listCourse.get(i).getName().equals((name))){
                return listCourse.get(i);
            }
        }
        return null;
    }
    private boolean Error_Course(Course course) {
        if(course.getCourse_Message().equals("必修") && course.getGrade_Way().equals("考察")){
            return false;
        }
        else {
            return true;
        }
    }
    public void show_Student() {
        //按学号排序
        Collections.sort(listStudent);
        for (int i = 0 ; i < listStudent.size() ; i++) {   //这里的循环是用来遍历每一位学生
            Student stu = listStudent.get(i);
            //从总课表listChooseCourse中获取该学生的选课记录集合
            ArrayList<Message_Box> stuCourseSelects = getStudentSelects(stu.getId());
            if (stuCourseSelects.size() != 0) {
                System.out.println(stu.getId() + " " + stu.getName() + " " + getAvgTotalScore(stuCourseSelects));
            }  else  {
                System.out.println(stu.getId() + " " + stu.getName() + " " + "did not take any exams");
            }
        }
    }
    //获得该学生选课记录的情况下返回该学生的总成绩
    private int getAvgTotalScore(ArrayList<Message_Box> stuCourseSelects) {
        int total = 0;
        for (Message_Box messageBox : stuCourseSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/stuCourseSelects.size());
    }
    //通过学生的Id查找选课记录表,返回该学生的选课记录
    private ArrayList<Message_Box> getStudentSelects(int id) {
        ArrayList<Message_Box> student_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId() == id) {
                student_Message.add(messageBox);
            }
        }
        return student_Message;
    }
    public void show_Course() {
        //按中文拼音进行排序
        Collections.sort(listCourse);
        for (int i = 0 ; i < listCourse.size() ; i++) {   //这里的循环是用来遍历每一位课程
            Course course = listCourse.get(i);
            //从总选课记录listCourse中获取该课程的学生信息集合
            ArrayList<Message_Box> courseStudentSelects = getcourseStudentSelects(course.getName());
            if (courseStudentSelects.size() != 0) {
                if (course.getGrade_Way().equals("考察")) {
                    System.out.println(course.getName() + " " + getAvgExamScore(courseStudentSelects) + " " + getAvgTotalCourse(courseStudentSelects));
                } else {
                    System.out.println(course.getName() + " " + getAvgDailyScore(courseStudentSelects) + " " + getAvgExamScore(courseStudentSelects) + " " + getAvgTotalCourse(courseStudentSelects));
                }
            } else {
                System.out.println(course.getName() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalCourse(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private int getAvgDailyScore(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += ((Grade_daily)messageBox.grade).get_DailyGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private int getAvgExamScore(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += messageBox.grade.get_ExamGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private ArrayList<Message_Box> getcourseStudentSelects(String name) {
        ArrayList<Message_Box> course_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.course.getName().equals(name)) {
                course_Message.add(messageBox);
            }
        }
        return course_Message;
    }
    public void show_Classroom() {
        Collections.sort(listClassroom);
        for (int i = 0 ; i < listClassroom.size() ; i++) {
            Classroom classroom = listClassroom.get(i);
            ArrayList<Message_Box> classroomSelects = getClassSelects(classroom.getClass_ID());
            if (classroomSelects.size() != 0) {
                System.out.println(classroom.getClass_ID() + " " + getAvgTotalClassScore(classroomSelects));
            }  else {
                System.out.println(classroom.getClass_ID() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalClassScore(ArrayList<Message_Box> classroomSelects) {
        int total = 0;
        for (Message_Box messageBox : classroomSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/classroomSelects.size());
    }
    private ArrayList<Message_Box> getClassSelects(int classId) {
        ArrayList<Message_Box> class_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId()/100 == classId) {
                class_Message.add(messageBox);
            }
        }
        return class_Message;
    }
    class Classroom implements Comparable<Classroom>{
        Classroom(int Id) {
            this.class_ID = Id;
        }
        private int class_ID;
        @Override
        public int compareTo(Classroom o) {
            return this.class_ID - o.getClass_ID();
        }
        public int getClass_ID() {
            return class_ID;
        }
    }
    class Grade_daily extends Grade{
        Grade_daily(String Daily , String Exam) {
            setDaily(Integer.parseInt(Daily));
            setExam(Integer.parseInt(Exam));
        }
        private int Daily;
        public int get_TotalGrade() {
            return (int)(this.getDaily()*0.3 + this.getExam()*0.7);
        }
        public int get_DailyGrade() {
            return this.getDaily();
        }
        public int get_ExamGrade() {
            return this.getExam();
        }
        public void setDaily(int daily) {
            Daily = daily;
        }
        public int getDaily() {
            return Daily;
        }
    }

    class Grade_Exam extends Grade{
        Grade_Exam(String Exam) {
            setExam(Integer.parseInt(Exam));
        }
        public int get_TotalGrade() {
            return (int)(this.getExam()*1.0);
        }

        public int get_ExamGrade() {
            return this.getExam();
        }
    }

    class Message_Box {
        Student student;
        Course course;
        Grade grade;
        Message_Box(Student student , Course course , Grade grade) {
            this.student = student;
            this.course = course;
            this.grade = grade;
        }
    }

class Course implements Comparable<Course>{
    Course(String name , String Course_Message , String Grade_Way) {
        this.name = name;
        this.Course_Message = Course_Message;
        this.Grade_Way = Grade_Way;
    }
    private String name;
    private String Course_Message;
    private String Grade_Way;
    @Override
    public int compareTo(Course o) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(name,o.getName());
    }
    public String getName() {
        return name;
    }
    public String getGrade_Way() {
        return Grade_Way;
    }
    public String getCourse_Message() {
        return Course_Message;
    }
}

abstract class Grade {
    private int Exam;
    public abstract int get_TotalGrade();
    public abstract int get_ExamGrade();
    public void setExam(int exam) {
        this.Exam = exam;
    }
    public int getExam() {
        return Exam;
    }
}
class Student implements Comparable<Student>{
    private int id;
    private String name;
    Student(int id , String name) {
        this.name = name;
        this.id = id;
    }
    @Override
    //学号排序
    public int compareTo(Student o) {
        return (this.id - o.getId());
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
}

    class InputMatching {
        String stuNumMatching = "[0-9]{8}";//8个0-9的数字
        String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String scoreMatching = "([1-9]?[0-9]|100)";
        String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String courseTypeMatching = "(选修|必修)";
        String checkCourseTypeMatching = "(考试|考察)";
        //courseInput用于定义课程信息模式(正则表达式)   /*这里的正则表达式考虑到了长度为二的情况吗? 这里的测试测试点似乎并不考虑这给error问题*/
        String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
        //scoreInput用于定义成绩信息模式(正则表达式)
        String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching + "(scoreMatching)?";
        String scoreInput_2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching + " " + scoreMatching;
        public int matchingInput(String s) {
            if (matchingCourse(s)) {
                return 1;
            }
            if (matchingScore(s) || s.matches(scoreInput_2)) {
                return 2;
            }
            return 0;
        }
        private boolean matchingCourse(String s) {
            return s.matches(courseInput);
        }
        private boolean matchingScore(String s) {
            //System.out.println(match);
            return s.matches(scoreInput);
        }
    }
}
复制代码

源码分析:

我实现课程成绩统计程序的具体的实现步骤如下:

  1. 创建一个Scanner对象in,用于获取用户的输入。
  2. 创建一个Work对象work,用于处理数据。
  3. 使用while循环读取用户输入的每一行数据,直到用户输入"end"为止。
  4. 将当前行数据传递给work对象的manage方法进行处理。
  5. manage方法中,将当前行数据按空格切割成字符串数组Line
  6. 使用switch语句根据输入的内容进行不同的操作。
    • 如果输入的是课程信息,创建一个Course对象,并进行一些检查和处理。如果课程信息符合要求,则将课程添加到listCourse列表中,否则输出错误信息。
    • 如果输入的是学生信息,创建一个Student对象,并进行一些检查和处理。如果学生信息符合要求,则将学生添加到listStudent列表中。
    • 如果输入的是班级信息,创建一个Classroom对象,并进行一些检查和处理。如果班级信息符合要求,则将班级添加到listClassroom列表中。
    • 如果输入的是选课记录信息,创建一个Message_Box对象,并进行一些检查和处理。将选课记录添加到listMessage_Box列表中。
  7. 循环结束后,调用work对象的show_Studentshow_Courseshow_Classroom方法分别输出学生、课程和班级的信息。

相关复杂度分析:

相关类图:

11-3 课程成绩统计程序-2

 源码呈现:

复制代码
import java.text.Collator;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //初始化一个Work类,开始读入数据
        Work work = new Work();
        //读入数据
        String message = in.nextLine();
        while (!message.equals("end")) {   //当message不是end的时候
            work.manage(message);   //在Work类里面处理类
            message = in.nextLine();  //读入下一行
        }
        //输出学生
        work.show_Student();
        //输出课程
        work.show_Course();
        //输出班级
        work.show_Classroom();
    }
}
class Work {
    ArrayList<Student> listStudent = new ArrayList<>();   //学生表
    ArrayList<Classroom> listClassroom = new ArrayList<>();    //班级表
    ArrayList<Course> listCourse = new ArrayList<>();   //课程表
    ArrayList<Message_Box> listMessage_Box = new ArrayList<>();   //选课记录表
    InputMatching matching = new InputMatching();
    public void manage(String message) {
        String Line[] = message.split(" " );
        switch (matching.matchingInput(message)) {
            case 1 : {   //输入的是课程
                Course course = new Course(Line[0],Line[1],Line[2]);
                if (Error_Course((course))){
                    Message_Box messageBox = new Message_Box(null,course,null);
                    if (Exist_Course(course))
                        listCourse.add(course);
                }
                else {
                    System.out.println(course.getName() + " : course type & access mode mismatch");
                }
                break;
            }
            case 2: {   //输入的是学生
                Course course = find_CourseByName(Line[2]);
                Student student = new Student(Integer.parseInt(Line[0]),Line[1]);
                Classroom classroom = new Classroom(student.getId()/100);
                if (Exist_Student(student))
                    listStudent.add(student);
                if (Exist_Classroom(classroom))
                    listClassroom.add(classroom);
                if (course!= null) {
                    if (Line.length == 4) {
                        if (course.getGrade_Way().equals("考察")){
                            Grade grade = new Grade_Exam(Line[3]);
                            Message_Box messageBox = new Message_Box(student,course,grade);
                            if (Exist_Message(messageBox))
                                listMessage_Box.add(messageBox);
                        } else {
                            System.out.println(Line[0] + " " + Line[1] + " : " +"access mode mismatch");
                        }
                    } else {
                        if (course.getGrade_Way().equals("考试") && Line.length == 5) {
                            Grade grade = new Grade_daily(Line[3],Line[4]);
                            Message_Box messageBox = new Message_Box(student,course,grade);
                            if (Exist_Message(messageBox))
                                listMessage_Box.add(messageBox);
                        }
                        else if (course.getGrade_Way().equals("实验") && Line.length-4 == Integer.parseInt(Line[3])) {
                            Grade grade = new Grade_Lab(Line);
                            Message_Box messageBox = new Message_Box(student,course,grade);
                            if (Exist_Message(messageBox))
                                listMessage_Box.add(messageBox);
                        }
                        else {
                            System.out.println(Line[0] + " " + Line[1] + " : " +"access mode mismatch");
                        }
                    }
                }
                else {
                    System.out.println(Line[2] + " " + "does not exist");
                }
                break;
            }
            default: {
                System.out.println("wrong format");
            }
        }
    }

    private boolean Exist_Message(Message_Box messageBox) {
        for(Message_Box messageBox1 : listMessage_Box){
            if ((messageBox.student.getId() == messageBox1.student.getId()) && (messageBox1.course.getName().equals(messageBox.course.getName()))){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Classroom(Classroom classroom) {
        for(Classroom classroom1 : listClassroom){
            if (classroom1.getClass_ID()==classroom.getClass_ID()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Student(Student student) {
        for (Student student1 : listStudent){
            if (student1.getId() == student.getId()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Course(Course course) {
        for (Course course1 : listCourse){
            if (course1.getName().equals(course.getName())){
                return false;
            }
        }
        return true;
    }
    private Course find_CourseByName(String name) {
        for (int i = 0 ; i < listCourse.size() ; i++) {
            if (listCourse.get(i).getName().equals((name))){
                return listCourse.get(i);
            }
        }
        return null;
    }
    private boolean Error_Course(Course course) {
        if (course.getCourse_Message().equals("必修") && course.getGrade_Way().equals("考察")){
            return false;
        }
        else if ((course.getCourse_Message().equals("实验") && !course.getGrade_Way().equals("实验")) || (!course.getCourse_Message().equals("实验") && course.getGrade_Way().equals("实验"))){
            return false;
        }
        else {
            return true;
        }
    }
    public void show_Student() {
        //按学号排序
        Collections.sort(listStudent);
        for (int i = 0 ; i < listStudent.size() ; i++) {   //这里的循环是用来遍历每一位学生
            Student stu = listStudent.get(i);
            //从总课表listChooseCourse中获取该学生的选课记录集合
            ArrayList<Message_Box> stuCourseSelects = getStudentSelects(stu.getId());
            if (stuCourseSelects.size() != 0) {
                System.out.println(stu.getId() + " " + stu.getName() + " " + getAvgTotalScore(stuCourseSelects));
            }  else  {
                System.out.println(stu.getId() + " " + stu.getName() + " " + "did not take any exams");
            }
        }
    }
    //获得该学生选课记录的情况下返回该学生的总成绩
    private int getAvgTotalScore(ArrayList<Message_Box> stuCourseSelects) {
        int total = 0;
        for (Message_Box messageBox : stuCourseSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/stuCourseSelects.size());
    }
    //通过学生的Id查找选课记录表,返回该学生的选课记录
    private ArrayList<Message_Box> getStudentSelects(int id) {
        ArrayList<Message_Box> student_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId() == id) {
                student_Message.add(messageBox);
            }
        }
        return student_Message;
    }
    public void show_Course() {
        //按中文拼音进行排序
        Collections.sort(listCourse);
        for (int i = 0 ; i < listCourse.size() ; i++) {   //这里的循环是用来遍历每一位课程
            Course course = listCourse.get(i);
            //从总选课记录listCourse中获取该课程的学生信息集合
            ArrayList<Message_Box> courseStudentSelects = getcourseStudentSelects(course.getName());
            if (courseStudentSelects.size() != 0) {
                if (course.getGrade_Way().equals("考察")) {
                    System.out.println(course.getName() + " " + getAvgExamScore(courseStudentSelects) + " " + getAvgTotalCourse(courseStudentSelects));
                }
                else if (course.getGrade_Way().equals("考试")){
                    System.out.println(course.getName() + " " + getAvgDailyScore(courseStudentSelects) + " " + getAvgExamScore(courseStudentSelects) + " " + getAvgTotalCourse(courseStudentSelects));
                }
                else if (course.getGrade_Way().equals("实验")) {
                    System.out.println(course.getName() + " " + getAvgTotalCourse(courseStudentSelects));
                }
            }
            else {
                System.out.println(course.getName() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalCourse(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private int getAvgDailyScore(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += ((Grade_daily)messageBox.grade).get_DailyGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private int getAvgExamScore(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += messageBox.grade.get_ExamGrade();
        }
        return (int)(1.0*total/courseStudentSelects.size());
    }
    private ArrayList<Message_Box> getcourseStudentSelects(String name) {
        ArrayList<Message_Box> course_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.course.getName().equals(name)) {
                course_Message.add(messageBox);
            }
        }
        return course_Message;
    }
    public void show_Classroom() {
        Collections.sort(listClassroom);
        for (int i = 0 ; i < listClassroom.size() ; i++) {
            Classroom classroom = listClassroom.get(i);
            ArrayList<Message_Box> classroomSelects = getClassSelects(classroom.getClass_ID());
            if (classroomSelects.size() != 0) {
                System.out.println(classroom.getClass_ID() + " " + getAvgTotalClassScore(classroomSelects));
            }  else {
                System.out.println(classroom.getClass_ID() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalClassScore(ArrayList<Message_Box> classroomSelects) {
        int total = 0;
        for (Message_Box messageBox : classroomSelects) {
            total += messageBox.grade.get_TotalGrade();
        }
        return (int)(1.0*total/classroomSelects.size());
    }
    private ArrayList<Message_Box> getClassSelects(int classId) {
        ArrayList<Message_Box> class_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId()/100 == classId) {
                class_Message.add(messageBox);
            }
        }
        return class_Message;
    }
    class InputMatching {
        String Lab_Times_Matching = "[4-9]{1}";  //1个0-9的数字
        String stuNumMatching = "[0-9]{8}";//8个0-9的数字
        String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String scoreMatching = "([1-9]?[0-9]|100)";
        String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String courseTypeMatching = "(选修|必修|实验)";
        String checkCourseTypeMatching = "(考试|考察|实验)";
        //courseInput用于定义课程信息模式(正则表达式)   /*这里的正则表达式考虑到了长度为二的情况吗? 这里的测试测试点似乎并不考虑这给error问题*/
        String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
        //scoreInput用于定义成绩信息模式(正则表达式)
        String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching + "(scoreMatching)?";
        String scoreInput_2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching + " " + scoreMatching;
        String lab_Input_0 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + Lab_Times_Matching;
        public int matchingInput(String s) {
            String[] input = s.split(" ");
            String Lab_up = "";
            if (input.length >= 5) {
                Lab_up = input[0] + " " + input[1] + " " + input[2] + " " + input[3];
            }
            if (matchingCourse(s)) {
                return 1;
            }
            if (matchingScore(s) || s.matches(scoreInput_2)) {
                return 2;
            }
            if (matching_Lab(Lab_up)){   //输入符合实验的前提条件
                int number = Integer.parseInt(input[3]);
                for (int i = 4 ; i < input.length ; i++) {   //查看后续条件是否符合
                    if (!input[i].matches(scoreMatching)) {   //如果找到一个不符合的
                        return 0;
                    }
                }
                return 2;
            }
            return 0;
        }
        private boolean matchingCourse(String s) {

            return s.matches(courseInput);
        }
        private boolean matchingScore(String s) {
            //System.out.println(match);
            return s.matches(scoreInput);
        }
        private boolean matching_Lab (String s) {
            return s.matches(lab_Input_0);
        }
    }
}
class Classroom implements Comparable<Classroom>{
    Classroom(int Id) {
        this.class_ID = Id;
    }
    private int class_ID;
    @Override
    public int compareTo(Classroom o) {
        return this.class_ID - o.getClass_ID();
    }
    public int getClass_ID() {
        return class_ID;
    }
}
class Grade_daily extends Grade {
    Grade_daily(String Daily , String Exam) {
        setDaily(Integer.parseInt(Daily));
        setExam(Integer.parseInt(Exam));
    }
    private int Daily;
    public int get_TotalGrade() {
        return (int)(this.getDaily()*0.3 + this.getExam()*0.7);
    }
    public int get_DailyGrade() {
        return this.getDaily();
    }
    public int get_ExamGrade() {
        return this.getExam();
    }
    public void setDaily(int daily) {
        Daily = daily;
    }
    public int getDaily() {
        return Daily;
    }
}


class Grade_Exam extends Grade{
    Grade_Exam(String Exam) {
        setExam(Integer.parseInt(Exam));
    }
    public int get_TotalGrade() {
        return (int)(this.getExam()*1.0);
    }

    public int get_ExamGrade() {
        return this.getExam();
    }
}
class Grade_Lab extends Grade{
    private int Laboratory = 0;
    private int times;
    Grade_Lab(String[] Inputs) {
        times=Integer.parseInt(Inputs[3]);
        for (int i = 4 ; i < Inputs.length; i++) {
            Laboratory += Integer.parseInt(Inputs[i]);
        }
    }
    @Override
    public int get_TotalGrade() {
        return (int)(1.0*Laboratory/times);
    }

    @Override
    public int get_ExamGrade() {
        return 0;
    }
}

class Message_Box {
    Student student;
    Course course;
    Grade grade;
    Message_Box(Student student , Course course , Grade grade) {
        this.student = student;
        this.course = course;
        this.grade = grade;
    }
}

class Course implements Comparable<Course>{
    Course(String name , String Course_Message , String Grade_Way) {
        this.name = name;
        this.Course_Message = Course_Message;
        this.Grade_Way = Grade_Way;
    }
    private String name;
    private String Course_Message;
    private String Grade_Way;
    @Override
    public int compareTo(Course o) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(name,o.getName());
    }
    public String getName() {
        return name;
    }
    public String getGrade_Way() {
        return Grade_Way;
    }
    public String getCourse_Message() {
        return Course_Message;
    }
}



abstract class Grade {
    private int Exam;
    public abstract int get_TotalGrade();
    public abstract int get_ExamGrade();
    public void setExam(int exam) {
        this.Exam = exam;
    }
    public int getExam() {
        return Exam;
    }
}
class Student implements Comparable<Student>{
    private int id;
    private String name;
    Student(int id , String name) {
        this.name = name;
        this.id = id;
    }
    @Override
    //学号排序
    public int compareTo(Student o) {
        return (this.id - o.getId());
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
}源码分析:
复制代码

以下是我代码的具体功能解析:
`Work` 类用于处理所有的主要操作。该类中有四个主要的数据结构,包括学生列表(`listStudent`),班级列表(`listClassroom`),课程列表(`listCourse`),以及选课记录列表(`listMessage_Box`)。
`manage(String message)` 方法用于处理输入的信息。该方法首先使用字符串分割函数将输入的信息分割为不同的部分,然后根据输入的内容执行不同的操作。

 输入的信息可以是关于课程的,关于学生的,或者其他。如果输入的信息是关于课程的,那么将会创建一个新的课程对象并将其添加到课程列表中。如果输入的信息是关于学生的,那么将会创建一个新的学生对象,班级对象,并且可能会创建一个新的选课记录,这些对象可能会被添加到各自的列表中。

`show_Student()`,`show_Course()`,`show_Classroom()` 这几个方法用于输出学生,课程和班级信息。尤其在`show_Student()`方法中,首先对学生列表进行排序,然后遍历每一个学生,获取每个学生的选课记录,并计算每个学生的平均分数。

 还有几个私有方法,例如`Exist_Message()`, `Exist_Classroom()`, `Exist_Student()`, `Exist_Course()`等,这些方法被用来检查某个对象是否已经存在于其相应的列表中。此外,还有`find_CourseByName()`, `Error_Course()`这样的方法,分别用于通过课程名查找课程,以及检查课程信息是否正确。
还有Student`, `Classroom`, `Course`, `Message_Box等类的实现,其较为简单,故不在此进行赘述

类图实现:

相关复杂度分析:

12-1 课程成绩统计程序-3

要求:

  • 修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。
  • 完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。

源码呈现:

复制代码
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //初始化一个Work类,开始读入数据
        Work work = new Work();
        //读入数据
        String message = in.nextLine();
        while (!message.equals("end")) {   //当message不是end的时候
            work.manage(message);   //在Work类里面处理类
            message = in.nextLine();  //读入下一行
        }
        //输出学生
        work.show_Student();
        //输出课程
        work.show_Course();
        //输出班级
        work.show_Classroom();
    }
}
class Work {
    ArrayList<Student> listStudent = new ArrayList<>();   //学生表
    ArrayList<Classroom> listClassroom = new ArrayList<>();    //班级表
    ArrayList<Course> listCourse = new ArrayList<>();   //课程表
    ArrayList<Message_Box> listMessage_Box = new ArrayList<>();   //选课记录表
    InputMatching matching = new InputMatching();
    public void manage(String message) {
        String Line[] = message.split(" " );
        switch (matching.matchingInput(message)) {
            case 1 : {   //输入的是课程
                Course course = new Course(Line[0],Line[1],Line[2]);
                if (Error_Course((course))) { //性质没有问题
                    if (Exist_Course(course)) { //是否存在,应该放在最后???
                        if (Weight_error(Line)) {  //是否权重数量对不上
                            if (Weight_sum_error(Line)) {//是否权重数量和==1
                                if (Line[2].equals("实验")) {
                                    for (int i = 4; i < Line.length; i++) {
                                        course.addWeight(Double.parseDouble(Line[i]));
                                    }
                                } else if (Line[2].equals("考试")) {
                                    course.addWeight(Double.parseDouble(Line[3]));
                                    course.addWeight(Double.parseDouble(Line[4]));
                                } else {
                                    course.addWeight(1);
                                }
                                listCourse.add(course);
                            } else {
                                System.out.println(Line[0] + " : weight value error");
                            }
                        } else {
                            System.out.println(Line[0] + " : number of scores does not match");
                        }
                    }
                }
                else {
                    System.out.println(course.getName() + " : course type & access mode mismatch");
                }
                break;
            }
            case 2: {   //输入的是学生
                boolean add = false;
                Course course = find_CourseByName(Line[2]);
                Student student = new Student(Integer.parseInt(Line[0]),Line[1]);
                Classroom classroom = new Classroom(student.getId()/100);
                if (Exist_Student(student)){
                    listStudent.add(student);
                    add = true;
                }
                if (Exist_Classroom(classroom) && add)
                    listClassroom.add(classroom);
                if (course!= null) {
                    if (Line.length == 4) {
                        if (course.getGrade_Way().equals("考察")){
                            Grade_Course grade_course = new Grade_Course(course,Line);
                            Message_Box messageBox = new Message_Box(student,course,grade_course);
                            if (Exist_Message(messageBox) && add)
                                listMessage_Box.add(messageBox);
                        } else {
                            System.out.println(Line[0] + " " + Line[1] + " : " +"access mode mismatch");
                        }
                    } else {
                        if (course.getGrade_Way().equals("考试") && Line.length == 5) {
                            Grade_Course grade_course = new Grade_Course(course,Line);
                            Message_Box messageBox = new Message_Box(student,course,grade_course);
                            if (Exist_Message(messageBox) && add)
                                listMessage_Box.add(messageBox);
                        }
                        else if (course.getGrade_Way().equals("实验") && Line.length-3 == course.get_Weight_size()) {
                            Grade_Course grade_course = new Grade_Course(course,Line);
                            Message_Box messageBox = new Message_Box(student,course,grade_course);
                            if (Exist_Message(messageBox) && add)
                                listMessage_Box.add(messageBox);
                        }
                        else {
                            System.out.println(Line[0] + " " + Line[1] + " : " +"access mode mismatch");
                        }
                    }
                }
                else {
                    System.out.println(Line[2] + " " + "does not exist");
                }
                break;
            }
            default: {
                System.out.println("wrong format");
            }
        }
    }

    private boolean Exist_Message(Message_Box messageBox) {
        for(Message_Box messageBox1 : listMessage_Box){
            if ((messageBox.student.getId() == messageBox1.student.getId()) && (messageBox1.course.getName().equals(messageBox.course.getName()))){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Classroom(Classroom classroom) {
        for(Classroom classroom1 : listClassroom){
            if (classroom1.getClass_ID()==classroom.getClass_ID()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Student(Student student) {
        for (Student student1 : listStudent){
            if (student1.getId() == student.getId()){
                return false;
            }
        }
        return true;
    }
    private boolean Exist_Course(Course course) {
        for (Course course1 : listCourse){
            if (course1.getName().equals(course.getName())){
                return false;
            }
        }
        return true;
    }
    private boolean Weight_error(String[] Line) {
        if (Line[2].equals("实验")) {
            int number = Integer.parseInt(Line[3]);
            if (Line.length - 4 == number)
                return true;
            else
                return false;
        } else if (Line[2].equals("考试")){
            if (Line.length - 3 == 2)
                return true;
            else
                return false;
        } else {
            return true;
        }
    }
    private boolean Weight_sum_error(String[] Line){
        if (Line[2].equals("实验")) {
            double total = 0;
            int number = Integer.parseInt(Line[3]);
            for (int i = 4 ; i < number + 4 ; i++) {
                total += Double.parseDouble(Line[i]);
            }
            if (total==1)
                return true;
            else
                return false;
        } else if (Line[2].equals("考试")) {
            double total = Double.parseDouble(Line[3]) + Double.parseDouble(Line[4]);
            if (total==1)
                return true;
            else
                return false;
        } else {
            return true;
        }
    }
    private Course find_CourseByName(String name) {
        for (int i = 0 ; i < listCourse.size() ; i++) {
            if (listCourse.get(i).getName().equals((name))){
                return listCourse.get(i);
            }
        }
        return null;
    }
    private boolean Error_Course(Course course) {
        if (course.getCourse_Message().equals("必修") && course.getGrade_Way().equals("考察")){
            return false;
        }
        else if ((course.getCourse_Message().equals("实验") && !course.getGrade_Way().equals("实验")) || (!course.getCourse_Message().equals("实验") && course.getGrade_Way().equals("实验"))){
            return false;
        }
        else {
            return true;
        }
    }
    public void show_Student() {
        //按学号排序
        Collections.sort(listStudent);
        for (int i = 0 ; i < listStudent.size() ; i++) {   //这里的循环是用来遍历每一位学生
            Student stu = listStudent.get(i);
            //从总课表listChooseCourse中获取该学生的选课记录集合
            ArrayList<Message_Box> stuCourseSelects = getStudentSelects(stu.getId());
            if (stuCourseSelects.size() != 0) {
                System.out.println(stu.getId() + " " + stu.getName() + " " + getAvgTotalScore(stuCourseSelects));
            }  else  {
                System.out.println(stu.getId() + " " + stu.getName() + " " + "did not take any exams");
            }
        }
    }
    //获得该学生选课记录的情况下返回该学生的总成绩
    private int getAvgTotalScore(ArrayList<Message_Box> stuCourseSelects) {
        int total = 0;
        for (Message_Box messageBox : stuCourseSelects) {
            total += messageBox.grade_course.get_grade_Total();
        }
        return (int)(total/stuCourseSelects.size());
    }
    //通过学生的Id查找选课记录表,返回该学生的选课记录
    private ArrayList<Message_Box> getStudentSelects(int id) {
        ArrayList<Message_Box> student_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId() == id) {
                student_Message.add(messageBox);
            }
        }
        return student_Message;
    }
    public void show_Course() {
        //按中文拼音进行排序
        Collections.sort(listCourse);
        for (int i = 0 ; i < listCourse.size() ; i++) {   //这里的循环是用来遍历每一位课程
            Course course = listCourse.get(i);
            //从总选课记录listCourse中获取该课程的学生信息集合
            ArrayList<Message_Box> courseStudentSelects = getcourseStudentSelects(course.getName());
            if (courseStudentSelects.size() != 0) {
                System.out.println(course.getName() + " " + getAvgTotalCourse(courseStudentSelects));
            }
            else {
                System.out.println(course.getName() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalCourse(ArrayList<Message_Box> courseStudentSelects) {
        int total = 0;
        for (Message_Box messageBox : courseStudentSelects) {
            total += messageBox.grade_course.get_grade_Total();
        }
        return (int)(total/courseStudentSelects.size());
    }
    private ArrayList<Message_Box> getcourseStudentSelects(String name) {
        ArrayList<Message_Box> course_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.course.getName().equals(name)) {
                course_Message.add(messageBox);
            }
        }
        return course_Message;
    }
    public void show_Classroom() {
        Collections.sort(listClassroom);
        for (int i = 0 ; i < listClassroom.size() ; i++) {
            Classroom classroom = listClassroom.get(i);
            ArrayList<Message_Box> classroomSelects = getClassSelects(classroom.getClass_ID());
            if (classroomSelects.size() != 0) {
                System.out.println(classroom.getClass_ID() + " " + getAvgTotalClassScore(classroomSelects));
            }  else {
                System.out.println(classroom.getClass_ID() + " " + "has no grades yet");
            }
        }
    }
    private int getAvgTotalClassScore(ArrayList<Message_Box> classroomSelects) {
        int total = 0;
        for (Message_Box messageBox : classroomSelects) {
            total += messageBox.grade_course.get_grade_Total();
        }
        return (int)(total/classroomSelects.size());
    }
    private ArrayList<Message_Box> getClassSelects(int classId) {
        ArrayList<Message_Box> class_Message = new ArrayList<>();
        for (Message_Box messageBox : listMessage_Box) {
            if (messageBox.student.getId()/100 == classId) {
                class_Message.add(messageBox);
            }
        }
        return class_Message;
    }
    class InputMatching {
        String Lab_Times_Matching = "[4-9]{1}";  //1个4-9的数字
        String stuNumMatching = "[0-9]{8}";//8个0-9的数字
        String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String scoreMatching = "([1-9]?[0-9]|100)";
        String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
        String courseTypeMatching = "(选修|必修|实验)";
        String checkCourseTypeMatching = "(考试|考察|实验)";
        String rate = "^(0(\\.\\d+)?|1(\\.0+)?)$";
        //courseInput用于定义课程信息模式(正则表达式)   /*这里的正则表达式考虑到了长度为二的情况吗? 这里的测试测试点似乎并不考虑这给error问题*/
        String courseInput_1 = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
        String courseInput_2 = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching + " " + rate + " " + rate;
        String courseInput_3 = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching + " " + Lab_Times_Matching;
        //scoreInput用于定义成绩信息模式(正则表达式)
        String scoreInput_1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching;
        String scoreInput_2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
                scoreMatching + " " + scoreMatching;
        String scoreInput_3 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching;
        public int matchingInput(String s) {
            String[] input = s.split(" ");
            String Lab_up = "";
            String Lab_down = "";
            if (input.length >= 4) {//防止报错
                Lab_up = input[0] + " " + input[1] + " " + input[2] + " " + input[3];
                Lab_down = input[0] + " " + input[1] + " " + input[2];
            }
            if (matchingCourse(s)) {//课程之考试、考察匹配
                return 1;
            }
            if (matching_Lab(Lab_up)){//课程之实验匹配
                for (int i = 4 ; i < input.length ; i++) {//查看后续条件是否符合
                    if (!input[i].matches(rate)) {//如果找到一个不符合的
                        return 0;
                    }
                }
                return 1;
            }
            if (matchingScore(s)) {//成绩之考试、考察
                return 2;
            }
            if (matching_Score_Lab(Lab_down)) {//成绩之实验
                for (int i = 4 ; i < input.length ; i++) {//查看后续条件是否符合
                    if (!input[i].matches(scoreMatching)) {//如果找到一个不符合的
                        return 0;
                    }
                }
                return 2;
            }
            return 0;
        }
        private boolean matchingCourse(String s) {
            String[] ppp = s.split(" ");
            if (ppp.length==5){
                if (ppp[0].matches(courseNameMatching) && ppp[1].matches(courseTypeMatching) && ppp[2].matches(checkCourseTypeMatching) && ppp[3].matches(rate) && ppp[4].matches(rate)){
                    return true;
                }
            }
            if (s.matches(courseInput_1) || s.matches(courseInput_2))
                return true;
            else
                return false;
        }
        private boolean matchingScore(String s) {
            if (s.matches(scoreInput_1) || s.matches(scoreInput_2))
                return true;
            else
                return false;
        }
        private boolean matching_Lab (String s) {
            return s.matches(courseInput_3);
        }
        private boolean matching_Score_Lab(String s) {
            return s.matches(scoreInput_3);
        }
    }
}
class Message_Box {
    Student student;
    Course course;
    //    Grade grade;
    Grade_Course grade_course;
    Message_Box(Student student , Course course , Grade_Course grade_course) {
        this.student = student;
        this.course = course;
        this.grade_course = grade_course;
    }
}

class Classroom implements Comparable<Classroom>{
    Classroom(int Id) {
        this.class_ID = Id;
    }
    private int class_ID;
    @Override
    public int compareTo(Classroom o) {
        return this.class_ID - o.getClass_ID();
    }
    public int getClass_ID() {
        return class_ID;
    }
}

class Student implements Comparable<Student>{
    private int id;
    private String name;
    Student(int id , String name) {
        this.name = name;
        this.id = id;
    }
    @Override
    //学号排序
    public int compareTo(Student o) {
        return (this.id - o.getId());
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
}
class Course implements Comparable<Course>{
    Course(String name , String Course_Message , String Grade_Way) {
        this.name = name;
        this.Course_Message = Course_Message;
        this.Grade_Way = Grade_Way;
    }
    private String name;
    private String Course_Message;
    private String Grade_Way;
    private ArrayList<Double> Weight = new ArrayList<>();
    @Override
    public int compareTo(Course o) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(name,o.getName());
    }
    public String getName() {
        return name;
    }
    public String getGrade_Way() {
        return Grade_Way;
    }
    public String getCourse_Message() {
        return Course_Message;
    }
    public void addWeight(double weight) {
        Weight.add(weight);
    }
    public double getWeight(int i) {
        return Weight.get(i);
    }
    public int get_Weight_size(){
        return Weight.size();
    }
}
class Grade_Course {
    ArrayList<Grade_itemize> grade_itemizes = new ArrayList<>();

    Grade_Course(Course course, String[] Line) {
        int num = course.get_Weight_size();
        if (num == 1) {
            grade_itemizes.add(new Grade_itemize(Integer.parseInt(Line[3]), course.getWeight(0)));
        } else if (num == 2) {
            for (int i = 0; i < num; i++) {
                grade_itemizes.add(new Grade_itemize(Integer.parseInt(Line[i + 3]), course.getWeight(i)));
            }
        } else if (num >= 4 && num <= 9) {
            for (int i = 0; i < num; i++) {
                grade_itemizes.add(new Grade_itemize(Integer.parseInt(Line[i + 3]), course.getWeight(i)));
            }
        }
    }

    public int get_grade_Total() {
        double total = 0;
        for (Grade_itemize gradeItemize : grade_itemizes) {
            total += gradeItemize.get_grade_One();
        }
        return (int) (total);
    }

    class Grade_itemize {
        private int score;
        private double date;

        Grade_itemize(int score, double date) {
            this.score = score;
            this.date = date;
        }

        public double get_grade_One() {
            return score * date;
        }
    }
}
复制代码

源码分析:

该源码的功能与之前相似,但这里根据题目需求,还引入了更复杂的课程处理。

1. 在`main`函数中,程序会一直读取并处理用户的输入,直到输入的字符串为"end"为止。处理完成后,它会依次打印学生信息、课程信息以及班级信息。

2. `manage`函数负责处理输入的信息,它依据输入的不同内容选择不同的处理方式:

当输入的是课程信息时,程序会创建一个新的课程,对课程的性质进行检查,确保课程不存在于课程列表中,并确保课程的权重设置正确(通过`Weight_error`和`Weight_sum_error`函数)。如果满足所有条件,就会把这个课程添加到课程列表中。

当输入的是学生信息时,程序会找到学生选的课程,然后创建一个新的学生和班级。如果这个学生和班级不存在于对应的列表中,就把他们添加进去。然后会根据选课的类型和提供的成绩信息创建一个新的`Grade_Course`对象,并将此选课记录添加到选课记录表中。

3. `Exist_Message`, `Exist_Classroom`, `Exist_Student`和`Exist_Course`这几个函数都是用来检查对象是否已经存在于对应的列表中。

4. `Weight_error`和`Weight_sum_error`函数用来检查课程的权重设置是否正确。如果权重的设置与课程的类型不符或者所有权重的和不等于1,那么就返回false。

5. Student、Classroom、Course、Message_Box、InputMatching、Grade_Course等较为简单,不做过多赘述。

相关复杂度分析:

类图实现:

 

不足:

  1. 方法太长:方法看起来相对较长,这使得代码的维护和理解变得困难。应该将代码逻辑拆分成更小的方法。

  2. 嵌套逻辑多:代码中嵌套了多个if-else语句和for循环,导致代码的可读性较差。应该考虑重构逻辑,以减少嵌套深度。

  3. 代码重复:代码中出现了一些重复的逻辑,应该考虑将重复的逻辑抽取出来,以提高代码的可维护性。

三:作业总结与代码改进

通过后来和其他同学的对比发现,我的代码的效率和性能很差,编码时只关注对题目的解答是否正确,没有进行一些算法和逻辑的使用,代码紊乱,多有无效代码。

下面给出对课程系统代码的改进(来源与其他同学交流)

复制代码
import java.util.*;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashSet<CourseSelection> courseSelectionHashSet = new HashSet<>();
        HashSet<String> courseNameSet = new HashSet<>();
        HashMap<Student, Boolean> studentScorehashMap = new HashMap<>();
        TreeMap<Student, Integer> averageStudentScore = new TreeMap<>();
        TreeMap<Course, Integer> courseUsualAverage = new TreeMap<>();
        TreeMap<Course, Integer> courseFinalAverage = new TreeMap<>();
        TreeMap<Course, Integer> courseCombinedAverage = new TreeMap<>();
        TreeMap<String, Integer> classCombinedAverage = new TreeMap<>();
        TreeMap<Course, ArrayList<Float>> scoreWeight = new TreeMap<>();
        StringBuilder stringBuilder = new StringBuilder();
 while (true) {
            String temp = scanner.nextLine();
            if (temp.equals("end")) {
                break;
            }
            String[] s = temp.split(" ");
            if (s[0].matches("[0-9]{0,100}")) {
                Student student = new Student(s[0], s[1]);
                Course course = null;
                Grade grade = new ExamScore();
                ArrayList<Float>weight=new ArrayList<>();
                if (s.length >= 5) {
                    int flag = 0;
                    for (int i = 4; i < s.length; i++) {
                        if (!s[i].matches("[0-9]{1,3}")) {
                            System.out.println("wrong format");
                            flag = 1;
                            break;
                        } else if (Integer.parseInt(s[i]) < 0 || Integer.parseInt(s[i]) > 100) {
                            System.out.println("wrong format");
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1)
                        continue;
                }
                if (s[0].length() != 8 || s[1].length() > 10 || s[2].length() > 10 || !s[3].matches("[0-9]{1,3}") || (Integer.parseInt(s[3]) < 0 || Integer.parseInt(s[3]) > 100)) {
                    System.out.println("wrong format");
                    continue;
                }
                if (courseNameSet.contains(s[2])) {
                    for (Course tempCourse : courseUsualAverage.keySet()) {
                        if (tempCourse.name.equals(s[2])) {
                            grade.weight = scoreWeight.get(tempCourse);
                            weight=grade.weight;
                            course = tempCourse;
                            break;
                        }
                    }
                }
                if (s.length == 5 && ((Integer.parseInt(s[4]) < 0) || (Integer.parseInt(s[4]) > 100))) {
                    System.out.println("wrong format");
                    continue;
                } else if (course != null && course.mode == 2) {
                    if(course.mode!=2&&s.length>5){
                        System.out.println("wrong format");
                        continue;
                    }
                    if (s.length != grade.weight.size() + 3) {
                        System.out.println(s[0] + " " + s[1] + " " + ": access mode mismatch");
                        classCombinedAverage.put(s[0].substring(0, 6), 0);
                        averageStudentScore.put(student, 0);
                        continue;
                    }
                }
                if (!courseNameSet.contains(s[2])) {
                    System.out.println(s[2] + " does not exist");
                    averageStudentScore.put(student, 0);
                    classCombinedAverage.put(s[0].substring(0, 6), 0);
                    continue;
                }
                if (s.length == 4 && course.mode == 1) {
                    grade = new ExamineGrade();
                    grade.weight=weight;
                    grade.finalGrade = Integer.parseInt(s[3]);
                } else if (s.length == 5 && course.mode == 0) {
                    grade = new ExamScore();
                    grade.weight=weight;
                    grade.usualGrade = Integer.parseInt(s[3]);
                    grade.finalGrade = Integer.parseInt(s[4]);
                } else if (course.mode == 2) {
                    grade = new ExperimentalScore();
                    grade.weight=weight;
                    for (int i = 3; i < s.length; i++) {
                        grade.experimentalGrades.add(Integer.parseInt(s[i]));
                    }
                }
                classCombinedAverage.put(s[0].substring(0, 6), 0);
                if ((s.length != 5 && course.mode == 0) || (s.length != 4 && course.mode == 1)) {
                    System.out.println(s[0] + " " + s[1] + " " + ": access mode mismatch");
                    averageStudentScore.put(student, 0);
                    continue;
                }

                //获取每个同学,以便计算平均分
                averageStudentScore.put(student, 0);
                CourseSelection courseSelection = new CourseSelection(course, student, grade);
                courseSelectionHashSet.add(courseSelection);
            }  else {
                 if(s.length>=5&&s[2].equals("考试")&&(!s[3].matches("0\\.[0-9]{1,2}")||!s[4].matches("0\\.[0-9]{1,2}"))){
                    System.out.println("wrong format");
                    continue;
                }
                if ((s[0].length() > 10 || ((!s[1].equals("必修") && !s[1].equals("选修") && !s[1].equals("实验")) || (!s[2].equals("考试") && !s[2].equals("考察") && !s[2].equals("实验"))))) {
                    System.out.println("wrong format");
                    continue;
                } else if ((s.length == 2) && (s[0].length() > 10 || !s[1].equals("必修"))) {
                    System.out.println("wrong format");
                    continue;
                }
                int type = 0;
                ArrayList<Float> arrayList = new ArrayList<>();
                if (s[1].equals("必修")) type = 0;
                if (s[1].equals("选修")) type = 1;
                if (s[1].equals("实验")) type = 2;
                Course course = new Course(s[0], type);
                int mode = 0;
                if (s[2].equals("考试")) mode = 0;
                if (s[2].equals("考察")) mode = 1;
                if (s[2].equals("实验")) mode = 2;
                course.mode = mode;
                if ((course.type == 0 && course.mode == 1) || (course.mode == 2 && course.type != 2) || course.type == 2 && course.mode != 2) {
                    System.out.println(s[0] + " : course type & access mode mismatch");
                    continue;
                }
                if (s[1].equals("实验") && !s[3].matches("[4-9]")) {
                    System.out.println("wrong format");
                    continue;
                } else if (s[1].equals("实验") && Integer.parseInt(s[3]) + 4 != s.length) {
                    System.out.println(course.name + " : number of scores does not match");
                    continue;
                } else if ((s[2].equals("考试"))&&(Math.abs (Float.parseFloat(s[3])+Float.parseFloat(s[4])- 1)>0.01)) {
                        System.out.println(course.name + " : weight value error");
                        continue;
                } else if (s[1].equals("实验")) {
                    int n = Integer.parseInt(s[3]);
                    float sum = 0;
                    for (int i = 0; i < n; i++) {
                        float tempFloat = Float.parseFloat(s[i + 4]);
                        sum += tempFloat;
                        arrayList.add(tempFloat);
                    }
                    if (Math.abs(sum-1)>0.01) {
                        System.out.println(course.name + " : weight value error");
                        continue;
                    }
                } else if (s[2].equals("考试")) {
                    arrayList.add(Float.parseFloat(s[3]));
                    arrayList.add(Float.parseFloat(s[4]));
                }
                if(courseNameSet.contains(course.name)){
                    continue;
                }
                scoreWeight.put(course, arrayList);
                courseUsualAverage.put(course, 0);
                courseFinalAverage.put(course, 0);
                courseCombinedAverage.put(course, 0);
                courseNameSet.add(s[0]);
            }
        }
        //courseSelectionHashSet.stream().map(s->s.course.name).forEach(System.out::println);
        //1.求每个同学平均成绩,排序打印
        for (Student student : averageStudentScore.keySet()) {
            List<Integer> list = new ArrayList<>();
            for (CourseSelection courseSelection : courseSelectionHashSet) {
                if (courseSelection.student.id.equals(student.id)) {
                    list.add(courseSelection.grade.getGrade());
                }
            }
            //List<Integer> list = courseSelectionHashSet.stream().filter(s -> s.student.id.equals(student.id)).map(s -> s.grade.getGrade()).toList();
            for (Integer tempScore : list) {
                averageStudentScore.replace(student, averageStudentScore.get(student) + tempScore);
            }
            if (list.size() != 0)
                averageStudentScore.replace(student, averageStudentScore.get(student) / list.size());
            if (list.size() == 0) {
                studentScorehashMap.put(student, false);
                System.out.println(student.id + " " + student.name + " " + "did not take any exams");
            } else {
                studentScorehashMap.put(student, true);
                System.out.println(student.id + " " + student.name + " " + averageStudentScore.get(student));
            }
        }
        //2.计算每门课程的平均 平时分,期末分,综合分
        for (Course course : courseUsualAverage.keySet()) {
            List<Integer> list = new ArrayList<>();
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            //平时分
            for (CourseSelection courseSelection : courseSelectionHashSet) {
                if (courseSelection.course.name.equals(course.name)) {
                    list.add(courseSelection.grade.usualGrade);
                }
            }
            //list = courseSelectionHashSet.stream().filter(s -> s.course.name.equals(course.name)).map(s -> s.grade.usualGrade).toList();
            //期末分
            for (CourseSelection courseSelection : courseSelectionHashSet) {
                if (courseSelection.course.name.equals(course.name)) {
                    list1.add(courseSelection.grade.finalGrade);
                }
            }
            //list1 = courseSelectionHashSet.stream().filter(s -> s.course.name.equals(course.name)).map(s -> s.grade.finalGrade).toList();
            //综合分
            for (CourseSelection courseSelection : courseSelectionHashSet) {
                if (courseSelection.course.name.equals(course.name)) {
                    list2.add(courseSelection.grade.getGrade());
                }
            }
            //list2 = courseSelectionHashSet.stream().filter(s -> s.course.name.equals(course.name)).map(s -> s.grade.getGrade()).toList();
            for (Integer tempScore : list) {
                courseUsualAverage.replace(course, courseUsualAverage.get(course) + tempScore);
            }
            for (Integer tempScore : list1) {
                courseFinalAverage.replace(course, courseFinalAverage.get(course) + tempScore);
            }
            for (Integer tempScore : list2) {
                courseCombinedAverage.replace(course, courseCombinedAverage.get(course) + tempScore);
            }
            if (list.size() != 0)
                courseUsualAverage.replace(course, courseUsualAverage.get(course) / list.size());
            if (list1.size() != 0)
                courseFinalAverage.replace(course, courseFinalAverage.get(course) / list1.size());
            if (list2.size() != 0)
                courseCombinedAverage.replace(course, courseCombinedAverage.get(course) / list2.size());
            if (list.size() == 0) {
                System.out.println(course.name + " has no grades yet");
            } else {
                System.out.println(course.name + " " + courseCombinedAverage.get(course));
            }
        }
        //3.计算班级的分数
        for (String classId : classCombinedAverage.keySet()) {
            int sum = 0;//人数
            boolean flag = false;//班级是否有分数记录
            for (Student student : averageStudentScore.keySet()) {
                if (student.id.substring(0, 6).equals(classId)) {
                    classCombinedAverage.replace(classId, classCombinedAverage.get(classId) + averageStudentScore.get(student));
                    if (studentScorehashMap.get(student)) {
                        flag = true;
                        sum++;
                    }
                }
            }
            if (sum != 0)
                classCombinedAverage.replace(classId, classCombinedAverage.get(classId) / sum);
            if (flag) {
                System.out.println(classId + " " + classCombinedAverage.get(classId));
            } else {
                System.out.println(classId + " " + "has no grades yet");
            }
        }
    }
}
class Course implements Comparable<Course>{
    protected String name;
    protected int type;//0为必修,1为选修,2为实验
    protected int mode=0;//0为考试,1为考察,2为实验
    public Course(String name, int type, int mode) {
        this.name = name;
        this.type = type;
        this.mode = mode;
    }
    public Course(String name){
        this.name=name ;
    };
    public Course(String name,int type){
        this.name=name;
        this.type=type;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Course course = (Course) o;
        return Objects.equals(name, course.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public int compareTo(Course o) {
        return Collator.getInstance(Locale.CHINA).compare(this.name, o.name);
    }
}
class CourseSelection {
    protected Course course;
    protected Student student;
    protected Grade grade;

    public CourseSelection(Course course, Student student, Grade grade) {
        this.course = course;
        this.student = student;
        this.grade = grade;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CourseSelection that = (CourseSelection) o;
        return Objects.equals(course, that.course) && Objects.equals(student, that.student);
    }
    @Override
    public int hashCode() {
        return Objects.hash(course, student);
    }

}
class ExamineGrade extends Grade{
    @Override
    Integer getGrade() {
        return this.finalGrade;
    }
    public ExamineGrade(){
        type=1;
    }
}
class ExamScore extends Grade{
    @Override
    Integer getGrade() {
        if(weight.size()!=0)
            return (int)( this.usualGrade* weight.get(0)+this.finalGrade*weight.get(1));
        return (int)( this.usualGrade* 0.3+this.finalGrade*0.7);
    }
    public ExamScore(){
        type=0;
    }
}
class ExperimentalScore extends Grade{
    Integer getGrade(){
        float result=0;
        for(int i=0;i<experimentalGrades.size();i++){
            result+=experimentalGrades.get(i)* weight.get(i);
        }
        return (int)result;
    }
}
abstract class Grade {
    protected Integer finalGrade=0;
    protected int type;
    protected Integer usualGrade=0;
    protected ArrayList<Integer>experimentalGrades=new ArrayList<>();
    protected ArrayList<Float>weight=new ArrayList<>();
    abstract Integer getGrade();
}
class Student implements Comparable<Student>{
    protected String id;
    protected String name;

    public Student() {
    }

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Student s) {
        return this.id.compareTo(s.id);
    }
}
复制代码

此代码更为简洁,运用了集合的高阶技巧等,应该反复品味,增强自己代码的可读性和可行性。

四:总结与一些感想

一些感想:

  • 学会了诸多JAVA基础知识,如泛型、正则表达式、冒号用法、迭代器遍历等
  • 学会了集合进阶的相关内容,如treemap,hashmap等

  • 了解了二叉树、红黑树等

  • 能编写简单的系统去体现自己的代码熟练度和对代码的理解

提升:

  • 要学会读题,学会自己建立代码的思维逻辑,学会自己独立解决代码的相关问题而不是一遇到问题就上网查。
  • 需要对Java的24个设计模式有更深入的理解和使用,避免写出杂乱无章的代码。

posted on   HorizonJ  阅读(14)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css
点击右上角即可分享
微信分享提示