一、前言
麻烦各位帅哥美女给高点分儿,我每次互评都给了90分以上!感谢!
1.知识点:
LinkedHashMap和HashMap是有区别的,前者是按插入顺序储存元素,后者是哈希表
排序会在后面讲
List ab = studenttotalscoremap.computeIfAbsent(grade.student, k -> Arrays.asList(0.0, 0.0));
ab.set(0, ab.get(0) + totalscore);这种方式可以计算指定位置的元素
其实主要还是逻辑问题,我所运用的知识点并没有太多
2.题量:不是太大,小题写的会快些,但是系列题找测试点的过程有时候很费时间
3.难度:不追求满分的话倒还可以,但是有的测试点是真找不到
二、设计与分析
7-3 课程成绩统计程序-2 分数 60 作者 蔡轲 单位 南昌航空大学课程成绩统计程序-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)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
输入样例1:
在这里给出一组输入。例如:
java 实验 实验
20201103 张三 java 4 70 80 90
end
输出样例1:
在这里给出相应的输出。例如:
20201103 张三 : access mode mismatch
20201103 张三 did not take any exams
java has no grades yet
202011 has no grades yet
输入样例2:
在这里给出一组输入。例如:
java 实验 实验
20201103 张三 java 3 70 80 90
end
输出样例2:
在这里给出相应的输出。例如:
wrong format
java has no grades yet
输入样例3:
在这里给出一组输入。例如:
java 必修 实验
20201103 张三 java 3 70 80 90 100
end
输出样例3:
在这里给出相应的输出。例如:
java : course type & access mode mismatch
wrong format
输入样例4:
在这里给出一组输入。例如:
java 必修 实验
20201103 张三 java 4 70 80 90 105
end
输出样例4:
在这里给出相应的输出。例如:
java : course type & access mode mismatch
wrong format
输入样例5:
在这里给出一组输入。例如:
java 选修 考察
C语言 选修 考察
java实验 实验 实验
编译原理 必修 考试
20201101 王五 C语言 76
20201216 李四 C语言 78
20201307 张少军 编译原理 82 84
20201103 张三 java实验 4 70 80 90 100
20201118 郑觉先 java 80
20201328 刘和宇 java 77
20201220 朱重九 java实验 4 60 60 80 80
20201132 王萍 C语言 40
20201302 李梦涵 C语言 68
20201325 崔瑾 编译原理 80 84
20201213 黄红 java 82
20201209 赵仙芝 java 76
end
输出样例5:
在这里给出相应的输出。例如:
20201101 王五 76
20201103 张三 85
20201118 郑觉先 80
20201132 王萍 40
20201209 赵仙芝 76
20201213 黄红 82
20201216 李四 78
20201220 朱重九 70
20201302 李梦涵 68
20201307 张少军 83
20201325 崔瑾 82
20201328 刘和宇 77
C语言 65 65
java 78 78
java实验 77
编译原理 81 84 82
202011 70
202012 76
202013 77
代码:import java.text.Collator;
import java.util.*;
public class Main {
public static void main(String[] args){
Classes classes = new Classes();
Courses courses = new Courses();
Scanner input = new Scanner(System.in);
while (true){
String str = input.nextLine();
String[] splitedStr = str.split(" ");
if (str.equals("end")){
break;
}
if (str.matches(".{1,10} 必修 实验") || str.matches(".{1,10} 必修 考察") ||
str.matches(".{1,10} 选修 实验") || str.matches(".{1,10} 实验 考察") ||
str.matches(".{1,10} 实验 考试")){
System.out.println(splitedStr[0] + " : course type & access mode mismatch");
} else if (str.matches(".{1,10} 必修 考试")){
if (courses.findAClassByName(splitedStr[0]) == null) {
Course course = new Course(splitedStr[0], splitedStr[1], splitedStr[2]);
courses.addACourse(course);
}
} else if (str.matches(".{1,10} 选修 考察")){
if (courses.findAClassByName(splitedStr[0]) == null) {
Course course = new Course(splitedStr[0], splitedStr[1], splitedStr[2]);
courses.addACourse(course);
}
} else if (str.matches(".{1,10} 选修 考试")){
if (courses.findAClassByName(splitedStr[0]) == null) {
Course course = new Course(splitedStr[0], splitedStr[1], splitedStr[2]);
courses.addACourse(course);
}
} else if (str.matches(".{1,10} 实验 实验")){
if (courses.findAClassByName(splitedStr[0]) == null) {
Course course = new Course(splitedStr[0], splitedStr[1], splitedStr[2]);
courses.addACourse(course);
}
}else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} (100|0|[1-9]\\d?) (100|0|[1-9]\\d?)")){//考试,录入学生信息
String classNum = splitedStr[0].substring(0,6);//获得该学生学号的前六位,也就是班级号
if (courses.courseExistence(splitedStr[2])) {//先查找此门课程是否存在在courses中
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("考试")) {
if (classes.ifAClassExist(classNum)) {//查找此学号前六位对应的班级号存在不存在
if (classes.findAClassByNum(classNum).isStudentExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).getName().equals(splitedStr[1])) {//查找该学号的学生是否存在
if (!classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).addData(course, splitedStr[3], splitedStr[4]);//先查找班级,再查找该学号的学生,最后将该门课程的考试信息录入
course.addAExamGrade(new ExamGrade(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4])));//将此学生的该门课程成绩录入
}
} else if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {//不存在则建立一个新的学生对象存入该班号的学生ArrayList中
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名称查找该课程
student.addData(course, splitedStr[3], splitedStr[4]);//将课程以及课程成绩录入该学生的信息中
classes.findAClassByNum(classNum).addAStudent(student);//将该学生录入这个班级当中
course.addAExamGrade(new ExamGrade(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4])));//将此学生的该门课程成绩录入
}
} else {//否则就新建立一个班级的信息,并且存到classes中
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
student.addData(course, splitedStr[3], splitedStr[4]);//将课程信息和课程成绩录入该学生的信息中
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
course.addAExamGrade(new ExamGrade(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4])));//将此学生的该门课程成绩录入
}
}else{
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
}else {
System.out.println(splitedStr[2] + " does not exist");
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
}
} else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} (100|0|[1-9]\\d?)")){//考察,录入学生信息
String classNum = splitedStr[0].substring(0,6);//获得该学生学号的前六位,也就是班级号
if (courses.courseExistence(splitedStr[2])) {//先查找此门课程是否存在在courses中
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("考察")) {
if (classes.ifAClassExist(classNum)) {//查找此学号前六位对应的班级号存在不存在
if (classes.findAClassByNum(classNum).isStudentExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).getName().equals(splitedStr[1])) {//查找该学号的学生是否存在
if (!classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).addData(course, splitedStr[3]);//先查找班级,再查找该学号的学生,最后将该门课程的考试信息录入
course.addAInspectGrade(new InspectGrade(Integer.parseInt(splitedStr[3])));//将此学生的该门课程成绩录入
}
} else if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {//不存在则建立一个新的学生对象存入该班号的学生ArrayList中
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名称查找该课程
student.addData(course, splitedStr[3]);//将课程以及课程成绩录入该学生的信息中
classes.findAClassByNum(classNum).addAStudent(student);//将该学生录入这个班级当中
course.addAInspectGrade(new InspectGrade(Integer.parseInt(splitedStr[3])));//将此学生的该门课程成绩录入
}
} else {//否则就新建立一个班级的信息,并且存到classes中
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
student.addData(course, splitedStr[3]);//将课程信息和课程成绩录入该学生的信息中
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
course.addAInspectGrade(new InspectGrade(Integer.parseInt(splitedStr[3])));//将此学生的该门课程成绩录入
}
}else{
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
}else {
System.out.println(splitedStr[2] + " does not exist");
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
}
} else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} [4-9]( (100|0|[1-9]\\d?))*")){
String classNum = splitedStr[0].substring(0,6);//获得该学生学号的前六位,也就是班级号
if ((splitedStr.length - 4) == Integer.parseInt(splitedStr[3])){
if (courses.courseExistence(splitedStr[2])) {//先查找此门课程是否存在在courses中
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("实验")) {
ArrayList<Integer> experiments = new ArrayList<>();
for (int i = 4;i < splitedStr.length;i ++){
experiments.add(Integer.parseInt(splitedStr[i]));
}
if (classes.ifAClassExist(classNum)){//班级是否存在
if (classes.findAClassByNum(classNum).isStudentExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).getName().equals(splitedStr[1])){//班级存在的话学生是否存在
if (!classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
classes.findAClassByNum(classNum).findAStudentByNum(splitedStr[0]).addData(course, experiments);
course.addAExperimentGrade(new ExperimentGrade(experiments));
}
} else if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])){//学生不存在
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
student.addData(course,experiments);//录入学生信息
classes.findAClassByNum(classNum).addAStudent(student);
course.addAExperimentGrade(new ExperimentGrade(experiments));//录入课程成绩
}
}else{//班级不存在
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
Course course = courses.findAClassByName(splitedStr[2]);//通过课程名字查找该课程
student.addData(course,experiments);//录入学生信息
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
course.addAExperimentGrade(new ExperimentGrade(experiments));//录入课程成绩
}
}else{
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
}else{
System.out.println(splitedStr[2] + " does not exist");
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
}
} else{//不匹配也要录入学生的信息
Student student = new Student(splitedStr[0], splitedStr[1]);//学生的名字和学号
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStudentExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStudent(student);
}
}else {
Class newClass = new Class(classNum, student);//将班号和该名学生录入这个新的班级中
classes.addAClass(newClass);//将这个新的班级加入classes
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
} else{
if (!str.matches("end")) {
System.out.println("wrong format");
}
}
}
classes.setStudentsAve();
classes.setClassesAve();
courses.setCoursesAve();
classes.sort();
courses.sort();
classes.showStudents();
courses.showCourses();
classes.showClasses();
}
}
class ExamGrade{
private int usualScore;//平时成绩
private int examScore;//考试成绩
private int totalScore;
public int getUsualScore() {
return usualScore;
}
public int getExamScore() {
return examScore;
}
public Boolean isValidScore(String score){
if (!score.matches("0|[1-9]|[1-9]\\d|100")){
System.out.println("wrong format");
return false;
}
return true;
}
public void setUsualScore(int usualScore) {
this.usualScore = usualScore;
}
public void setExamScore(int examScore) {
this.examScore = examScore;
}
public ExamGrade() {
}
public ExamGrade(int usualScore, int examScore) {
this.usualScore = usualScore;
this.examScore = examScore;
}
public void setTotalScore(){
totalScore = (int)(usualScore * 0.3 + examScore * 0.7);
}
public int getTotalScore() {
return totalScore;
}
}
class InspectGrade{
private int grade;//考察成绩
public InspectGrade() {
}
public InspectGrade(int grade) {
this.grade = grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
}
class ExperimentGrade{
private ArrayList<Integer> grades = new ArrayList<>();//每次实验成绩
private int totalScore;
public void addAGrade(int grade){
grades.add(grade);
}
public void calculateTotalScore(){
int sum = 0;
for (int g : grades){
sum += g;
}
totalScore = sum / grades.size();
}
public int getTotalScore() {
return totalScore;
}
public ExperimentGrade() {
}
public ExperimentGrade(ArrayList<Integer> grades) {
this.grades = grades;
}
}
class Student implements Comparable<Student>{
private ArrayList<StudentData> data = new ArrayList<>();
private String num;
private String name;
private int averageGrade = -1;
public Student(String num, String name) {
if (name.length() < 10){
this.name = name;
}else{
System.out.println("wrong format");
}
if (num.length() == 8){
this.num = num;
}else{
System.out.println("wrong format");
}
}
public void setAverageGrade(){
int sum = 0;//sum计算该学生成绩的综合
if (data.size() != 0) {
for (StudentData s : data) {
sum += s.getGrade();
}
averageGrade = sum / data.size();
}
}
public String getName() {
return name;
}
public String getNum() {
return num;
}
public void addData(Course course,String score1/*平时成绩*/,String score2/*考试成绩*/){//addData方法有两个重载,一个是有平时成绩和考试成绩的方法,另外一个是只有考察成绩的方法
ExamGrade examGrade = new ExamGrade();
examGrade.setUsualScore(Integer.parseInt(score1));
examGrade.setExamScore(Integer.parseInt(score2));
data.add(new StudentData(course,examGrade));
}
public void addData(Course course,String score){
InspectGrade inspectGrade = new InspectGrade();
inspectGrade.setGrade(Integer.parseInt(score));
data.add(new StudentData(course,inspectGrade));
}
public void addData(Course course,ArrayList<Integer> grades){
ExperimentGrade experimentGrade = new ExperimentGrade(grades);
data.add(new StudentData(course,experimentGrade));
}
public int getAverageGrade() {
return averageGrade;
}
public void setGrades(){
for (StudentData d : data){
d.setGrade();
}
}
@Override
public int compareTo(Student o) {
return num.compareTo(o.getNum());
}
public boolean finACourseExist(String str){
for (StudentData s : data){
if (s.getCourse().getName().equals(str)){
return true;
}
}
return false;
}
}
class Course implements Comparable<Course>{
private String name;//课程名称
private String type;//课程类型
private String examing;//考试类型
private ArrayList<ExamGrade> examGrades = new ArrayList<>();//三个ArrayList根据考核方式只会用到一个
private ArrayList<InspectGrade> inspectGrades = new ArrayList<>();//三个ArrayList根据考核方式只会用到一个
private ArrayList<ExperimentGrade> experimentGrades = new ArrayList<>();//三个ArrayList根据考核方式只会用到一个
private int averageUsualGrade = -1;//平时
private int averageExamGrade = -1;//考试
private int averageExperiment = -1;//实验
private int averageGrade = -1;//考察
private int averageScore = -1;//总的平均
public Course(String name, String type, String examing) {
this.name = name;
this.type = type;
this.examing = examing;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getExaming() {
return examing;
}
public void addAExamGrade(ExamGrade examGrade){
examGrades.add(examGrade);
}
public void addAInspectGrade(InspectGrade inspectGrade){
inspectGrades.add(inspectGrade);
}
public void addAExperimentGrade(ExperimentGrade experimentGrade){
experimentGrades.add(experimentGrade);
}
public void setAverageScore(){
int examSum = 0,usualSum = 0,sum = 0;
if (examing.equals("考试") && examGrades.size() != 0){
for (ExamGrade e : examGrades){
e.setTotalScore();
examSum += e.getExamScore();
usualSum += e.getUsualScore();
sum += e.getTotalScore();
}
averageExamGrade = examSum / examGrades.size();
averageUsualGrade = usualSum / examGrades.size();
averageScore = sum / examGrades.size();
}
if (examing.equals("考察") && inspectGrades.size() != 0){
for (InspectGrade i : inspectGrades){
sum += i.getGrade();
}
averageGrade = sum / inspectGrades.size();
averageScore = averageGrade;
}
if (examing.equals("实验") && experimentGrades.size() != 0){
for (ExperimentGrade e : experimentGrades){
e.calculateTotalScore();
sum += e.getTotalScore();
}
averageScore = sum / experimentGrades.size();
}
}
@Override
public int compareTo(Course o) {
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(this.name,o.getName());
}
public int getAverageUsualGrade() {
return averageUsualGrade;
}
public int getAverageExamGrade() {
return averageExamGrade;
}
public int getAverageScore() {
return averageScore;
}
}
class Courses{
ArrayList<Course> courses = new ArrayList<>();
public void addACourse(Course course){
courses.add(course);
}
public Boolean courseExistence(String str){//查找课程是否存在
for (Course c : courses){
if (c.getName().equals(str)){
return true;
}
}
return false;
}
public void inspectIsRightExaming(){//检验必修类的课程的考察方式是否为考试
ArrayList<Course> C = new ArrayList<>();
for (Course c : courses){
if (c.getType().equals("必修")){
if (c.getExaming().equals("考察")){
System.out.println( c.getName() + " : course type & access mode mismatch");
C.add(c);
}
}
}
for (Course c : C){
courses.remove(c);
}
}
public Course findAClassByName(String name){//通过名字查找课程,并且返回这个课程的信息
for (Course c : courses){
if (name.equals(c.getName())){
return c;
}
}
return null;//永远不会走到这一步,因为使用这个方法的前提是先查找这门课程是否存在,所以放心
}
public void setCoursesAve(){
for (Course c : courses){
c.setAverageScore();
}
}
public void sort(){
Collections.sort(courses);
}
public void showCourses(){
for (Course c : courses){
if (c.getAverageScore() != -1) {
if (c.getExaming().equals("考试")) {
System.out.println(c.getName() + " " + c.getAverageUsualGrade() + " " + c.getAverageExamGrade() + " " + c.getAverageScore());
} else if (c.getExaming().equals("考察")){
System.out.println(c.getName() + " " + c.getAverageScore() + " " + c.getAverageScore());
}else{
System.out.println(c.getName() + " " + c.getAverageScore());
}
}else{
System.out.println(c.getName() + " has no grades yet");
}
}
}
}
class Class implements Comparable<Class>{
private ArrayList<Student> students = new ArrayList<>();
private int averageScore = -1;
private String num;
public String getNum() {
return num;
}
public Class(String num,Student student) {
students.add(student);
this.num = num;
}
public Boolean isStudentExist(String str){//对应学号的学生是否存在
for (Student s : students){
if (s.getNum().equals(str)){
return true;
}
}
return false;
}
public void addAStudent(Student student){
students.add(student);
}
public Student findAStudentByNum(String str){
for (Student s : students){
if (s.getNum().equals(str)){
return s;
}
}
return null;//同样,因为先判断该学号的学生是否存在所以不用担心会返回null
}
public void setAverageScore(){
int sum = 0,i = 0;
if (students.size() != 0) {
for (Student s : students) {
if (s.getAverageGrade() != -1) {
sum += s.getAverageGrade();
i ++;
}
}
if (i != 0){
this.averageScore = sum / i;
}
}
}
@Override
public int compareTo(Class o) {
return num.compareTo(o.getNum());
}
public ArrayList<Student> getStudents() {
return students;
}
public int getAverageScore() {
return averageScore;
}
}
class StudentData{//学生单门课程的的信息
private Course course;
private ExamGrade totalGrade;//考试成绩
private InspectGrade grade;//考查成绩
private ExperimentGrade experimentGrade;
private int Grade;
public void setGrade(){
if (course.getExaming().equals("考试")){
Grade = (int) (totalGrade.getUsualScore() * 0.3 + totalGrade.getExamScore() * 0.7);
}else if (course.getExaming().equals("考察")){
Grade = grade.getGrade();
}else{
experimentGrade.calculateTotalScore();
Grade = experimentGrade.getTotalScore();
}
}
public int getGrade() {
return Grade;
}
public StudentData(Course course,ExamGrade examGrade){
this.course = course;
totalGrade = examGrade;
}
public StudentData(Course course, InspectGrade inspectGrade){
this.course = course;
this.grade = inspectGrade;
}
public StudentData(Course course,ExperimentGrade experimentGrade){
this.course = course;
this.experimentGrade = experimentGrade;
}
public Course getCourse() {
return course;
}
}
class Classes{
private ArrayList<Class> classes = new ArrayList<>();
public void addAClass(Class newClass){//在调用这个方法之前,会先调用班级是否存在方法
classes.add(newClass);
}
public Boolean ifAClassExist(String Num){//判断该班号的班级是否在存在
for (Class c : classes){
if (c.getNum().equals(Num)){
return true;
}
}
return false;
}
public Class findAClassByNum(String str){//通过班号查找一个班级
for (Class c : classes){
if (c.getNum().equals(str)){
return c;
}
}
return null;//不会走到这一步,因为在调用这个方法之前首先会判断这个班号对应的班级是否存在
}
public void setClassesAve(){
for (Class c : classes){
c.setAverageScore();
}
}
public void setStudentsAve(){
for (Class c : classes){
for (Student s : c.getStudents()){
s.setGrades();
s.setAverageGrade();
}
}
}
public void sort(){
for (Class c : classes){
Collections.sort(c.getStudents());
}
Collections.sort(classes);
}
public void showStudents(){
for (Class c : classes){
for (Student s : c.getStudents()){
if (s.getAverageGrade() != -1) {
System.out.println(s.getNum() + " " + s.getName() + " " + s.getAverageGrade());
}else{
System.out.println(s.getNum() + " " + s.getName() + " did not take any exams");
}
}
}
}
public void showClasses(){
for (Class c : classes){
if (c.getAverageScore() != -1) {
System.out.println(c.getNum() + " " + c.getAverageScore());
}else{
System.out.println(c.getNum() + " has no grades yet");
}
}
}
}
7-2 课程成绩统计程序-3
分数 64
作者 蔡轲
单位 南昌航空大学
课程成绩统计程序-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)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
输入样例1:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
end
输出样例1:
在这里给出相应的输出。例如:
java has no grades yet
输入样例2:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2
end
输出样例2:
在这里给出相应的输出。例如:
java : number of scores does not match
输入样例3:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.1
end
输出样例3:
在这里给出相应的输出。例如:
java : weight value error
输入样例4:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
20201116 张三 java 70 80 90 100
end
输出样例4:
在这里给出相应的输出。例如:
20201116 张三 86
java 86
202011 86
输入样例5:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
20201116 张三 java 70 80 90 100 80
end
输出样例5:
在这里给出相应的输出。例如:
20201116 张三 : access mode mismatch
20201116 张三 did not take any exams
java has no grades yet
202011 has no grades yet
代码:
import java.text.Collator;
import java.util.*;
public class Main {
public static void main(String[] args){
Classes classes = new Classes();
Courses courses = new Courses();
Scanner input = new Scanner(System.in);
while (true){
String str = input.nextLine();
String[] splitedStr = str.split(" ");
if (str.equals("end")){break;}
if (str.matches(".{1,10} 必修 实验") || str.matches(".{1,10} 必修 考察") ||
str.matches(".{1,10} 选修 实验") || str.matches(".{1,10} 实验 考察") ||
str.matches(".{1,10} 实验 考试")){
System.out.println(splitedStr[0] + " : course type & access mode mismatch");
} else if (str.matches(".{1,10} 必修 考试( ([0-9]{1,}[.][0-9]*))+")){
if (courses.findAClassByName(splitedStr[0]) == null) {
if (splitedStr.length == 5) {
float weight;
weight = Float.parseFloat(splitedStr[3]) + Float.parseFloat(splitedStr[4]);
if (Math.abs(weight - 1) < 0.0001) {
Cou cou = new Cou(splitedStr[0], splitedStr[1], splitedStr[2],2, new ArrayList<>(Arrays.asList(Float.parseFloat(splitedStr[3]), Float.parseFloat(splitedStr[4]))));
courses.addACourse(cou);
}else{System.out.println(splitedStr[0] + " : weight value error");}
}else{System.out.println(splitedStr[0] + " : number of scores does not match");}
}
} else if (str.matches(".{1,10} 选修 考察")){
if (courses.findAClassByName(splitedStr[0]) == null) {
Cou cou = new Cou(splitedStr[0], splitedStr[1], splitedStr[2]);
courses.addACourse(cou);
}
} else if (str.matches(".{1,10} 选修 考试( ([0-9]{1,}[.][0-9]*))+")){
if (courses.findAClassByName(splitedStr[0]) == null) {
if (splitedStr.length == 5) {
float weight;
weight = Float.parseFloat(splitedStr[3]) + Float.parseFloat(splitedStr[4]);
if (Math.abs(weight - 1) < 0.0001) {
Cou cou = new Cou(splitedStr[0], splitedStr[1], splitedStr[2],2, new ArrayList<>(Arrays.asList(Float.parseFloat(splitedStr[3]), Float.parseFloat(splitedStr[4]))));
courses.addACourse(cou);
}else{System.out.println(splitedStr[0] + " : weight value error");}
}else{System.out.println(splitedStr[0] + " : number of scores does not match");}
}
} else if (str.matches(".{1,10} 实验 实验 [4-9]( ([0-9]{1,}[.][0-9]*))+")){
if (courses.findAClassByName(splitedStr[0]) == null) {
if (Integer.parseInt(splitedStr[3]) == splitedStr.length - 4) {
float weight = 0;
ArrayList<Float> weights = new ArrayList<>();
for (int i = 1;i <= Integer.parseInt(splitedStr[3]);i ++){
weight += Float.parseFloat(splitedStr[3 + i]);
weights.add(Float.parseFloat(splitedStr[3 + i]));}
if (Math.abs(weight - 1) < 0.0001) {
Cou cou = new Cou(splitedStr[0], splitedStr[1], splitedStr[2], Integer.parseInt(splitedStr[3]), weights);
courses.addACourse(cou);
}else{System.out.println(splitedStr[0] + " : weight value error");}
}else{System.out.println(splitedStr[0] + " : number of scores does not match");}
}
}else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} (100|0|[1-9]\\d?) (100|0|[1-9]\\d?)")){
String classNum = splitedStr[0].substring(0,6);
if (courses.courseExistence(splitedStr[2])) {
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("考试")) {
if (classes.ifAClassExist(classNum)) {
if (classes.findAClassByNum(classNum).isStuExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).getName().equals(splitedStr[1])) {
if (!classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Cou cou = courses.findAClassByName(splitedStr[2]);
classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).addData(cou, splitedStr[3], splitedStr[4], cou.getW());
cou.addAExamGrade(new ExamG(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4]), cou.getW()));}
} else if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou, splitedStr[3], splitedStr[4], cou.getW());
classes.findAClassByNum(classNum).addAStu(stu);
cou.addAExamGrade(new ExamG(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4]), cou.getW()));}
} else {
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou, splitedStr[3], splitedStr[4], cou.getW());
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
cou.addAExamGrade(new ExamG(Integer.parseInt(splitedStr[3]), Integer.parseInt(splitedStr[4]), cou.getW()));}
}else{
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {classes.findAClassByNum(classNum).addAStu(stu);}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");}
}else {
System.out.println(splitedStr[2] + " does not exist");
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {classes.findAClassByNum(classNum).addAStu(stu);}
}else {Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);}
}
} else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10} (100|0|[1-9]\\d?)")){
String classNum = splitedStr[0].substring(0,6);
if (courses.courseExistence(splitedStr[2])) {
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("考察")) {
if (classes.ifAClassExist(classNum)) {
if (classes.findAClassByNum(classNum).isStuExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).getName().equals(splitedStr[1])) {
if (!classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Cou cou = courses.findAClassByName(splitedStr[2]);
classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).addData(cou, splitedStr[3]);
cou.addAInspectGrade(new InsG(Integer.parseInt(splitedStr[3])));
}
} else if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou, splitedStr[3]);
classes.findAClassByNum(classNum).addAStu(stu);
cou.addAInspectGrade(new InsG(Integer.parseInt(splitedStr[3])));
}
} else {
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou, splitedStr[3]);
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
cou.addAInspectGrade(new InsG(Integer.parseInt(splitedStr[3])));
}
}else{
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStu(stu);
}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
}else {
System.out.println(splitedStr[2] + " does not exist");
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStu(stu);
}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
}
} else if (str.matches("\\d{8} [\\u4E00-\\u9FA5A-Za-z]{1,10} [\\u4E00-\\u9FA5A-Za-z]{1,10}( (100|0|[1-9]\\d?))*")){
String classNum = splitedStr[0].substring(0,6);
if (courses.findAClassByName(splitedStr[2]) == null || (splitedStr.length - 3) == courses.findAClassByName(splitedStr[2]).getTimes()){
if (courses.courseExistence(splitedStr[2])) {
if (courses.findAClassByName(splitedStr[2]).getExaming().equals("实验")) {
ArrayList<Integer> experiments = new ArrayList<>();
for (int i = 3;i < splitedStr.length;i ++){
experiments.add(Integer.parseInt(splitedStr[i]));
}
if (classes.ifAClassExist(classNum)){
if (classes.findAClassByNum(classNum).isStuExist(splitedStr[0]) && classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).getName().equals(splitedStr[1])){//班级存在的话学生是否存在
if (!classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).finACourseExist(splitedStr[2])) {
Cou cou = courses.findAClassByName(splitedStr[2]);
classes.findAClassByNum(classNum).findAStuByNum(splitedStr[0]).addData(cou, experiments, cou.getW());
cou.addAExperimentGrade(new ExpG(experiments, cou.getW()));
}
} else if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])){
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou,experiments, cou.getW());
classes.findAClassByNum(classNum).addAStu(stu);
cou.addAExperimentGrade(new ExpG(experiments, cou.getW()));
}
}else{//班级不存在
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
Cou cou = courses.findAClassByName(splitedStr[2]);
stu.addData(cou,experiments, cou.getW());
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
cou.addAExperimentGrade(new ExpG(experiments, cou.getW()));
}
}else{
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStu(stu);
}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");
}
}else{
System.out.println(splitedStr[2] + " does not exist");
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStu(stu);
}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
}
} else{
Stu stu = new Stu(splitedStr[0], splitedStr[1]);
if (classes.ifAClassExist(classNum)) {
if (!classes.findAClassByNum(classNum).isStuExist(splitedStr[0])) {
classes.findAClassByNum(classNum).addAStu(stu);
}
}else {
Class newClass = new Class(classNum, stu);
classes.addAClass(newClass);
}
System.out.println(splitedStr[0] + " " + splitedStr[1] + " : access mode mismatch");}
} else{
if (!str.matches("end")) {System.out.println("wrong format");}
}
}
classes.setStudentsAve();
classes.setClassesAve();
courses.setCoursesAve();
classes.sort();
courses.sort();
classes.showStudents();
courses.showCourses();
classes.showClasses();
}
}
class ExamG {
private ArrayList<Float> w;
private int usualScore;
private int examScore;
private int totalScore;
public int getUsualScore() {return usualScore;}
public int getExamScore() {return examScore;}
public ExamG(int usualScore, int examScore, ArrayList<Float> w) {
this.usualScore = usualScore;
this.examScore = examScore;
this.w = w;
}
public void setTotalScore(){totalScore = (int)(usualScore * w.get(0) + examScore * w.get(1));}
public int getTotalScore() {return totalScore;}
}
class InsG {
private int grade;
public InsG() {}
public InsG(int grade) {this.grade = grade;}
public void setGrade(int grade) {this.grade = grade;}
public int getGrade() {return grade;}
}
class ExpG {
private ArrayList<Float> w;
private ArrayList<Integer> grades;
private int totalScore;
public void calculateTotalScore(){double sum = 0;
int i = 0;
for (int g : grades){sum += g * w.get(i);
i ++;}
totalScore = (int)(sum);}
public int getTotalScore() {return totalScore;}
public ExpG(ArrayList<Integer> grades, ArrayList<Float> w) {this.grades = grades;
this.w = w;}
}
class Stu implements Comparable<Stu>{
private ArrayList<StudentData> data = new ArrayList<>();
private String num;
private String name;
private int averageGrade = -1;
public Stu(String num, String name) {
if (name.length() < 10){
this.name = name;
}else{
System.out.println("wrong format");}
if (num.length() == 8){
this.num = num;
}else{System.out.println("wrong format");}
}
public void setAverageGrade(){
int sum = 0;
if (data.size() != 0) {
for (StudentData s : data) {sum += s.getGrade();}
averageGrade = sum / data.size();
}
}
public String getName() {return name;}
public String getNum() {return num;}
public void addData(Cou cou, String score1, String score2, ArrayList<Float> weights){
ExamG examG = new ExamG(Integer.parseInt(score1),Integer.parseInt(score2),weights);
data.add(new StudentData(cou, examG));}
public void addData(Cou cou, String score){
InsG insG = new InsG();
insG.setGrade(Integer.parseInt(score));
data.add(new StudentData(cou, insG));
}
public void addData(Cou cou, ArrayList<Integer> grades, ArrayList<Float> weights){
ExpG expG = new ExpG(grades,weights);
data.add(new StudentData(cou, expG));}
public int getAveG() {return averageGrade;}
public void setGrades(){for (StudentData d : data){d.setGrade();}}
@Override
public int compareTo(Stu o) {return num.compareTo(o.getNum());}
public boolean finACourseExist(String str){
for (StudentData s : data){
if (s.getCourse().getName().equals(str)){return true;}
}
return false;
}
}
class Cou implements Comparable<Cou>{
private String name;
private String type;
private String examing;
private ArrayList<ExamG> examGS = new ArrayList<>();
private ArrayList<InsG> insGS = new ArrayList<>();
private ArrayList<ExpG> expGS = new ArrayList<>();
private int aveUsuG = -1;
private int aveExamG = -1;
private int aveG = -1;
private int aveS = -1;
private int times;
private ArrayList<Float> w;
public Cou(String name, String type, String examing) {
this.name = name;
this.type = type;
this.examing = examing;
}
public Cou(String name, String type, String examing, int times, ArrayList<Float> w) {
this.name = name;
this.type = type;
this.examing = examing;
this.times = times;
this.w = w;
}
public String getName() {return name;}
public String getExaming() {return examing;}
public void addAExamGrade(ExamG examG){
examGS.add(examG);}
public void addAInspectGrade(InsG insG){
insGS.add(insG);}
public void addAExperimentGrade(ExpG expG){
expGS.add(expG);}
public int getTimes() {return times;}
public void setAverageScore(){int examSum = 0,usualSum = 0,sum = 0;
if (examing.equals("考试") && examGS.size() != 0){
for (ExamG e : examGS){e.setTotalScore();
examSum += e.getExamScore();
usualSum += e.getUsualScore();
sum += e.getTotalScore();}
aveExamG = examSum / examGS.size();
aveUsuG = usualSum / examGS.size();
aveS = sum / examGS.size();}
if (examing.equals("考察") && insGS.size() != 0){
for (InsG i : insGS){sum += i.getGrade();}
aveG = sum / insGS.size();
aveS = aveG;}
if (examing.equals("实验") && expGS.size() != 0){
for (ExpG e : expGS){e.calculateTotalScore();
sum += e.getTotalScore();}
aveS = sum / expGS.size();}
}
public ArrayList<Float> getW() {return w;}
@Override
public int compareTo(Cou o) {Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(this.name,o.getName());}
public int getAveS() {return aveS;}
}
class Courses{
ArrayList<Cou> cours = new ArrayList<>();
public void addACourse(Cou cou){
cours.add(cou);}
public Boolean courseExistence(String str){
for (Cou c : cours){
if (c.getName().equals(str)){return true;}
}
return false;}
public Cou findAClassByName(String name){
for (Cou c : cours){if (name.equals(c.getName())){return c;}
}
return null;}
public void setCoursesAve(){for (Cou c : cours){c.setAverageScore();}}
public void sort(){Collections.sort(cours);}
public void showCourses(){
for (Cou c : cours){
if (c.getAveS() != -1) {
if (c.getExaming().equals("考试")) {System.out.println(c.getName() + " " +c.getAveS());
} else if (c.getExaming().equals("考察")){System.out.println(c.getName() + " " + c.getAveS());
}else{System.out.println(c.getName() + " " + c.getAveS());}
}else{System.out.println(c.getName() + " has no grades yet");}
}
}
}
class Class implements Comparable<Class>{
private ArrayList<Stu> stus = new ArrayList<>();
private int averageScore = -1;
private String num;
public String getNum() {
return num;
}
public Class(String num, Stu stu) {stus.add(stu);
this.num = num;}
public Boolean isStuExist(String str){
for (Stu s : stus){if (s.getNum().equals(str)){return true;}
}
return false;}
public void addAStu(Stu stu){stus.add(stu);}
public Stu findAStuByNum(String str){for (Stu s : stus){if (s.getNum().equals(str)){return s;}
}
return null;}
public void setAverageScore(){int sum = 0,i = 0;
if (stus.size() != 0) {
for (Stu s : stus) {
if (s.getAveG() != -1) {sum += s.getAveG();
i ++;}
}
if (i != 0){this.averageScore = sum / i;}
}
}
@Override
public int compareTo(Class o) {return num.compareTo(o.getNum());}
public ArrayList<Stu> getStudents() {return stus;}
public int getAverageScore() {return averageScore;}
}
class StudentData{
private Cou cou;
private ExamG totalGrade;
private InsG grade;
private ExpG expG;
private int Grade;
public void setGrade(){
if (cou.getExaming().equals("考试")){Grade = (int) (totalGrade.getUsualScore() * cou.getW().get(0) + totalGrade.getExamScore() * cou.getW().get(1));
}else if (cou.getExaming().equals("考察")){Grade = grade.getGrade();
}else{expG.calculateTotalScore();
Grade = expG.getTotalScore();}
}
public int getGrade() {return Grade;}
public StudentData(Cou cou, ExamG examG){this.cou = cou;
totalGrade = examG;}
public StudentData(Cou cou, InsG insG){this.cou = cou;
this.grade = insG;}
public StudentData(Cou cou, ExpG expG){this.cou = cou;
this.expG = expG;}
public Cou getCourse() {return cou;}
}
class Classes{
private ArrayList<Class> classes = new ArrayList<>();
public void addAClass(Class newClass){classes.add(newClass);}
public Boolean ifAClassExist(String Num){
for (Class c : classes){
if (c.getNum().equals(Num)){return true;}
}
return false;}
public Class findAClassByNum(String str){
for (Class c : classes){
if (c.getNum().equals(str)){return c;}
}
return null;}
public void setClassesAve(){
for (Class c : classes){c.setAverageScore();}
}
public void setStudentsAve(){
for (Class c : classes){
for (Stu s : c.getStudents()){s.setGrades();
s.setAverageGrade();}
}
}
public void sort(){
for (Class c : classes){Collections.sort(c.getStudents());}
Collections.sort(classes);}
public void showStudents(){
for (Class c : classes){
for (Stu s : c.getStudents()){
if (s.getAveG() != -1) {System.out.println(s.getNum() + " " + s.getName() + " " + s.getAveG());
}else{System.out.println(s.getNum() + " " + s.getName() + " did not take any exams");}
}
}
}
public void showClasses(){
for (Class c : classes){
if (c.getAverageScore() != -1) {System.out.println(c.getNum() + " " + c.getAverageScore());
}else{
System.out.println(c.getNum() + " has no grades yet");}
}
}
}
三、踩坑心得
- 应该多给代码标上备注。比如这几次题目集相隔时间有点远,导致我对之前的代码没什么记忆,又没有注释,让我读起来还需要仔细回忆一下这些东西都是干什么的,浪费了很多时间,也感到了注释的重要性。
- 不要瞎写,先动脑子再动键盘,我往往是感觉哪不对就改,并没有想好怎么改,导致代码冗余。
- 高效debug是改代码的关键。
四、改进意见
1.日后不能只追求写出来,更要在一开始就想好后面该怎么办,怎么写可以让代码更加的稳定而不会随意一下就崩掉
2.与那些算法设计的比较好的同学交流交流,询问他们是如何想到更加优质的代码和更加良好的结构的。进行一些比较,效仿并努力达到他人的代码质量
五、总结
javayyds,这次按照老师的类图写真让我感受到了java的便利,每次迭代需要改的地方都很少,java最重要的地方还是逻辑,这次是老师为我们想好类图,但下次我们就要自己想了,在写代码之前缕清题目逻辑并确定一个合适的类与类的关系是至关重要的,这我在以后也要努力学习。我也要坚持写注释,不写真记不住。在每次修改代码前都问自己,这么改会不会影响到其它地方。
标签:int,BLOG3,splitedStr,classNum,classes,new,public
From: https://www.cnblogs.com/nchuxuanbao/p/17891966.html