1) 编写Student类,主要属性包括学号、姓名、性别、班级
2) 编写Score类,主要属性包括:学号、课程名、分数
3) 模拟期末考试的成绩统计应用场景,要求
(1) 所有学生名单及对应科目成绩已经初始化在数组中
(2) 要求输出每门课程的所有学生的平均成绩。
(3) 输出某门课程的最高成绩,最低成绩
(4) 可以查询某个学生的所有成绩。
源代码
二、源程序
Score类
public class Score {
private String studentID;
private String course;
private int grades;
public Score(String studentID, String course, int grades) {
this.studentID = studentID;
this.course = course;
this.grades = grades;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getGrades() {
return grades;
}
public void setGrades(int grades) {
this.grades = grades;
}
}
Student类
public class Student {
private String studentID;
private String studentName;
private String sex;
private String classID;
public Student(String studentID, String studentName, String sex, String classID) {
this.studentID = studentID;
this.studentName = studentName;
this.sex = sex;
this.classID = classID;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClassID() {
return classID;
}
public void setClassID(String classID) {
this.classID = classID;
}
}
Util类
public class Util {
public static void getAverage(Score[] scores) {
Set<String> set = new HashSet<>();
for (Score score : scores)
set.add(score.getCourse());
for (String course : set)
System.out.println(course + "平均成绩:" + Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics().getAverage());
}
public static void getMax(Score[] scores, String course) {
IntSummaryStatistics statistics = Arrays.stream(scores).filter(x -> x.getCourse().equals(course)).mapToInt(Score::getGrades).summaryStatistics();
System.out.println(course + "最高成绩:" + statistics.getMax());
System.out.println(course + "最低成绩:" + statistics.getMin());
}
public static void getAllGrade(Score[] scores, Student[] students, String studentName) {
String studentID = null;
for (Student student : students)
if (student.getStudentName().equals(studentName))
studentID = student.getStudentID();
for (Score score : scores)
if (studentID.equals(score.getStudentID()))
System.out.println(studentName + " " + score.getCourse() + "成绩为:" + score.getGrades());
}
public static void main(String[] args) {
Score[] scores = new Score[]{new Score("1", "math", 89), new Score("1", "english", 90), new Score("2", "math", 92), new Score("2", "english", 95)};
Student[] student = new Student[]{new Student("1", "mary", "women", "1"), new Student("2", "john", "men", "1")};
getAverage(scores);
getMax(scores, "math");
getAllGrade(scores, student, "john");
}
}