import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.text.Collator; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s_record = s.nextLine(); ParseInput handle = new ParseInput(); while (!s_record.equals("end")) { handle.parseInput(s_record); s_record = s.nextLine(); } handle.showStudents(); handle.showCourses(); handle.showClasses(); } } class Class implements Comparable{ String num; int averg; Student stu; Class(String num,int averg) { this.averg = averg; this.num = num; } Class(String num) { this.num = num; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public int getAverg() { return averg; } public void setAverg(int averg) { this.averg = averg; } public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof Class){ Class goods = (Class) o; if(this.num.compareTo(goods.num)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } } class Course implements Comparable{ String type; String name; String bcho; int ccave; int nnave; int ffave; Course(String name,String type,String bcho) { this.bcho = bcho; this.name = name; this.type = type; } public String getBcho() { return bcho; } public void setBcho(String bcho) { this.bcho = bcho; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int compareTo(Object o) { if(o instanceof Course){ Course goods = (Course) o; Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(name,goods.name); } throw new RuntimeException("传入的数据类型不一致"); } public int compareTo(Course o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(name,o.getName()); } } 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 courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修|实验)"; static String checkcourseTypeMatching = "(考试|考察|实验)"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; 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) { //System.out.println(match); return s.matches(scoreInput); } } class ParseInput { InputMatching im; ArrayList<Course> classlist = new ArrayList<>(); ArrayList<Student> stulist = new ArrayList<>(); ArrayList<Class> clist = new ArrayList<>(); ArrayList<St> ss = new ArrayList<>(); public void parseInput(String s_record) { // TODO Auto-generated method stub String[] swd = s_record.split(" "); if (im.matchingInput(s_record) == 1) { String[] a = s_record.split(" "); if (a[1].equals("必修") == true && a[2].equals("考察") == true) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } if(a[1].equals("实验")==true&&a[2].equals("实验")==false) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } if(a[2].equals("实验")==true&&a[1].equals("实验")==false) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } Course cls = new Course(a[0], a[1], a[2]); if (judecls(classlist, a[0]) == null) { classlist.add(cls); } } if(im.matchingInput(s_record) !=1&&swd.length<=5) { String[] a = s_record.split(" "); if (a.length == 4) { Course clss; clss = judecls(classlist, a[2]); if (clss == null) { System.out.println(a[2] + " does not exist"); dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { if (clss.bcho.equals("考试") == true) { System.out.println(a[0] + " " + a[1] + " : access mode mismatch"); // 学号+英文空格+姓名+英文空格+": access mode mismatch" dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { int grade; grade = Integer.valueOf(a[3]); if(grade>100||grade<0) { System.out.println("wrong format"); return; } Student stu = new Student(a[0], a[1], clss, grade); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stu.setfinal(grade); stulist.add(stu); dd(a[0],a[1]); } } } else { Course clss; clss = judecls(classlist, a[2]); if (clss == null) { System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist"); System.out.println(a[2] + " does not exist"); // 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist" dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { if (clss.bcho.equals("考核") == true) { dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { int egrade, ngrade; ngrade = Integer.valueOf(a[3]); egrade = Integer.valueOf(a[4]); if(ngrade>100||egrade<0||ngrade<0||egrade>100) { System.out.println("Wrong format"); return; } Student stu = new Student(a[0], a[1], clss, egrade, ngrade); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stu.setFinal(egrade, ngrade); stulist.add(stu); dd(a[0],a[1]); } } } } if(im.matchingInput(s_record) !=1&&swd.length>5) { if(s_record.indexOf("end")!=-1) return; String[] a = s_record.split(" "); int n = a.length - 4; int num = Integer.valueOf(a[3]); if(num==3) { System.out.println("wrong format"); return; } if(n!=num) { System.out.println(a[0]+" "+a[1]+" : access mode mismatch"); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } dd(a[0],a[1]); return; } int[] x = new int[num]; int sum = 0; for(int i=0;i<num;i++) { x[i]= Integer.valueOf(a[4+i]); sum = sum +x[i]; if(x[i]>100||x[i]<0) { System.out.println("wrong format"); return; } } Course clss; clss = judecls(classlist, a[2]); if (clss == null) { System.out.println(a[2] + " does not exist"); } Student st = new Student(a[0],a[1],clss,sum/num); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stulist.add(st); st.setFinal_grade(sum/num); dd(a[0],a[1]); } } public Course judecls(ArrayList<Course> cls, String name) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).name.equals(name) == true) return cls.get(i); } return null; } public Class jude(ArrayList<Class> cls, String num) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).num.equals(num) == true) return cls.get(i); } return null; } public St jugde(ArrayList<St> cls, String id) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).id.equals(id) == true) return cls.get(i); } return null; } public void dd(String s,String n) { St stt = new St(s,n); if(jugde(ss,s)==null) ss.add(stt); } public void showStudents() { int sum = 0; int n = 0; for (int i = 0; i < ss.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (ss.get(i).id.equals(stulist.get(j).id) == true) { sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(ss.get(i).id+" "+ss.get(i).name + " did not take any exams"); else { ss.get(i).aevv = sum / n; } sum = 0; n = 0; } Collections.sort(ss); for (int i = 0; i < ss.size(); i++) { St cs = ss.get(i); if(cs.aevv!=0) System.out.println(cs.id+" "+cs.name+" "+cs.aevv); } } public void showCourses() { // TODO Auto-generated method stub int sum = 0; int n = 0; int esum = 0; int nsum = 0; for (int i = 0; i < classlist.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (classlist.get(i).name.equals(stulist.get(j).cls.name) == true) { if (classlist.get(i).bcho.equals("考试") == true) { nsum = nsum + stulist.get(j).nor_grade; } esum = esum + stulist.get(j).exa_grade; sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(classlist.get(i).name + " has no grades yet"); else { classlist.get(i).ccave = sum / n; classlist.get(i).ffave = esum / n; if (classlist.get(i).bcho.equals("考试") == true) classlist.get(i).nnave = nsum / n; } sum = 0; n = 0; esum = 0; nsum = 0; } Collections.sort(classlist); for (int i = 0; i < classlist.size(); i++) { Course cs = classlist.get(i); if(cs.ffave!=0) { if(classlist.get(i).bcho.equals("考试") == true) System.out.println(cs.name+" "+cs.nnave+" "+cs.ffave+" "+cs.ccave); if(classlist.get(i).bcho.equals("考察") == true) System.out.println(cs.name+" "+cs.ffave+" "+cs.ccave); if(classlist.get(i).bcho.equals("实验") == true) System.out.println(cs.name+" "+cs.ccave); } } } public void showClasses() { //TODO Auto-generated method stub int sum = 0; int n = 0; for (int i = 0; i < clist.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (clist.get(i).num.equals(stulist.get(j).id.substring(0, 6)) == true) { sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(clist.get(i).num + " has no grades yet"); else { clist.get(i).averg = sum / n; } sum = 0; n = 0; } Collections.sort(clist); for (int i = 0; i < clist.size(); i++) { Class cs = clist.get(i); if(cs.averg!=0) System.out.println(cs.num+" "+cs.averg); } } } class St implements Comparable{ String id; Student stu; String name; int aevv; St(String id,String name) { this.id=id; this.name = name; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof St){ St goods = (St) o; if(this.id.compareTo(goods.id)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } } class Student implements Comparable{ Course cls; String id; String name; int final_grade; int nor_grade; int exa_grade; int avv; Student(String id,String name,Course cls,int exa_grade) { this.name = name; this.cls = cls; this.id=id; this.exa_grade = exa_grade; } Student(String id,String name,Course cls,int exa_grade,int nor_grade) { this.name = name; this.cls = cls; this.id=id; this.exa_grade = exa_grade; this.nor_grade = nor_grade; } public void setfinal(int exa_grade) { final_grade = exa_grade; } public void setFinal(int exa_grade,int nor_grade) { final_grade = (int)(exa_grade*0.7 + nor_grade*0.3); } public Course getCls() { return cls; } public void setCls(Course cls) { this.cls = cls; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getFinal_grade() { return final_grade; } public void setFinal_grade(int final_grade) { this.final_grade = final_grade; } public int getNor_grade() { return nor_grade; } public void setNor_grade(int nor_grade) { this.nor_grade = nor_grade; } public int getExa_grade() { return exa_grade; } public void setExa_grade(int exa_grade) { this.exa_grade = exa_grade; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof Student){ Student goods = (Student) o; if(this.id.compareTo(goods.id)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } }
无bug。
5、第七次作业第四题
此题非常简单。
//动物发生模拟器. 请在下面的【】处添加代码。 public class Main { public static void main(String[] args) { System.out.println("猫的叫声:喵喵"); System.out.println("狗的叫声:汪汪"); System.out.println("山羊的叫声:咩咩"); } }
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.text.Collator; import java.util.Comparator; import java.util.Locale; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String nextLine = in.nextLine(); ParseInput handleWith=new ParseInput(); while (!nextLine.equals("end")) { handleWith.parseInput(nextLine); nextLine = in.nextLine(); } handleWith.showStudents(); handleWith.showCourses(); handleWith.showClasses(); } } 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 regex = "(\\d+\\.\\d+)( (\\d+\\.\\d+)){2,9}"; static String experimentNumber = "[4-9]"; static String qwer = "\\d+\\.\\d+"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInputlmn = courseNameMatching + " " + courseTypeMatching1 + " " + "考试"+ " " + qwer + " " + qwer; static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; static String courseInputijk = courseNameMatching + " " + "实验" + " " + "实验" + " " + experimentNumber + " " + regex; static String scoreInputkmh = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching +" "+scoreMatching1; public static int matchInput(String s) {//check input string is valid and not empty string and not empty string if (matchCourse(s)) { return 1; } if (matchScore(s)) {// return 2; } return 0; } private static boolean matchScore(String s) { return (s.matches(scoreInput)||s.matches(scoreInputkmh)); } private static boolean matchCourse(String s) {//check if the score is correct return (s.matches(courseInput)||s.matches(courseInputijk)||s.matches(courseInputlmn)); } } class Course implements Comparable<Course>{ public String DnName; public String DnCharacter; public String DnExamine; public int number; public double [] weight;//weight vector for the current character in the current @Override public int compareTo(Course o) {// TOD should probably return 0 instead of 1 for performance Comparator<Object> compare = Collator.getInstance(Locale.CHINA); return compare.compare(DnName,o.getDnName()); } public Course(String DnName, String DnCharacter, String DnExamine,int number,double [] weight) {// this.DnExamine = DnExamine; this.number = number; this.DnCharacter = DnCharacter;// TOD should probably return 0 instead this.weight = weight; this.DnName = DnName; } public String getDnCharacter() { return DnCharacter;// TOD should probably return null instead of null } public String getDnName() { return DnName; } public Course(String DnName, String DnCharacter, String DnExamine,double [] weight) {// TOD should return null instead of throwing this.DnName = DnName; this.DnCharacter = DnCharacter; this.DnExamine = DnExamine; this.weight = weight; } public int getNumber() { return number; } public double[] getWeight() { return weight; } public Course(String DnName, String DnCharacter, String DnExamine) { this.DnName = DnName; //DnCharacter == DnCharacter && DnExamine == DnExamine this.DnCharacter = DnCharacter; this.DnExamine = DnExamine; } public String getDnExamine() { return DnExamine; } } class myClass implements Comparable<myClass>{// private String ID; ArrayList<Student> listStudent=new ArrayList<>(); public String getID() { return ID; } void addStudent(Student stu){ listStudent.add(stu); } public myClass(String ID) { this.ID = ID; } @Override public int compareTo(myClass myclass){ return ID.compareTo(myclass.getID()); } } class ParseInput{ ArrayList<myClass> classMyArrayList = new ArrayList<>(); ArrayList<Course> courseMyArrayList = new ArrayList<>(); ArrayList<Choose> chooseMyArrayList = new ArrayList<>(); ArrayList<Student> studentMyArrayList = new ArrayList<>(); myClass getclass(String classId){ for (myClass myClass : classMyArrayList) { if (myClass.getID().equals(classId)) return myClass; } return null; } public void showClasses() {// show the classes in the list of classes that are available for the given class Collections.sort(classMyArrayList);// sort the classes in the list of classes that are available for the given for (myClass myRoom : classMyArrayList) { ArrayList<Choose> stuClassSelects = getClassSelects(myRoom.getID());//get the class select list from the list of if (stuClassSelects != null) { System.out.println(myRoom.getID() + " " + getAvgTotalScore(stuClassSelects)); } else { System.out.println(myRoom.getID() + " " + "has no grades yet"); } } } public void parseInput(String nextLine) { String[] arraylist = nextLine.split(" "); switch (InputMatching.matchInput(nextLine)){//select class from arraylist and class from class list case 1: inputCourse(arraylist); break; case 2: inputScore(arraylist); break; case 0: System.out.println("wrong format"); break; } } Course getCourse(String courseName){ for (Course couKKK : courseMyArrayList) { if (couKKK.getDnName().equals(courseName)) return couKKK; } return null; } Student getStudent(String stuId){ for (Student student : studentMyArrayList) {// if (student.getStudentNumber().equals(stuId))// return student; } return null; } public void inputScore(String[] a){ myClass myclass; Student student; student=getStudent(a[0]);// student is the student myclass=getclass(a[0].substring(0,6)); if(myclass==null){ myclass=new myClass(a[0].substring(0,6)); classMyArrayList.add(myclass); } if(student==null){// student student=new Student(a[0], a[1]); studentMyArrayList.add(student);//student.getStudentNumber myclass.addStudent(student);//student = student.getStudentNumber(); } Course course=getCourse(a[2]); if(course==null){ System.out.println(a[2]+" does not exist");//System.out.println//System.out.println return; } if(!checkGrade(a,course)) return; Score score; if(a.length==4){// student score=new kcScore(Integer.parseInt(a[3])); } else if(a.length==5) {// student student score=new ScoreAtFirst(Integer.parseInt(a[3]),Integer.parseInt(a[4]),course.getWeight()); } else { int num = a.length-3; int [] flag =new int[num];//num = a.length - 3; for (int i = 0; i < flag.length; i++) { flag[i]=Integer.parseInt(a[i+3]); }//for(int i = 0; i < flag.length && i < num; i++) { score=new syScore(flag,0,course.getWeight()); } if(existChooseCourse(student,course)) return; Choose chooseCourse = new Choose(course, student, score);//new Choose(course, student , score); / chooseMyArrayList.add(chooseCourse); // } public int getSomeScore(ArrayList<Choose> courseSelects)//实验 { float sum =0; for(Choose Dn : courseSelects) { sum +=Dn.scores.getExperimentScore(); } return (int)(sum/courseSelects.size()); } public void inputCourse(String[] a){ Course course; //处理输入的内容 double [] weight; if(a[2].equals("实验")&&a.length>7){ weight = weightAtFirst(a); course = new Course(a[0],a[1],a[2],Integer.parseInt(a[3]),weight); } else { if (a[2].equals("考试")&&a.length==5)// means that the first character is the name of the course and the second character is the name of the course selection method and the third character { weight = weightAtLast(a); course = new Course(a[0], a[1], a[2], weight); } else course = new Course(a[0],a[1],a[2]); } if(getCourse(a[0])!=null) return; if(!checkCourseIs(a,course)) return; if(getCourse(a[0])==null&&a[2].equals("考察")&&a.length==3){// Course is already in the list of courses that are already in the list of courses course=new Course(a[0], a[1], a[2]); courseMyArrayList.add(searchCourse(course));// Course is already in the list of courses that are already in the list of courses } if(getCourse(a[0])==null&&a[2].equals("实验")&&a.length>=8){// weight = weightAtFirst(a); course=new Course(a[0], a[1], a[2],Integer.parseInt(a[3]),weight); courseMyArrayList.add(searchCourse(course)); } if(getCourse(a[0])==null&&a[2].equals("考试")&&a.length==5){ weight = weightAtLast(a);// course=new Course(a[0], a[1], a[2],0,weight); courseMyArrayList.add(searchCourse(course)); } } double [] weightAtFirst(String[] b){// weight at first character of string array array double [] awnffe = new double[Integer.parseInt(b[3])]; for (int i = 0; i < awnffe.length; i++) { awnffe[i]= Float.parseFloat(b[i+4]); } return awnffe; } double[] weightAtLast(String[] b){ double [] a = new double[2]; for (int i = 0; i < a.length; i++) {// // a[i]=Float.parseFloat(b[i+3]); } return a; } boolean checkCourseIs(String[] items,Course course){ if (items[2].equals("实验")&&items.length-4!=Integer.parseInt(items[3])){// Tod System.out.println(course.getDnName() + " : number of scores does not match"); return false; } if (items[2].equals("实验")&&items.length-4==Integer.parseInt(items[3])){ int num = Integer.parseInt(items[3]); double sum=0; int [] flag =new int[num]; for (int i = 0; i < flag.length; i++) {// check number of scores matches number sum+=Double.parseDouble(items[i+4]); } if(sum>1.0001||sum<0.9999){// check number of scores matches number System.out.println(course.getDnName() + " : weight value error");// return false; } } if (items[2].equals("考试")&&items.length==5){ double sum= 0; int [] flag =new int[2]; for (int i = 0; i < flag.length; i++) {// dawxa wdad dwfcfeffgjaj sum+=Double.parseDouble(items[i+3]); } if(sum>1.0001||sum<0.9999){ // check number of scores matches numbe number System.out.println(course.getDnName() + " : weight value error"); return false; } } if(course.getDnCharacter().equals("选修")&&course.getDnExamine().equals("考察")) return true; // qwdwad adhad dhggw if(course.getDnCharacter().equals("选修")&&course.getDnExamine().equals("考试")) return true; if(course.getDnCharacter().equals("实验")&&course.getDnExamine().equals("实验")) return true; if(course.getDnCharacter().equals("必修")&&course.getDnExamine().equals("考试")) return true; System.out.println(course.getDnName()+" : course type & access mode mismatch"); return false; } boolean existChooseCourse(Student stu,Course course){ // check wd specifics specific settings for (Choose choose : chooseMyArrayList) { if (choose.getCourse().getDnName().equals(course.getDnName())) { if (choose.getStudent().getStudentNumber().equals(stu.getStudentNumber())) return true; } } return false; } public void showCourses() { Collections.sort(courseMyArrayList);// for (Course cour : courseMyArrayList) { // ArrayList<Choose> CourseSelects = getCourseSelects(cour.getDnName()); if (CourseSelects != null) {// ser weq gt rtergfd dsddwd t tegeew if (cour.getDnExamine().equals("考试")) System.out.println(cour.getDnName() + " " + getAvgTotalScore(CourseSelects)); if (cour.getDnExamine().equals("考察")) System.out.println(cour.getDnName() + " " + getAvgTotalScore(CourseSelects)); if (cour.getDnExamine().equals("实验")) System.out.println(cour.getDnName() + " " + getSomeScore(CourseSelects)); } else { System.out.println(cour.getDnName() + " has no grades yet"); } } } public boolean checkGrade(String[] items,Course course){ String courseType=course.getDnExamine(); if(courseType.equals("考察")&&items.length==4){// Course Select List return true; } if(courseType.equals("考试")&&items.length==5){// Course Select List return true; } if(courseType.equals("实验")&&items.length-3==course.getNumber()){// we return true; } System.out.println(items[0]+" "+items[1]+" : access mode mismatch");//output return false; } public ArrayList<Choose> getStudentSelects(String stNumber) { ArrayList<Choose> stusHHH = new ArrayList<>(); for (Choose hhsadu : chooseMyArrayList) {// check adad dwdh if (hhsadu.students.getStudentNumber().equals(stNumber)) { stusHHH.add(hhsadu); } } if(stusHHH.size()!=0) return stusHHH; else return null; } public int getAvgTotalScore(ArrayList<Choose> listChooseCourse) { int sum =0; for(Choose Dn : listChooseCourse) { sum +=Dn.scores.getTotalScore(); // calculate total score for each student in the list of students in the list } return sum/listChooseCourse.size(); } public ArrayList<Choose> getClassSelects(String classID){ ArrayList<Choose> classes = new ArrayList<>(); for (Choose cho : chooseMyArrayList) { // for each student in the list of students in the list if (cho.students.getStudentNumber().substring(0, 6).equals(classID)) { classes.add(cho); } } if(classes.size()!=0) // 判断班级是否为0 return classes; else return null; } public ArrayList<Choose> getCourseSelects(String courseName){ ArrayList<Choose> courses = new ArrayList<>(); for (Choose ch : chooseMyArrayList) {// for each student in the list of students in the list if (ch.courses.getDnName().equals(courseName)) {// courses.add(ch); } } if(courses.size()!=0) return courses; else return null; } Course searchCourse(Course course){ for (Choose choLLL : chooseMyArrayList) {// only students with no students present in the list are selected if (choLLL.getCourse().getDnName().equals(course.getDnName())) { return null; } } return course; } public void showStudents() { Collections.sort(studentMyArrayList);// for (Student stuGGG : studentMyArrayList) { ArrayList<Choose> stuCourseSelects = getStudentSelects(stuGGG.getStudentNumber()); //从总选课表Choose中获取该生的选课记录集合 if (stuCourseSelects != null) { System.out.println(stuGGG.getStudentNumber() + " " + stuGGG.getStudentName() + " " + getAvgTotalScore(stuCourseSelects)); } else { // only students with no students present in the list are selected System.out.println(stuGGG.getStudentNumber() + " " + stuGGG.getStudentName() + " " + "did not take any exams"); } } } } class Student implements Comparable<Student>{//学生类 protected String stuNumbers; protected String stuNames; public Student(String stuNumber, String stuName) { this.stuNames=stuName; this.stuNumbers=stuNumber; } public String getStudentNumber() { return stuNumbers; } public String getStudentName() { return stuNames; } @Override public int compareTo(Student student){ // TOD should be implemented by subclass return getStudentNumber().compareTo(student.getStudentNumber()); } } abstract class Score{ int lastScore;//期末 int totalScore; public float getExperimentScore() { return totalScore; } Score(int finalScore){this.lastScore=finalScore;}// TOD float getTotalScore() { return totalScore; } } class ScoreAtFirst extends Score{//考试 private int ScoreOringin;//平时 ScoreAtFirst(int usualScore, int finalScore,double [] b){ super(finalScore); this.ScoreOringin=usualScore; this.totalScore=(int)(ScoreOringin*b[0]+finalScore*b[1]);// 总成绩 } } class syScore extends Score{//实验 syScore(int [] a,int finalScore,double [] b){ super(finalScore); double sum=0; for (int i = 0; i <a.length; i++) {// TOD should be faster than double array creation here for (int j = 0; j < b.length; j++) { sum+=(a[i]*b[i]); } } this.totalScore= (int)sum/a.length; } } class kcScore extends Score{//考察 kcScore(int finalScore){ super(finalScore); this.totalScore=finalScore; } } class Choose { Course courses; Student students; Score scores; public Course getCourse() { return courses; } public Student getStudent() { return students; } public Choose(Course course, Student student, Score score) { this.courses = course; this.students = student; this.scores = score; } }