请统计某个老师教授的所有课程的学生考试的通过率 注意:1个老师可能任多门课,学生可能参加多门课的多场考试 ---------------请完成以下方法----------------- /** * * @param teacher 需要统计的任课老师 * @param resultList 本学年所有学生的所有考试的结果 * @return 任课老师各课的学生考试通过率 */ public static Map<Course,Float> calculate(Teacher teacher, List<ExamResult> resultList) { } -----------以下是相关对象定义---------- //课程定义 public class Course { private int id; // 课程名称 private String name; // 任课老师 private Teacher teacher; public Course(int id, String name, Teacher teacher) { this.id = id; this.name = name; this.teacher = teacher; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } } //学生定义 public class Student { private int id; // 学生姓名 private String name; // 性别 private int sex; // 生日 private Date birth; public Student(int id, String name, int sex, Date birth) { this.id = id; this.name = name; this.sex = sex; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } } //老师定义 public class Teacher { private int id; // 老师姓名 private String name; // 性别 private int sex; public Teacher
时间: 2023-03-01 14:25:33 浏览: 186
考试通过率统计
完成以下方法:
```
public static Map<Course,Float> calculate(Teacher teacher, List<ExamResult> resultList) {
Map<Course, Float> result = new HashMap<>();
Map<Course, Integer> coursePassCount = new HashMap<>();
Map<Course, Integer> courseTotalCount = new HashMap<>();
for (ExamResult examResult : resultList) {
Course course = examResult.getCourse();
Student student = examResult.getStudent();
boolean isPass = examResult.isPass();
Teacher t = course.getTeacher();
if (!t.equals(teacher)) {
continue;
}
if (!courseTotalCount.containsKey(course)) {
courseTotalCount.put(course, 1);
} else {
courseTotalCount.put(course, courseTotalCount.get(course) + 1);
}
if (isPass) {
if (!coursePassCount.containsKey(course)) {
coursePassCount.put(course, 1);
} else {
coursePassCount.put(course, coursePassCount.get(course) + 1);
}
}
}
for (Map.Entry<Course, Integer> entry : courseTotalCount.entrySet()) {
Course course = entry.getKey();
int totalCount = entry.getValue();
int passCount = coursePassCount.getOrDefault(course, 0);
float passRate = (float) passCount / totalCount;
result.put(course, passRate);
}
return result;
}
```
该方法使用三个Map来统计每门课程的通过人数和总人数,最后遍历总人数Map,计算通过率并将结果放入最终的Map中返回。
阅读全文