一、前言
(1)pta第七次作业题目列表如下:
7-1 菜单计价程序-5
总结:这个菜单计价1程序-5是前菜单计价程序-3的迭代,难度较之前的有所提升,题目难度对我来说感觉很大,写了很久也没有拿下。
(2)pta第八次作业题目列表如下:
7-1 课程成绩统计程序-1
总结:总算是熬过了菜单,迎来了新的课程成绩统计,有了前面菜单的练习,对于该课程成绩统计的设计和完成有了极大的提升,没有刚开始写菜单时的那种难以下手和无力感,也是拿下了大量的分数。
(3)pta第九次作业题目列表如下:
7-1 统计Java程序中关键词的出现次数
总结:这次题目又与之前不同,这次题目是让我们统计关键词出现的次数,代码量较之之前也有所大量的减少,但是需要我们去学习很多的新东西,如对set和map的使用,还有正则表达式的使用,需要我们去学习的新东西很多,去理解这些东西的用法。
(4)pta第十次作业题目列表如下:
7-1 容器-HashMap-检索
7-2 容器-HashMap-排序
7-3 课程成绩统计程序-2
7-4 动物发声模拟器(多态)
总结:这一次作业可算不只是一道题目了,除了7-3的课程成绩统计程序,其它三道题目的难度都比较低,只是需要去理解一下HashMap的使用就可以完成,然后课程成绩统计程序-2是对上次课程成绩统计-1的迭代,较之之前就只是添加了一种考试形式和分数计算形式,没有怎么增大题目的难度,只有两个测试点没有通过。
(5)pta第十一次作业题目列表如下:
7-1 容器-ArrayList-排序
7-2 课程成绩统计程序-3
7-3 jmu-Java-02基本语法-03-身份证排序
7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
7-5 jmu-Java-03面向对象基础-05-覆盖
总结:这次题目也是就课程成绩统计程序-3有所难度,其它题目都能通过学习在短时间内解决,这个课程成绩统计程序-3又是对于课程成绩统计程序-2的迭代,改变了类与类之间的关系和输入的方式。
二、设计与分析
7-1 菜单计价程序-5
本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。
注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。
以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:
1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"
例如:麻婆豆腐 川菜 9 T
菜价的计算方法:
周一至周五 7折, 周末全价。
特色菜的口味类型:川菜、晋菜、浙菜
川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;
晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;
例如:麻婆豆腐 川菜 9 T
输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数
例如:1 麻婆豆腐 4 1 9
单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:
acidity num out of range : 5
输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。
一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。
如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:
table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格
例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜
计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。
注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。
2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:
格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
例如:table 1 : tom 13670008181 2023/5/1 21/30/00
约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。
输出用户支付金额格式:
用户姓名+英文空格+手机号+英文空格+支付金额
注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:
计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。
将所有记录的菜价累加得到整桌菜的价格。
总结:对于这道题我没能完成,只得到了部分分数,没能全部完成。
7-1 课程成绩统计程序-1
某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图:
类图如下:
总结:该题目顺利完成,主要做题目过程就是先把各个类中的所需写的属性和方法写完,然后在根据题目中的条件,用方法去实现一个个过程,原本是直接写的,那样会有很多杂糅代码,通过一个个方法去实现很大程度上减少了代码量,根据各个课程的考核不同,对课程的存入进行分类。
import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Class> classes = new ArrayList<>(); ArrayList<Course> courses = new ArrayList<>(); ArrayList<Student> students = new ArrayList<>(); ArrayList<CourseSelection> courseSelections = new ArrayList<>(); Scanner input = new Scanner(System.in); while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } String[] nextLine = str.split(" "); if (nextLine.length == 3) { addCourse(nextLine,courses); } else if (nextLine.length == 5) { addScoredCourseSelection(nextLine,courseSelections,students,courses); } else if (nextLine.length == 4) { addUnScoredCourseSelection(nextLine, courses, students, courseSelections); } else { System.out.println("wrong format"); } } for (Student student:students ) { int classNumber = Integer.parseInt(student.getStudentNumber()) / 100; if (classes.size() == 0) { Class clas = new Class(); clas.setClassNumber(classNumber); clas.getStudents().add(student); classes.add(clas); } else { ListIterator<Class> iterator = classes.listIterator(); while (iterator.hasNext()) { Class clas = iterator.next(); if (clas.getClassNumber() == classNumber) { clas.getStudents().add(student); break; } else { if (classes.indexOf(clas) == classes.size() - 1) { Class clasNew = new Class(); clasNew.setClassNumber(classNumber); clasNew.getStudents().add(student); iterator.add(clasNew); } } } } } Collections.sort(classes, Comparator.comparingInt(o -> o.getClassNumber())); for (Class clas:classes ) { clas.setClassAverageScore(print1(clas.getStudents(), courseSelections)); } print2(courses, courseSelections); for (Class clas:classes ) { if (clas.getClassAverageScore() == -1) { System.out.println(clas.getClassNumber() + " has no grades yet"); } else System.out.println(clas.getClassNumber() + " " + clas.getClassAverageScore()); } } public static int print1(ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { int classAverageScore = 0; Collections.sort(students, Comparator.comparingInt(o -> Integer.parseInt(o.getStudentNumber()))); for (Student student:students ) { int averageScore = 0; int courseNum = 0; for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentNumber().equals(student.getStudentNumber())) { averageScore += cs.getScore().getTotalScore(); courseNum ++; } } if (courseNum == 0) { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " did not take any exams"); return -1; } else { averageScore /= courseNum; System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " " + averageScore); } classAverageScore += averageScore; } return classAverageScore / students.size(); } public static void print2(ArrayList<Course> courses, ArrayList<CourseSelection> courseSelections) { Collections.sort(courses, (o1, o2) -> { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(o1.getCourseName(), o2.getCourseName()); }); for (Course course:courses ) { int courseAverageScore = 0; int courseDailyPerformance = 0; int courseFinalGrade = 0; int studentNum = 0; for (CourseSelection cs:courseSelections ) { if (course.getCourseName().equals(cs.getCourse().getCourseName())) { courseAverageScore += cs.getScore().getTotalScore(); courseDailyPerformance += cs.getScore().getDailyPerformance(); courseFinalGrade += cs.getScore().getFinalGrade(); studentNum ++; } } if (studentNum == 0) { System.out.println(course.getCourseName() + " has no grades yet"); } else { courseAverageScore /= studentNum; courseDailyPerformance /= studentNum; courseFinalGrade /= studentNum; if (course.getAssessmentMethod().equals("考试")) { System.out.println(course.getCourseName() + " " + courseDailyPerformance + " " + courseFinalGrade + " " + courseAverageScore); } else { System.out.println(course.getCourseName() + " " + courseFinalGrade + " " + courseAverageScore); } } } } public static boolean isCourseExist(String courseName, ArrayList<Course> courses, int length, Student student) { for (Course course:courses ) { if (course.getCourseName().equals(courseName)) { if ((course.getAssessmentMethod().equals("考试") && length != 5) || (course.getAssessmentMethod().equals("考察") && length != 4)) { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " : access mode mismatch"); return false; } else return true; } } System.out.println(courseName + " does not exist"); return true; } public static boolean RepetitiveCourses(Course course,ArrayList<Course> courses) { for (Course cou:courses ) { if (cou.getCourseName().equals(course.getCourseName())) return true; } return false; } public static boolean RepetitiveScores(CourseSelection courseSelection,ArrayList<CourseSelection> courseSelections) { for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentName().equals(courseSelection.getStudent().getStudentName())) { if (cs.getCourse().getCourseName().equals(courseSelection.getCourse().getCourseName())) { return true; } } } return false; } public static void addCourse(String[] nextLine, ArrayList<Course> courses) { Course course = new Course(nextLine[0], nextLine[1], nextLine[2]); if (RepetitiveCourses(course,courses)) return; if (nextLine[0].length() > 10 || (!nextLine[1].equals("必修") && !nextLine[1].equals("选修")) || (!nextLine[2].equals("考试") && (!nextLine[2].equals("考察")))) { System.out.println("wrong format"); return; } if (course.getCourseNature().equals("必修") && !course.getAssessmentMethod().equals("考试")) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); return; } courses.add(course); } public static void addScoredCourseSelection(String[] nextLine, ArrayList<CourseSelection> courseSelections, ArrayList<Student> students, ArrayList<Course> courses) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || !nextLine[4].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } public static void addUnScoredCourseSelection(String[] nextLine, ArrayList<Course> courses, ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new AssessmentResults(Integer.parseInt(nextLine[3])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } } abstract class Score { private int dailyPerformance; // 平时成绩 private int finalGrade; // 期末成绩 public Score(int finalGrade) { this.finalGrade = finalGrade; } public Score(int dailyPerformance, int finalGrade) { this.dailyPerformance = dailyPerformance; this.finalGrade = finalGrade; } public int getDailyPerformance() { return dailyPerformance; } public void setDailyPerformance(int dailyPerformance) { this.dailyPerformance = dailyPerformance; } public int getFinalGrade() { return finalGrade; } public void setFinalGrade(int finalGrade) { this.finalGrade = finalGrade; } public abstract int getTotalScore(); } class ExaminationResults extends Score { public ExaminationResults(int dailyPerformance, int finalGrade) { super(dailyPerformance, finalGrade); } @Override public int getTotalScore() { return (int) (getDailyPerformance() * 0.3 + getFinalGrade() * 0.7); } } class AssessmentResults extends Score { public AssessmentResults(int finalGrade) { super(finalGrade); } public int getTotalScore() { return getFinalGrade(); } } class Course { private String courseName; // 课程名称 private String courseNature; // 课程性质 private String AssessmentMethod; // 考核方式 public Course(String courseName, String courseNature, String assessmentMethod) { this.courseName = courseName; this.courseNature = courseNature; AssessmentMethod = assessmentMethod; } public Course() { } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getCourseNature() { return courseNature; } public void setCourseNature(String courseNature) { this.courseNature = courseNature; } public String getAssessmentMethod() { return AssessmentMethod; } public void setAssessmentMethod(String assessmentMethod) { AssessmentMethod = assessmentMethod; } } class Student { private String studentNumber; private String studentName; public Student(String studentNumber, String studentName) { this.studentNumber = studentNumber; this.studentName = studentName; } public String getStudentNumber() { return studentNumber; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; } public String getStudentName() { return studentName; } } class Class { private int classNumber; private int classAverageScore; private ArrayList<Student> students = new ArrayList<>(); public Class(int classNumber, ArrayList<Student> students) { this.classNumber = classNumber; this.students = students; } public Class() { } public int getClassAverageScore() { return classAverageScore; } public void setClassAverageScore(int classAverageScore) { this.classAverageScore = classAverageScore; } public int getClassNumber() { return classNumber; } public void setClassNumber(int classNumber) { this.classNumber = classNumber; } public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } public void addStudent(Student student) { students.add(student); } } class CourseSelection { private Course course; private Student student; private Score score; public CourseSelection(Course course, Student student, Score score) { this.course = course; this.student = student; this.score = score; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Score getScore() { return score; } public void setScore(Score score) { this.score = score; } }
以上就是代码部分了,讲述一下编写过程,先从各个类的设计开始,再到输入的判断,将其进行分类,再一个个分别去处理输入的数据,构建一个个方法去对应不同的输入数据。
7-1 统计Java程序中关键词的出现次数
编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:
- Java中共有53个关键字(自行百度)
- 从键盘输入一段源码,统计这段源码中出现的关键字的数量
- 注释中出现的关键字不用统计
- 字符串中出现的关键字不用统计
- 统计出的关键字及数量按照关键字升序进行排序输出
- 未输入源码则认为输入非法
输入格式:
输入Java源码字符串,可以一行或多行,以exit
行作为结束标志
输出格式:
- 当未输入源码时,程序输出
Wrong Format
- 当没有统计数据时,输出为空
- 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为
数量\t关键字
-
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<String, Integer> map = new TreeMap<>(); String line; StringBuilder sb = new StringBuilder(); while (true) { line = scanner.nextLine(); if (line.equals("exit")) { break; } sb.append(line.replaceAll("//.*", "").replaceAll("\"(.*?)\"", "")); } String srcCode = sb.toString().replaceAll("\"(.*?)\"","").replaceAll("/\\*(.*?)\\*/",""); String[] words = srcCode.split("\\W"); // 以非单词字符为分隔符切分源码 for (String word : words) { if (isKeyword(word)) { map.put(word, map.getOrDefault(word, 0) + 1); } } if (map.isEmpty()) { System.out.println("Wrong Format"); } else { for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getValue() + "\t" + entry.getKey()); } } } private static boolean isKeyword(String word) { Set<String> keywords = new TreeSet<>(Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" )); for (String keyword : keywords) { if (keyword.equals(word)) { return true; } } return false; } }
总结:了解对set和map的使用。
-
7-3 课程成绩统计程序-2
-
课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩的平均分
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩
实验次数至少4次,不超过9次
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
考试/考察课程成绩格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
实验课成绩格式:课程名称+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
- 总结:这是成绩计算系列的第二个,较之前添加了实验课的内容,题目的整体结构基本没变,所以相应的我添加了ExperimentalResults类。
- 类图如下:
-
import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Class> classes = new ArrayList<>(); ArrayList<Course> courses = new ArrayList<>(); ArrayList<Student> students = new ArrayList<>(); ArrayList<CourseSelection> courseSelections = new ArrayList<>(); Scanner input = new Scanner(System.in); while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } String[] nextLine = str.split(" "); if (nextLine.length == 3) { addCourse(nextLine,courses); } else if (nextLine.length == 5) { addScoredCourseSelection(nextLine,courseSelections,students,courses); } else if (nextLine.length == 4) { addUnScoredCourseSelection(nextLine, courses, students, courseSelections); } else if (nextLine.length >5 ){ addCourseSelection(nextLine, courseSelections, students, courses); } else { System.out.println("wrong format"); } } for (Student student:students ) { int classNumber = Integer.parseInt(student.getStudentNumber()) / 100; if (classes.size() == 0) { Class clas = new Class(); clas.setClassNumber(classNumber); clas.getStudents().add(student); classes.add(clas); } else { ListIterator<Class> iterator = classes.listIterator(); while (iterator.hasNext()) { Class clas = iterator.next(); if (clas.getClassNumber() == classNumber) { clas.getStudents().add(student); break; } else { if (classes.indexOf(clas) == classes.size() - 1) { Class clasNew = new Class(); clasNew.setClassNumber(classNumber); clasNew.getStudents().add(student); iterator.add(clasNew); } } } } } Collections.sort(classes, Comparator.comparingInt(o -> o.getClassNumber())); for (Class clas:classes ) { clas.setClassAverageScore(print1(clas.getStudents(), courseSelections)); } print2(courses, courseSelections); for (Class clas:classes ) { if (clas.getClassAverageScore() == -1) { System.out.println(clas.getClassNumber() + " has no grades yet"); } else System.out.println(clas.getClassNumber() + " " + clas.getClassAverageScore()); } } public static int print1(ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { int classAverageScore = 0; Collections.sort(students, Comparator.comparingInt(o -> Integer.parseInt(o.getStudentNumber()))); for (Student student:students ) { int averageScore = 0; int courseNum = 0; for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentNumber().equals(student.getStudentNumber())) { averageScore += cs.getScore().getTotalScore(); courseNum ++; } } if (courseNum == 0) { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " did not take any exams"); return -1; } else { averageScore /= courseNum; System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " " + averageScore); } classAverageScore += averageScore; } return classAverageScore / students.size(); } public static void print2(ArrayList<Course> courses, ArrayList<CourseSelection> courseSelections) { Collections.sort(courses, (o1, o2) -> { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(o1.getCourseName(), o2.getCourseName()); }); for (Course course:courses ) { int courseAverageScore = 0; int courseDailyPerformance = 0; int courseFinalGrade = 0; int studentNum = 0; for (CourseSelection cs:courseSelections ) { if (course.getCourseName().equals(cs.getCourse().getCourseName())) { courseAverageScore += cs.getScore().getTotalScore(); courseDailyPerformance += cs.getScore().getDailyPerformance(); courseFinalGrade += cs.getScore().getFinalGrade(); studentNum ++; } } if (studentNum == 0) { System.out.println(course.getCourseName() + " has no grades yet"); } else { courseAverageScore /= studentNum; courseDailyPerformance /= studentNum; courseFinalGrade /= studentNum; if (course.getAssessmentMethod().equals("考试")) { System.out.println(course.getCourseName() + " " + courseDailyPerformance + " " + courseFinalGrade + " " + courseAverageScore); } else if(course.getAssessmentMethod().equals("考察")) { System.out.println(course.getCourseName() + " " + courseFinalGrade + " " + courseAverageScore); } else { System.out.println(course.getCourseName()+" "+ courseAverageScore); } } } } public static boolean isCourseExist(String courseName,String num, ArrayList<Course> courses, int length, Student student) { for (Course course:courses ) { if (course.getCourseName().equals(courseName)) { if ((course.getAssessmentMethod().equals("考试") && length != 5) || (course.getAssessmentMethod().equals("考察") && length != 4) || (course.getAssessmentMethod().equals("实验") && (Integer.parseInt(num)+4)!=length)) { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " : access mode mismatch"); return false; } else return true; } } System.out.println(courseName + " does not exist"); return true; } public static boolean RepetitiveCourses(Course course,ArrayList<Course> courses) { for (Course course2:courses ) { if (course2.getCourseName().equals(course.getCourseName())) return true; } return false; } public static boolean RepetitiveScores(CourseSelection courseSelection,ArrayList<CourseSelection> courseSelections) { for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentName().equals(courseSelection.getStudent().getStudentName())) { if (cs.getCourse().getCourseName().equals(courseSelection.getCourse().getCourseName())) { return true; } } } return false; } public static void addCourse(String[] nextLine, ArrayList<Course> courses) { Course course = new Course(nextLine[0], nextLine[1], nextLine[2]); if (RepetitiveCourses(course,courses)) return; if (nextLine[0].length() > 10 || (!nextLine[1].equals("必修") && !nextLine[1].equals("选修") && !nextLine[1].equals("实验")) || (!nextLine[2].equals("考试") && !nextLine[2].equals("考察") && !nextLine[2].equals("实验"))) { System.out.println("wrong format"); return; } if ((course.getCourseNature().equals("必修") && !course.getAssessmentMethod().equals("考试"))||(course.getCourseNature().equals("实验")&&!course.getAssessmentMethod().equals("实验"))||(!course.getCourseNature().equals("实验")&&course.getAssessmentMethod().equals("实验"))) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); return; } courses.add(course); } public static void addScoredCourseSelection(String[] nextLine, ArrayList<CourseSelection> courseSelections, ArrayList<Student> students, ArrayList<Course> courses) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || !nextLine[4].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], nextLine[3],courses, nextLine.length, student)) { Score score = new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } public static void addUnScoredCourseSelection(String[] nextLine, ArrayList<Course> courses, ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], nextLine[3],courses, nextLine.length, student)) { Score score = new AssessmentResults(Integer.parseInt(nextLine[3])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } public static void addCourseSelection (String[] nextLine, ArrayList<CourseSelection> courseSelections, ArrayList<Student> students, ArrayList<Course> courses) { if((!nextLine[3].matches("[4-9]")) || (Integer.parseInt(nextLine[3])+4)!=nextLine.length || nextLine[0].length() != 8 || nextLine[1].length() > 10 || !isScoreExist(nextLine)) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], nextLine[3],courses, nextLine.length, student)) { int score1[] = new int[Integer.parseInt(nextLine[3])]; for(int i=5;i<nextLine.length+1;i++) { score1[i-5] = Integer.parseInt(nextLine[i-1]); } Score score = new ExperimentalResults(score1); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } public static boolean isScoreExist (String[] nextLine) { for(int i=3;i<nextLine.length;i++) { if(!nextLine[i].matches("100|[1-9][0-9]|[0-9]")) return false; } return true; } } abstract class Score { private int dailyPerformance; // 平时成绩 private int finalGrade; // 期末成绩 private int experimentalGrade[]; // 实验成绩 public Score(int finalGrade) { this.finalGrade = finalGrade; } public Score(int dailyPerformance, int finalGrade) { this.dailyPerformance = dailyPerformance; this.finalGrade = finalGrade; } public Score(int[] experimentalGrade) { this.experimentalGrade = experimentalGrade; } public int getDailyPerformance() { return dailyPerformance; } public void setDailyPerformance(int dailyPerformance) { this.dailyPerformance = dailyPerformance; } public int getFinalGrade() { return finalGrade; } public void setFinalGrade(int finalGrade) { this.finalGrade = finalGrade; } public int[] getExperimentalGrade() { return experimentalGrade; } public void setExperimentalGrade(int[] experimentalGrade) { this.experimentalGrade = experimentalGrade; } public abstract int getTotalScore(); } class ExaminationResults extends Score { public ExaminationResults(int dailyPerformance, int finalGrade) { super(dailyPerformance, finalGrade); } @Override public int getTotalScore() { return (int) (getDailyPerformance() * 0.3 + getFinalGrade() * 0.7); } } class AssessmentResults extends Score { public AssessmentResults(int finalGrade) { super(finalGrade); } @Override public int getTotalScore() { return getFinalGrade(); } } class ExperimentalResults extends Score { public ExperimentalResults(int[] experimentalGrade) { super(experimentalGrade); } @Override public int getTotalScore() { int Totalscore = 0; for(int i=0;i< getExperimentalGrade().length;i++) { Totalscore += getExperimentalGrade()[i]; } return Totalscore/(getExperimentalGrade().length); } } class Course { private String courseName; // 课程名称 private String courseNature; // 课程性质 private String AssessmentMethod; // 考核方式 public Course(String courseName, String courseNature, String assessmentMethod) { this.courseName = courseName; this.courseNature = courseNature; AssessmentMethod = assessmentMethod; } public Course() { } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getCourseNature() { return courseNature; } public void setCourseNature(String courseNature) { this.courseNature = courseNature; } public String getAssessmentMethod() { return AssessmentMethod; } public void setAssessmentMethod(String assessmentMethod) { AssessmentMethod = assessmentMethod; } } class Student { private String studentNumber; private String studentName; public Student(String studentNumber, String studentName) { this.studentNumber = studentNumber; this.studentName = studentName; } public String getStudentNumber() { return studentNumber; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; } public String getStudentName() { return studentName; } } class Class { private int classNumber; private int classAverageScore; private ArrayList<Student> students = new ArrayList<>(); public Class(int classNumber, ArrayList<Student> students) { this.classNumber = classNumber; this.students = students; } public Class() { } public int getClassAverageScore() { return classAverageScore; } public void setClassAverageScore(int classAverageScore) { this.classAverageScore = classAverageScore; } public int getClassNumber() { return classNumber; } public void setClassNumber(int classNumber) { this.classNumber = classNumber; } public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } public void addStudent(Student student) { students.add(student); } } class CourseSelection { private Course course; private Student student; private Score score; public CourseSelection(Course course, Student student, Score score) { this.course = course; this.student = student; this.score = score; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Score getScore() { return score; } public void setScore(Score score) { this.score = score; } }
7-2 课程成绩统计程序-3
-
程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,
要求:修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。
完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。
题目最后的参考类图未做修改,大家根据要求自行调整,以下内容加粗字体显示的内容为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩乘以权重后累加而得。
课程权重值在录入课程信息时输入。(注意:所有分项成绩的权重之和应当等于1)
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式、分项成绩数量、每个分项成绩的权重。
考试课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+平时成绩的权重+英文空格+期末成绩的权重
考察课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
实验课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+分项成绩数量n+英文空格+分项成绩1的权重+英文空格+。。。+英文空格+分项成绩n的权重
实验次数至少4次,不超过9次
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程成绩信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、每次成绩{在系列-2的基础上去掉了(实验次数),实验次数要和实验课程信息中输入的分项成绩数量保持一致}
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
1)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
2)学号由8位数字组成
3)姓名不超过10个字符
4)课程名称不超过10个字符
5)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免四舍五入误差,
计算单个成绩时,分项成绩乘以权重后要保留小数位,计算总成绩时,累加所有分项成绩的权重分以后,再去掉小数位。
学生总成绩/整个班/课程平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩按课程名称的字符顺序输出
课程成绩输出格式:课程名称+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
6)如果解析实验课程信息时,输入的分项成绩数量值和分项成绩权重的个数不匹配,输出:课程名称+" : number of scores does not match"
7)如果解析考试课、实验课时,分项成绩权重值的总和不等于1,输出:课程名称+" : weight value error"
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
- 总结:这次较前次实验的总成绩等于课程每次实验成绩乘以权重后累加而得,并且要修改结构,将继承改为组合。主要是要将成绩类个修改一下,并且要注意权重是有浮点误差。
- 类图如下
-
import java.text.Collator; import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Class> classes = new ArrayList<>(); ArrayList<Course> courses = new ArrayList<>(); ArrayList<Student> students = new ArrayList<>(); ArrayList<CourseSelection> courseSelections = new ArrayList<>(); Scanner input = new Scanner(System.in); while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } String[] nextLine = str.split(" "); if (nextLine[1].equals("必修") || nextLine[1].equals("选修") || nextLine[1].equals("实验")) { addCourse(nextLine,courses); } else if (nextLine.length == 5) { addScoredCourseSelection1(nextLine,courseSelections,students,courses); } else if (nextLine.length == 4) { addScoredCourseSelection2(nextLine, courses, students, courseSelections); } else if (nextLine.length > 5) { addScoredCourseSelection3(nextLine,courses,students,courseSelections); } else { System.out.println("wrong format"); } } for (Student student:students ) { int classNumber = Integer.parseInt(student.getStudentNumber()) / 100; if (classes.size() == 0) { Class clas = new Class(); clas.setClassNumber(classNumber); clas.getStudents().add(student); classes.add(clas); } else { ListIterator<Class> iterator = classes.listIterator(); while (iterator.hasNext()) { Class clas = iterator.next(); if (clas.getClassNumber() == classNumber) { clas.getStudents().add(student); break; } else { if (classes.indexOf(clas) == classes.size() - 1) { Class clasNew = new Class(); clasNew.setClassNumber(classNumber); clasNew.getStudents().add(student); iterator.add(clasNew); } } } } } Collections.sort(classes, Comparator.comparingInt(o -> o.getClassNumber())); for (Class clas:classes ) { clas.setClassAverageScore(print1(clas.getStudents(), courseSelections)); } print2(courses, courseSelections); for (Class clas:classes ) { if (clas.getClassAverageScore() == -1) { System.out.println(clas.getClassNumber() + " has no grades yet"); } else System.out.println(clas.getClassNumber() + " " + clas.getClassAverageScore()); } } public static int print1(ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { int classAverageScore = 0; Collections.sort(students, Comparator.comparingInt(o -> Integer.parseInt(o.getStudentNumber()))); for (Student student:students ) { int averageScore = 0; int courseNum = 0; for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentNumber().equals(student.getStudentNumber())) { averageScore += cs.getScore().getTotalScore(); courseNum ++; } } if (courseNum == 0) { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " did not take any exams"); return -1; } else { averageScore /= courseNum; System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " " + averageScore); } classAverageScore += averageScore; } return classAverageScore / students.size(); } public static void print2(ArrayList<Course> courses, ArrayList<CourseSelection> courseSelections) { Collections.sort(courses, (o1, o2) -> { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(o1.getCourseName(), o2.getCourseName()); }); for (Course course:courses ) { int courseAverageScore = 0; int studentNum = 0; for (CourseSelection cs:courseSelections ) { if (course.getCourseName().equals(cs.getCourse().getCourseName())) { courseAverageScore += cs.getScore().getTotalScore(); studentNum ++; } } if (studentNum == 0) { System.out.println(course.getCourseName() + " has no grades yet"); } else { courseAverageScore /= studentNum; System.out.println(course.getCourseName() + " " + courseAverageScore); } } } public static boolean isCourseExist(String courseName, ArrayList<Course> courses, int length, Student student) { for (Course course:courses ) { if (course.getCourseName().equals(courseName)) { if ((course.getAssessmentMethod().equals("考试") && length == 5) || (course.getAssessmentMethod().equals("考察") && length == 4) || (course.getAssessmentMethod().equals("实验") && length == course.getWeightNumber())) { return true; } else { System.out.println(student.getStudentNumber() + " " + student.getStudentName() + " : access mode mismatch"); return false; } } } System.out.println(courseName + " does not exist"); return false; } public static boolean RepetitiveCourses(String courseName,ArrayList<Course> courses) { for (Course cou:courses ) { if (cou.getCourseName().equals(courseName)) return true; } return false; } public static boolean RepetitiveScores(CourseSelection courseSelection,ArrayList<CourseSelection> courseSelections) { for (CourseSelection cs:courseSelections ) { if (cs.getStudent().getStudentName().equals(courseSelection.getStudent().getStudentName())) { if (cs.getCourse().getCourseName().equals(courseSelection.getCourse().getCourseName())) { return true; } } } return false; } public static void addCourse(String[] nextLine, ArrayList<Course> courses) { if (RepetitiveCourses(nextLine[0],courses)) return; if (nextLine[0].length() > 10 || (!nextLine[2].equals("考试") && !nextLine[2].equals("考察") && !nextLine[2].equals("实验"))) { System.out.println("wrong format"); return; } int weightNumber; double allWeight = 0; ArrayList<Double> weights = new ArrayList<>(); if (nextLine[1].equals("必修")) { if (nextLine.length != 5) { System.out.println(nextLine[0] + " : number of scores does not match"); return; } weightNumber = 2; weights.add(Double.parseDouble(nextLine[3])); weights.add(Double.parseDouble(nextLine[4])); for (double weight:weights ) { allWeight += weight; } } else if (nextLine[1].equals("选修")) { weightNumber = 1; allWeight = 1; weights.add(1.0); } else if (nextLine[1].equals("实验")) { weightNumber = Integer.parseInt(nextLine[3]); if (nextLine.length != 4 + weightNumber) { System.out.println(nextLine[0] + " : number of scores does not match"); return; } if (!nextLine[3].matches("[4-9]")) { System.out.println("wrong format"); return; } for (int i = 4;i < nextLine.length;i ++) { weights.add(Double.parseDouble(nextLine[i])); allWeight += Double.parseDouble(nextLine[i]); } } else { System.out.println("wrong format"); return; } if (allWeight != 1) { System.out.println(nextLine[0] + " : weight value error"); return; } Course course = new Course(nextLine[0], nextLine[1], nextLine[2], weightNumber, weights); if ((course.getCourseNature().equals("必修") && !course.getAssessmentMethod().equals("考试")) || (course.getCourseNature().equals("实验") && !course.getAssessmentMethod().equals("实验"))) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); return; } courses.add(course); } public static void addScoredCourseSelection1(String[] nextLine, ArrayList<CourseSelection> courseSelections, ArrayList<Student> students, ArrayList<Course> courses) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || !nextLine[4].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { Score score = new Score(new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])), course); CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) break; courseSelections.add(courseSelection); } } } } public static void addScoredCourseSelection2(String[] nextLine, ArrayList<Course> courses, ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { if (!nextLine[3].matches("100|[1-9][0-9]|[0-9]") || nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { Score score = new Score(new AssessmentResults(Integer.parseInt(nextLine[3])), course); CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) break; courseSelections.add(courseSelection); } } } } public static void addScoredCourseSelection3(String[] nextLine, ArrayList<Course> courses, ArrayList<Student> students, ArrayList<CourseSelection> courseSelections) { ArrayList<Integer> results = new ArrayList<>(); if (nextLine[0].length() != 8 || nextLine[1].length() > 10) { System.out.println("wrong format"); return; } for (int i = 3;i < nextLine.length;i ++) { if (!nextLine[i].matches("100|[1-9][0-9]|[0-9]")) { System.out.println("wrong format"); return; } results.add(Integer.parseInt(nextLine[i])); } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length - 3, student)) { for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { Score score = new Score(new ExperimentalResults(results), course); CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) break; courseSelections.add(courseSelection); } } } } } class Score { private ExaminationResults examinationResults; private AssessmentResults assessmentResults; private ExperimentalResults experimentalResults; private Course course; public Score(ExaminationResults examinationResults, Course course) { this.examinationResults = examinationResults; this.course = course; } public Score(AssessmentResults assessmentResults, Course course) { this.assessmentResults = assessmentResults; this.course = course; } public Score(ExperimentalResults experimentalResults, Course course) { this.experimentalResults = experimentalResults; this.course = course; } public int getTotalScore() { if (course.getAssessmentMethod().equals("考试")) { return (int) (examinationResults.getDailyPerformance() * course.getWeight().get(0) + examinationResults.getFinalGrade() * course.getWeight().get(1)); } else if (course.getAssessmentMethod().equals("考察")) { return assessmentResults.getTotalScore(); } else { double totalScore = 0; int i = 0; for (int result:experimentalResults.getResults() ) { totalScore += result * course.getWeight().get(i); i ++; } return (int) (totalScore); } } } class ExaminationResults { private int dailyPerformance; private int finalGrade; public ExaminationResults(int dailyPerformance, int finalGrade) { this.dailyPerformance = dailyPerformance; this.finalGrade = finalGrade; } public int getDailyPerformance() { return dailyPerformance; } public void setDailyPerformance(int dailyPerformance) { this.dailyPerformance = dailyPerformance; } public int getFinalGrade() { return finalGrade; } public void setFinalGrade(int finalGrade) { this.finalGrade = finalGrade; } } class AssessmentResults { private int totalScore; public int getTotalScore() { return totalScore; } public void setTotalScore(int totalScore) { this.totalScore = totalScore; } public AssessmentResults(int totalScore) { this.totalScore = totalScore; } } class ExperimentalResults { private ArrayList<Integer> results = new ArrayList<>(); public ExperimentalResults(ArrayList<Integer> results) { this.results = results; } public ArrayList<Integer> getResults() { return results; } public void setResults(ArrayList<Integer> results) { this.results = results; } } class Course { private String courseName; // 课程名称 private String courseNature; // 课程性质 private String AssessmentMethod; // 考核方式 private int weightNumber; private ArrayList<Double> weight; public Course(String courseName, String courseNature, String assessmentMethod, int weightNumber, ArrayList<Double> weight) { this.courseName = courseName; this.courseNature = courseNature; AssessmentMethod = assessmentMethod; this.weightNumber = weightNumber; this.weight = weight; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getCourseNature() { return courseNature; } public void setCourseNature(String courseNature) { this.courseNature = courseNature; } public String getAssessmentMethod() { return AssessmentMethod; } public void setAssessmentMethod(String assessmentMethod) { AssessmentMethod = assessmentMethod; } public int getWeightNumber() { return weightNumber; } public void setWeightNumber(int weightNumber) { this.weightNumber = weightNumber; } public ArrayList<Double> getWeight() { return weight; } public void setWeight(ArrayList<Double> weight) { this.weight = weight; } } class Student { private String studentNumber; private String studentName; public Student(String studentNumber, String studentName) { this.studentNumber = studentNumber; this.studentName = studentName; } public String getStudentNumber() { return studentNumber; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; } public String getStudentName() { return studentName; } } class Class { private int classNumber; private int classAverageScore; private ArrayList<Student> students = new ArrayList<>(); public Class(int classNumber, ArrayList<Student> students) { this.classNumber = classNumber; this.students = students; } public Class() { } public int getClassAverageScore() { return classAverageScore; } public void setClassAverageScore(int classAverageScore) { this.classAverageScore = classAverageScore; } public int getClassNumber() { return classNumber; } public void setClassNumber(int classNumber) { this.classNumber = classNumber; } public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } public void addStudent(Student student) { students.add(student); } } class CourseSelection { private Course course; private Student student; private Score score; public CourseSelection(Course course, Student student, Score score) { this.course = course; this.student = student; this.score = score; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Score getScore() { return score; } public void setScore(Score score) { this.score = score; } }
三、采坑心得
我觉得在训练集9这道题吧,就我自己的操作是把输入里全部无关紧要的符号全部替换成了空格,但是又会有问题,然而只用正则表达式又不能去掉没用的符合。但把二者结合就不会了,我就一个个试着先把没有的符合去掉,再用正则表判断了。
还有就是最后那次成绩计算系列那道题了,就那个权重和浮点误差的坑了我好久,其实浮点数都又这个问题,以后养成习惯遇到浮点数就要有这个觉悟就行了。
-
四、改进建议
要改进的就感觉得加点注释了,就比如我现在自己去看都会有点云里雾里的感觉。就成绩计算系列中主函数还是写得太复杂了些,感觉还是没有做到面对对象,导致了后面修改也很复制,写得感觉有点乱,希望日后能有所改进吧。
五、总结
这个阶段虽然感觉比较难,但也确实学到了很多东西,比如说整个javafx的学习以及相关的作业,可能自己做得并不好,但也确实熟悉了javafx。还有就是map和set的学习,这两个一开始还只是在网上搜代码看到了,直到自己用还有训练,也确实真正学习到了,并且也是真好用。此外,就是stock这些,刚开始学是真有点不懂,直到自己摸索以及训练,才真正懂得了其中的奥秘。总之就是要肯下功夫,有时候真正学会了一样东西是真的会有成就感。
六、评价
最后评价一下吧,学Java这门课也有一个学期了,对于段老师边讲边练、以练代学的教学方法,一开始是有点不知所措,但也确实可以学的很多知识,这种方法也让我后期Java的学习变得可以游刃有余了。至于教学组织的线上线下混合式教学,我感觉好像不是很有用,因为网上的课我看得并不是很认真,而且自己学、看课本、查资料也能行。教学过程的pta题目级驱动,我感觉真的很有必要,自己时刻保持这种状态去学习是真的可以学到东西。至于教学模式,感觉比较新颖,比如说javafx这一章的,通过小组学习里面的内容,再让小组亲自讲,激发了我非常大的好奇心,因为小组要亲自讲,所以自己肯定要学得比较透彻,别的大部分小组也讲的很好,这样确实激发了学习的兴趣。总的来说,java这门课还是学得比较有趣,如果pta作业能多延几天就更好了。
标签:11,nextLine,总结性,return,ArrayList,Blog,course,student,public From: https://www.cnblogs.com/TTTTZ/p/17510206.html