Java作业最后一次总结
目录:
- 前言
- 设计与分析
- 踩坑心得
- 主要困难以及改进建议
- 总结
一、前言
1.第七次作业有关HashMap检索以及排序,动物发声模拟的题目难度都比较低,掌握基本的HashMap的用法就可以很顺利的完成。第一次课程成绩统计程序我并没有顺利完成,能过的测试点很少,在第一次课程成绩计价程序的基础上,这次的课程成绩计价程序2过了大多数测试点。总体难度相当于之前的菜单计价程序难度要低。
2.第八次作业有关身份证排序,覆盖的问题能够非常顺利的完成,但自定义接口中的问题一些方法的设计存在问题。这次的课程成绩计价程序相对于前两次对于我来说,难度飙升。对一些异常情况的处理也不太好。过的测试点也很少。
3.期末考试吸取上次期中考试的教训,先写的编程题,对于我来说,期末考试比期中考试要简单些,不过后两道编程题没有完全按照题目的要求设计,而是根据我平常的习惯设计的类。涉及的知识点也是继承多态,以及设计原则,接口的问题。
二、设计与分析
7-1 容器-HashMap-检索问题
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap<String, String[]> studentMap = new HashMap<>(); while (input.hasNext()) { String stringInfo = input.nextLine(); if (stringInfo.equals("end")) { break; } String[] studentInfo = stringInfo.split(" "); String[] infoArray = {studentInfo[1], studentInfo[2]}; studentMap.put(studentInfo[0], infoArray); } String queryId = input.nextLine(); if (studentMap.containsKey(queryId)) { String[] infoArray = studentMap.get(queryId); System.out.println(queryId + " " + infoArray[0] + " " + infoArray[1]); } else { System.out.println("The student " + queryId + " does not exist"); } input.close(); } }
这题我使用了String类的split方法将输入的信息拆分为一个字符串数组,其中第一个元素是学生的学号,第二个元素是学生的姓名,第三个元素是学生的年龄。将这些信息存储在一个字符串数组中,然后将其作为值,以学号为键,存储在一个HashMap中。输入要查询的学生学号,如果输入的学号存在于HashMap中,将输出该学生的学号、姓名和年龄。否则,将输出一条错误消息,指示查询的学生不存在。知道基本的hashmap使用方法就可以顺利完成此题。
7-2 容器-HashMap-排序
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap<Integer, String[]> studentMap = new HashMap<>(); while (input.hasNext()) { String stringInfo = input.nextLine(); if (stringInfo.equals("end")) { break; } String[] studentInfo = stringInfo.split(" "); int id = Integer.parseInt(studentInfo[0]); String[] infoArray = {studentInfo[1], studentInfo[2]}; studentMap.put(id, infoArray); } List<Integer> sortedIds = new ArrayList<>(studentMap.keySet()); Collections.sort(sortedIds, Collections.reverseOrder()); for (int id : sortedIds) { String[] infoArray = studentMap.get(id); System.out.println(id + " " + infoArray[0] + " " + infoArray[1]); } input.close(); } }
这题我使用了String类的split方法将输入的信息拆分为一个字符串数组,其中第一个元素是学生的学号(转换为整型),第二个元素是学生的姓名,第三个元素是学生的年龄。然后,将这些信息存储在一个字符串数组中,再将其作为值,以学号为键,存储在一个HashMap中。创建了一个ArrayList对象sortedIds,用于存储学生的学号,并对其进行降序排序。然后,遍历排序后的学号列表,依次输出每个学生的学号、姓名和年龄。
7-3课程成绩统计程序-2
部分代码如下:
class InputStream { ArrayList<Class>classes=new ArrayList<>(); ArrayList<Student>students=new ArrayList<>(); ArrayList<Course>courses=new ArrayList<>(); ArrayList<ChooseCourse>chooseCourses=new ArrayList<>(); static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String NumMatching = "[4-9]";//8个0-9的数字 static String regex = "([1-9]\\d{0,1}|0|100)( ([1-9]\\d{0,1}|0|100)){3,8}"; 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 = "(考试|考察|实验)"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; static String courseInput1 = courseNameMatching + " " + courseTypeMatching ; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching +" "+ scoreMatching; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +NumMatching+" " +regex; static String scoreInput3 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +NumMatching+" " + scoreMatching+" "+ scoreMatching+" "+ scoreMatching; public void inputStream(String s){ String[] news = s.split(" "); int inputType = matchingInput(s); if (inputType == 0) { System.out.println("wrong format"); } else if (inputType == 1) { courseInput(news); } else if (inputType == 2) { scoreInput(news); } } //对输入的课程信息进行判断 public void courseInput(String[] news) { Course existingCourse = judgeCourse(news[0]); if (news.length == 3) { String accessMode = (news[1].equals("必修") && (news[2].equals("考察") || news[2].equals("实验"))) || (news[1].equals("选修") && news[2].equals("实验")) ? "考试" : news[2]; if (existingCourse == null) { Course course = new Course(news[0], news[1], accessMode); courses.add(course); } } else { if (existingCourse == null) { Course course = new Course(news[0], news[1], "考试"); courses.add(course); } } } //对输入的成绩信息进行判断 public void scoreInput(String[] s){ Student student=new Student(s[1],s[0]); Student existstudent=judgeStudent(s[0]); if(existstudent==null) students.add(student); Class class1=new Class(); String classNumber1 = s[0].substring(0, 6); class1.setclassNumber(classNumber1); class1.students.add(student); if(judgeClass(classNumber1)==null) classes.add(class1); Score score; Course existcourse = judgeCourse(s[2]); if(existcourse!=null) { if(s.length==4) { if(existcourse.testway.equals("考试")||existcourse.testway.equals("实验")) { System.out.println(s[0] + " " + s[1] + " : access mode mismatch"); return; } score=new reviewScore(); score.setScore(Integer.parseInt(s[3])); }else if(s.length==5){ if(existcourse.testway.equals("考察")||existcourse.testway.equals("实验")) { System.out.println(s[0] + " " + s[1] + " : access mode mismatch"); return; } score=new testScore(); score.setScore(Integer.parseInt(s[4])); ((testScore) score).setUsualScore(Integer.parseInt(s[3])); } else { if(!existcourse.testway.equals("实验")||s.length!=4+Integer.parseInt(s[3])) { System.out.println(s[0] + " " + s[1] + " : access mode mismatch"); return; } score=new experimentalScore(); for(int i=0;i<Integer.parseInt(s[3]);i++){ ((experimentalScore) score).scores.add(Integer.parseInt(s[i+4])); } } ChooseCourse chooseCourse=new ChooseCourse(existcourse,student,score); if(ChooseCourse.judgeChooseCourse(chooseCourse)==false) chooseCourses.add(chooseCourse); } else { System.out.println(s[2]+" does not exist"); } } public Class judgeClass(String s){ for(int i=0;i<classes.size();i++){ if(classes.get(i).getclassNumber().equals(s)) return classes.get(i); } return null; } public Course judgeCourse(String s) { for(int i=0;i<courses.size();i++){ if(courses.get(i).getCourseName().equals(s)) return courses.get(i); } return null; } public Student judgeStudent(String s){ for(int i=0;i<students.size();i++){ if(students.get(i).getStudentId().equals(s)) return students.get(i); } return null; } public void showStudents() { Collections.sort(students); for (int i = 0; i< students.size(); i++) { Student student = students.get(i); ArrayList<ChooseCourse> studentCourseSelects = getStudentSelects(student.getStudentId()); if (studentCourseSelects.size() !=0) { System.out.println(student. getStudentId() + " " + student.getName() +" "+getAvgTotalScore(studentCourseSelects)); } else { System.out.println(student.getStudentId() + " " + student.getName() + " " + "did not take any exams"); } } } public void showCourse() { Collections.sort(courses); for (Course course : courses) { ArrayList<ChooseCourse> studentCourseSelects = getCoursesSelects(course.getCourseName()); if (studentCourseSelects.size() != 0) { if (getUTotalScore(studentCourseSelects) == 0 && !course.judge.equals("实验")) { System.out.println(course.getCourseName() + " " + getEndTotalScore(studentCourseSelects) + " " + getFinalScore(studentCourseSelects)); } else if (getUTotalScore(studentCourseSelects) == 0 && course.judge.equals("实验")) { System.out.println(course.getCourseName() + " " + getFinalScore(studentCourseSelects)); } else { System.out.println(course.getCourseName() + " " + getUTotalScore(studentCourseSelects) + " " + getEndTotalScore(studentCourseSelects) + " " + getFinalScore(studentCourseSelects)); } } else { System.out.println(course.getCourseName() + " has no grades yet"); } } } public int getUTotalScore(ArrayList<ChooseCourse> studentCourseSelects) { int sum = 0; for (ChooseCourse chooseCourse : studentCourseSelects) { sum += chooseCourse.score.getUsualScore(); } return (int) (sum / studentCourseSelects.size()); } public int getFinalScore(ArrayList<ChooseCourse> studentCourseSelects) { int sum = 0; for (ChooseCourse chooseCourse : studentCourseSelects) { sum += chooseCourse.score.getFinalScore(); } return (int) (sum / studentCourseSelects.size()); } public int getEndTotalScore(ArrayList<ChooseCourse> studentCourseSelects) { int sum = 0; for (ChooseCourse chooseCourse : studentCourseSelects) { sum += chooseCourse.score.getScore(); } return (int) (sum / studentCourseSelects.size()); } public void showClass() { Collections.sort(classes); for (int i = 0; i < classes.size(); i++) { Class class1 = classes.get(i); ArrayList<ChooseCourse> studentCourseSelects = getClassSelects(class1.getclassNumber()); if (studentCourseSelects.size() != 0) { System.out.println(class1.getclassNumber() + " " + getClassTotalScore(studentCourseSelects)); } else { System.out.println(class1.getclassNumber() + " has no grades yet"); } } } public ArrayList<ChooseCourse> getClassSelects(String classNumber) { ArrayList<ChooseCourse> class1 = new ArrayList<>(); for (ChooseCourse chooseCourse : chooseCourses) { String studentId = chooseCourse.getStudent().getStudentId(); if (studentId.substring(0, 6).equals(classNumber)) { class1.add(chooseCourse); } } return class1; } public ArrayList<ChooseCourse> getCoursesSelects(String courseName) { ArrayList<ChooseCourse> courses1 = new ArrayList<>(); for (ChooseCourse chooseCourse : chooseCourses) { if (chooseCourse.getCourse().getCourseName().equals(courseName)) { courses1.add(chooseCourse); } } return courses1; } public int getClassTotalScore(ArrayList<ChooseCourse> studentCourseSelects) { if (studentCourseSelects.size() == 0) { return 0; } int sum = 0; for (ChooseCourse chooseCourse : studentCourseSelects) { sum += chooseCourse.getScore().getFinalScore(); } return (int) (sum / studentCourseSelects.size()); } public int getAvgTotalScore(ArrayList<ChooseCourse> studentCourseSelects) { if (studentCourseSelects.size() == 0) { return 0; } int sum = 0; for (ChooseCourse chooseCourse : studentCourseSelects) { sum += chooseCourse.getScore().getFinalScore(); } return (int) (sum / studentCourseSelects.size()); } public ArrayList<ChooseCourse> getStudentSelects(String studentId) { ArrayList<ChooseCourse> students = new ArrayList<>(); for (ChooseCourse chooseCourse : chooseCourses) { if (chooseCourse.getStudent().getStudentId().equals(studentId)) { students.add(chooseCourse); } } return students; } public static int matchingInput(String s) { if (s.matches(courseInput)||s.matches(courseInput1)) { return 1; } if (s.matches(scoreInput)||s.matches(scoreInput1)||s.matches(scoreInput2)||s.matches(scoreInput3)) { return 2; } return 0; } }
本题我运用了不同类型的正则表达式,对输入的信息进行处理,通过 inputStream 方法根据输入类型调用 courseInput 方法或 scoreInput 方法进行处理,courseInput 方法处理课程信息的输入,根据输入内容创建 Course 对象,并将其添加到对应的列表中,scoreInput 方法处理成绩信息的输入,根据输入内容创建 Student、Score、ChooseCourse 对象,并将其添加到对应的列表中,judgeClass、judgeCourse 和 judgeStudent 方法用于判断输入的班级、课程和学生是否存在,showStudents、showCourse 和 showClass 方法分别用于展示学生信息、课程信息和班级信息,getUTotalScore、getFinalScore、getEndTotalScore 等方法用于计算学生或课程的总分数,matchingInput 方法用于判断输入的字符串属于课程信息还是成绩信息。
类图:
7-4按照题目要求来做,基本上不会有什么问题。
第八次作业
7-3身份证排序
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input= new Scanner(System.in); List<String> idList = new ArrayList<String>(); int n = Integer.parseInt(input.nextLine()); for (int i = 0; i < n; i++) { String id = input.nextLine(); idList.add(id); } String IdInfo = input.nextLine(); while (!IdInfo.equals("exit")) { if (IdInfo.equals("sort1")) { sortAndPrintByDate(idList); } else if (IdInfo.equals("sort2")) { sortAndPrintAll(idList); } else { System.out.println("exit"); break; } IdInfo = input.nextLine(); } input.close(); } private static void sortAndPrintByDate(List<String> idList) { List<String> sortedList = new ArrayList<>(idList); Collections.sort(sortedList, (a, b) -> { String dateA = a.substring(6, 14); String dateB = b.substring(6, 14); return dateA.compareTo(dateB); }); for (String id : sortedList) { String date = id.substring(6, 14); String formattedDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8); System.out.println(formattedDate); } } private static void sortAndPrintAll(List<String> idList) { List<String> sortedList = new ArrayList<>(idList); Collections.sort(sortedList, (a, b) -> { String dateA = a.substring(6, 14); String dateB = b.substring(6, 14); return dateA.compareTo(dateB); }); for (String id : sortedList) { System.out.println(id); } } }
本题先将读取输入的数据行数 n 和 n 行字符串数据,将其添加到 idList 中,循环读取用户输入的命令字符串,如果是 "sort1",则调用 sortAndPrintByDate 方法进行按日期排序并输出;如果是 "sort2",则调用 sortAndPrintAll 方法进行全部排序并输出;否则退出循环。我运用了sortAndPrintByDate 方法将 idList 复制一份,通过 Collections.sort 方法按日期排序,并在遍历输出时将日期格式化为 yyyy-MM-dd 的形式。
7-2课程成绩统计程序-3
class InputStream{ static String stuStudentIdMatching = "[0-9]{8}"; static String stuNameMatching = "\\S{1,10}"; static String scoreMatching = "(\\d|[1-9]\\d|100)"; static String courseNameMatching = "\\S{1,10}"; static String courseTypeMatching = "(选修|必修|实验)"; static String weightMatching = "(0\\.[0-9]+|1\\.0)(\\s(0\\.[0-9]+|1\\.0))*"; static String checkcourseTypeMatching = "(考试|考察|实验)"; static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching + "(\\s\\d+\\s" + weightMatching + ")?"; static String scoreInput = stuStudentIdMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(\\s"+scoreMatching+")*"; private final ArrayList<ChooseCourse> ChooseCourses = new ArrayList<>(); private HashMap<String, Course> courses = new HashMap<>(); private final ArrayList<Student> students = new ArrayList<>(); private final ArrayList<String> Class = new ArrayList<>(); private final HashMap<String,String> coursetestWay=new HashMap(); public void inputStream(String input) { String[] inputs = input.split(" "); int matchingInput = matchingInput(input); String studentId = inputs[0]; String studentName = inputs[1]; String courseName = inputs[2]; if (judge(input) == 1) { if (courses.containsKey(studentId)) return; else { Course newCourse = new Course(studentId, studentName, courseName); double totalWeight = 0; if (courseName.equals("考试")) { if (inputs.length != 5) { System.out.println("wrong format"); return; } totalWeight = Double.parseDouble(inputs[3]) + Double.parseDouble(inputs[4]); if (totalWeight != 1) { System.out.println(studentId + " : weight value error"); return; } List<Double> weightList = new ArrayList<>(); weightList.add(Double.parseDouble(inputs[3])); weightList.add(Double.parseDouble(inputs[4])); newCourse.setWeightList(weightList); } else if (courseName.equals("考察")) { if (inputs.length != 3) { System.out.println("wrong format"); return; } } else if (courseName.equals("实验")) { if (inputs.length < 5 || (inputs.length - 4) != Integer.parseInt(inputs[3])) { System.out.println(studentId + " : number of scores does not match"); return; } int numScores = Integer.parseInt(inputs[3]); if (numScores < 4 || numScores > 9) { System.out.println(studentId + " : Number of scores should be between 4 and 9"); return; } List<Double> weightList1 = new ArrayList<>(); for (int i = 4; i < inputs.length; i++) { totalWeight += Double.parseDouble(inputs[i]); weightList1.add(Double.parseDouble(inputs[i])); } if (totalWeight != 1) { System.out.println(studentId + " : weight value error"); return; } newCourse.setWeightList(weightList1); } else { System.out.println(studentId + " : course type & access mode mismatch"); return; } courses.put(studentId, newCourse); } } else if (matchingInput == 2) { Course findcourse = courses.get(courseName); if (findcourse == null) { System.out.println(studentId + ' ' + studentName + " : " + courseName + " does not exist"); return; } String courseType = findcourse.gettestWay(); if (!courseName.equals(courseName)) { System.out.println(studentId + ' ' + studentName + " : " + courseName + " does not exist"); return; } Student newStudent = new Student(studentId, studentName); if (!isStudentExist(newStudent.getStudentId())) students.add(newStudent); if (!isStudentInClass(studentId.substring(0, 6))) { Class.add(studentId.substring(0, 6)); } if (isCourseSelected(studentId, courseName)) return; if (findcourse.gettestWay().equals("考试") && inputs.length != 5) { System.out.println(studentId + ' ' + studentName + " : access mode mismatch"); } else if (findcourse.gettestWay().equals("考察") && inputs.length != 4) { System.out.println(studentId + ' ' + studentName + " : access mode mismatch"); }else if (findcourse.gettestWay().equals("实验") && inputs.length != 3 + findcourse.getWeightList().size()) { System.out.println(studentId + ' ' + studentName + " : access mode mismatch"); } else { ChooseCourse newChooseCourse = new ChooseCourse(); newChooseCourse.setCourse(findcourse); Grade grade = null; if (findcourse.gettestWay().equals("考试")) { ExamGrade examGrade = new ExamGrade(); if (Integer.parseInt(inputs[3]) < 0 || Integer.parseInt(inputs[3]) > 100 || Integer.parseInt(inputs[4]) < 0 || Integer.parseInt(inputs[4]) > 100) { System.out.println("wrong format"); return; } examGrade.setUsualGrade(Double.parseDouble(inputs[3])); examGrade.setFinalGrade(Double.parseDouble(inputs[4])); examGrade.setUsualWeight(findcourse.getWeightList().get(0)); examGrade.setFinalWeight(findcourse.getWeightList().get(1)); grade = examGrade; } else if (findcourse.gettestWay().equals("实验")) { WeightedGrade weightedGrade = new WeightedGrade(); ArrayList<Double> weightList = new ArrayList<>(); ArrayList<Double> scoreList = new ArrayList<>(); double sumScore = 0; for (int i = 3; i < inputs.length; i++) { if (Double.parseDouble(inputs[i]) < 0 || Double.parseDouble(inputs[i]) > 100) { System.out.println("wrong format"); return; } sumScore += Double.parseDouble(inputs[i]) * findcourse.getWeightList().get(i - 3); scoreList.add(Double.parseDouble(inputs[i])); weightList.add(findcourse.getWeightList().get(i - 3)); } weightedGrade.setScoreList(scoreList); weightedGrade.setWeightList(weightList); weightedGrade.setFinalGrade(sumScore); grade = weightedGrade; } else { NoExamGrade noExamGrade = new NoExamGrade(); noExamGrade.setFinalGrade(Integer.parseInt(inputs[3])); grade = noExamGrade; } newChooseCourse.setGrade(grade); newChooseCourse.setStudent(newStudent); ChooseCourses.add(newChooseCourse); } } else { System.out.println("wrong format"); } } private Course checkCourse(String courseName){ return courses.get(courseName); } private boolean isStudentExist(String studentId) { for (int i = 0; i < students.size(); i++) { if (students.get(i).getStudentId().equals(studentId)) { return true; } } return false; } private Boolean isStudentInClass(String classStudentId){ for (String cname:Class){ if(cname.equals(classStudentId))return true; } return false; } private boolean isCourseSelected(String studentId, String courseName) { for (int i = 0; i < ChooseCourses.size(); i++) { ChooseCourse currentChooseCourse = ChooseCourses.get(i); if (currentChooseCourse.getStudent().getStudentId().equals(studentId) && currentChooseCourse.getCourse().getName().equals(courseName)) { return true; } } return false; } public void studentScore(){ for (Student student:students){ double sum=0; int count=0; for (ChooseCourse ChooseCourse:ChooseCourses){ if (ChooseCourse.getStudent().getStudentId().equals(student.getStudentId())) { sum+=ChooseCourse.getGrade().getTotalGrade(); count++; } } if(count==0) { System.out.println(student.getStudentId()+' '+student.getName()+' '+"did not take any exams"); } else { System.out.println(student.getStudentId()+' '+student.getName()+' '+(int)sum/count); } } } public void CourseScore(){ for (Course course:courses.values()){ double sumUsualScore=0; double sumFinalScore=0; double sumTotalScore=0; int count=0; for(ChooseCourse chooseCourse:ChooseCourses){ if(chooseCourse.getCourse().getName().equals(course.getName())){ count++; sumTotalScore+=chooseCourse.getGrade().getTotalGrade(); sumFinalScore+=chooseCourse.getGrade().getFinalGrade(); if(chooseCourse.getCourse().gettestWay().equals("考试")){ sumUsualScore+=chooseCourse.getGrade().getUsualGrade(); } } } if (count==0) System.out.println(course.getName()+' '+"has no grades yet"); // 四舍五入 保留整数 else if(course.gettestWay().equals("考试")) { System.out.println(course.getName()+' '+(int)(sumUsualScore/count) + ' '+(int)(sumFinalScore/count) + ' '+(int)(sumTotalScore/count)); } else if(course.gettestWay().equals("考察")) { System.out.println(course.getName()+' '+(int)(sumTotalScore/count) + ' '+(int)(sumFinalScore/count)); } else if(course.gettestWay().equals("实验")) { System.out.println(course.getName()+' '+ (int)(sumTotalScore/count)); } } } public void ClassScore() { Class.forEach(classStudentId -> { double sum = ChooseCourses.stream() .filter(chooseCourse -> chooseCourse.getStudent().getStudentId().substring(0, 6).equals(classStudentId)) .mapToDouble(chooseCourse -> chooseCourse.getGrade().getTotalGrade()) .sum(); long count = ChooseCourses.stream() .filter(chooseCourse -> chooseCourse.getStudent().getStudentId().substring(0, 6).equals(classStudentId)) .count(); if (count == 0) { System.out.println(classStudentId + " has no grades yet"); } else { System.out.println(classStudentId + " " + (int) (sum / count)); } }); }
-
类图
-
根据输入的内容使用正则表达式匹配,判断输入的类型。然后根据不同的输入类型进行相应的操作当输入的类型为1时,表示添加课程。首先判断课程是否已存在,如果存在则返回。否则根据输入的内容创建新的课程,并将其加入到课程列表中。当输入的类型为2时,表示添加学生选课成绩。然后根据输入的内容创建学生对象和成绩对象,将其加入到已选课程列表中。其中
checkCourse
方法用于检查课程是否存在,isStudentExist
方法用于检查学生是否已存在,isStudentInClass
方法用于检查学生是否属于某个班级,isCourseSelected
方法用于检查学生是否已选择某课程,studentScore
方法用于计算并输出每个学生的平均成绩,CourseScore
方法用于计算并输出每门课程的平均成绩,ClassScore
方法用于计算并输出每个班级的平均成绩。
期末考试就没啥太大的问题,就是最后一题,有个测试点没过。也不知道是不是情况没考虑到,刚开始因为数值只判断了大于情况下,输出错误信息,后来修改了,考虑了等于的情况但仍然存在问题。
三、踩坑心得
1、课程成绩统计2,我的代码逻辑我自认为是没有问题的,也不记得当时自己改了还是没改,有的测试点有的时候能过,有时候不能过,会出现运行超时的情况,我想了想,可能是因为我的代码算法复杂度高,代码中比如嵌套循环过多,导致程序运行时间过长。也可能是因为输入的数据量很大,代码的处理方式不够高效,导致运行超时。
2、身份证排序,刚开始我定义了一个IdCard
类来表示身份证对象,创建ArrayList<IdCard>
来存储身份证对象,通过重写compare()
方法实现不同的排序方式,并使用Collections.sort()
方法对对象列表进行排序,最后根据排序类型进行输出,但是并不能正确输出身份证信息,后来我使用了List<String>
来存储身份证号码,通过Collections.sort()
方法对列表进行排序,根据不同的排序类型进行输出。解决了这个问题。
3.在进行成绩统计时,需要选择合适的数据结构来存储学生成绩。用数组、集合、映射,方便进行统计和检索,在这次中我运用了集合,对输入的不同信息不同类别采用不同的集合,刚开始我是用数组的,发现采用数组,不仅对异常情况处理不太方便,而且输入信息判断要采用多层嵌套循环,写着写着我就迷糊了。
四、主要困难及改进建议
1.课程计价程序尽可能地将相似的功能抽象成方法或函数,提高代码的复用性和可维护性。在本次题目中我就把成绩统计的功能封装成一个独立的方法或类。在进行成绩计算时,需要考虑不同科目的权重和计算公式。这些因素可能比较复杂,最好采用可扩展的设计,方便后续的修改和维护,虽然迭代题目到这个就结束了。
2.自定义接口,主要困难:实现ArrayIntegerStack类时,需要注意数组大小的限制和动态扩容的问题,如果数组大小不够,需要进行扩容操作,否则无法继续入栈。输出结果时,需要注意格式和内容的准确性和清晰度。刚开始就是这个原因,对输出的结果没有采用统一的方式输出,导致输出结果不对,后来我规范了输出格式。
3.在日期格式化方面,使用Java的SimpleDateFormat类来进行日期格式化,以提高代码的可读性和可维护性
五、总结
通过这几次作业,在实现课程成绩统计时,需要设计和创建学生类、课程类以及成绩统计类等多个对象,并通过对象之间的交互来完成相应的功能。加深了我对面向对象编程的概念的理解和应用,对封装、继承和多态有了进一步的理解。在统计学生成绩时,需要组织和管理学生信息、课程信息和成绩数据,使我使用数组、集合或自定义数据结构,以及排序、搜索等算法更加灵活。通过计算每个学生的平均分、最高分和最低分,以及每门课程的平均分、最高分和最低分,学会了对数据进行处理和分析的思维方式和技巧。多学习使用eclipse,这个编译软件很好用,功能很多,自动报错的功能对于代码的修改很有帮助,写pta不能只在那上面写代码,灵活运用eclipse对自己编写修改代码很有帮助。
不同类之间的关系,许多时候正是不知如何进行将类互相呼应,导致代码亢长,产生垃圾代码,这不利于学习;当能够了解,并运用类与类之间的关系,则能解决大多数问题。