用java编写一个学生成绩管理系统。学生的属性包括学号、姓名、年龄等。每个学生要学习若干课程,每门课程有平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩,其中平时成绩的数目不定(因为不同课程的教师布置的平时作业不同),而总评成绩是其它成绩的平均值。请先设计合理的类存放这些信息,然后设计程序完成以下功能: (1) 列出某个学生的所有成绩; (2) 列出某门课程每个学生的总评成绩,及所有学生总评成绩的总评分; (3) 分区段统计某门课程的学生总评成绩,例如60分以下的学生人数、60至70分的学生人数等。
时间: 2023-06-17 11:04:53 浏览: 393
以下是Java代码实现学生成绩管理系统的基本功能:
```java
import java.util.ArrayList;
class Student {
private String id;
private String name;
private int age;
private ArrayList<Course> courses;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
this.courses = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void addCourse(Course course) {
courses.add(course);
}
public ArrayList<Course> getCourses() {
return courses;
}
}
class Course {
private String name;
private ArrayList<Double> dailyGrades;
private double midGrade;
private double pracGrade;
private double finalGrade;
private double totalGrade;
public Course(String name) {
this.name = name;
this.dailyGrades = new ArrayList<>();
}
public String getName() {
return name;
}
public void addDailyGrade(double grade) {
dailyGrades.add(grade);
}
public void setMidGrade(double grade) {
this.midGrade = grade;
}
public void setPracGrade(double grade) {
this.pracGrade = grade;
}
public void setFinalGrade(double grade) {
this.finalGrade = grade;
}
public double getTotalGrade() {
return totalGrade;
}
public void calculateTotalGrade() {
double dailyGradeSum = 0;
for (double grade : dailyGrades) {
dailyGradeSum += grade;
}
double dailyGradeAvg = dailyGradeSum / dailyGrades.size();
totalGrade = (dailyGradeAvg + midGrade + pracGrade + finalGrade) / 4;
}
}
public class GradeManagementSystem {
private ArrayList<Student> students;
public GradeManagementSystem() {
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void listStudentGrades(String studentId) {
for (Student student : students) {
if (student.getId().equals(studentId)) {
System.out.println("Student ID: " + student.getId());
System.out.println("Student Name: " + student.getName());
for (Course course : student.getCourses()) {
System.out.println("Course Name: " + course.getName());
System.out.println("Daily Grades: " + course.getDailyGrades().toString());
System.out.println("Midterm Grade: " + course.getMidGrade());
System.out.println("Practice Grade: " + course.getPracGrade());
System.out.println("Final Exam Grade: " + course.getFinalGrade());
System.out.println("Total Grade: " + course.getTotalGrade());
}
}
}
}
public void listCourseGrades(String courseName) {
double totalGradeSum = 0;
int totalGradeCount = 0;
for (Student student : students) {
for (Course course : student.getCourses()) {
if (course.getName().equals(courseName)) {
System.out.println("Student Name: " + student.getName());
System.out.println("Total Grade: " + course.getTotalGrade());
totalGradeSum += course.getTotalGrade();
totalGradeCount++;
}
}
}
double totalGradeAvg = totalGradeSum / totalGradeCount;
System.out.println("Total Grade Average: " + totalGradeAvg);
}
public void listCourseGradeDistribution(String courseName) {
int[] gradeCounts = new int[11];
for (Student student : students) {
for (Course course : student.getCourses()) {
if (course.getName().equals(courseName)) {
int grade = (int) Math.floor(course.getTotalGrade() / 10);
if (grade < 0) {
grade = 0;
}
if (grade > 10) {
grade = 10;
}
gradeCounts[grade]++;
}
}
}
for (int i = 0; i < 10; i++) {
System.out.println(i * 10 + " - " + (i + 1) * 10 + " Count: " + gradeCounts[i]);
}
System.out.println("100 Count: " + gradeCounts[10]);
}
public static void main(String[] args) {
GradeManagementSystem system = new GradeManagementSystem();
// Add some students and courses
Student student1 = new Student("101", "Alice", 20);
Course course1 = new Course("Math");
course1.addDailyGrade(80);
course1.addDailyGrade(90);
course1.setMidGrade(75);
course1.setPracGrade(85);
course1.setFinalGrade(90);
course1.calculateTotalGrade();
student1.addCourse(course1);
system.addStudent(student1);
Student student2 = new Student("102", "Bob", 21);
Course course2 = new Course("English");
course2.addDailyGrade(85);
course2.addDailyGrade(95);
course2.addDailyGrade(90);
course2.setMidGrade(80);
course2.setPracGrade(90);
course2.setFinalGrade(85);
course2.calculateTotalGrade();
student2.addCourse(course2);
system.addStudent(student2);
// Test the system functions
system.listStudentGrades("101");
system.listCourseGrades("Math");
system.listCourseGradeDistribution("Math");
}
}
```
在上面的代码中,我们定义了两个类:Student 和 Course。Student 类表示学生,包含学号、姓名、年龄等属性和一个课程列表。Course 类表示课程,包含课程名、平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩。我们还定义了一个 GradeManagementSystem 类表示成绩管理系统,包括添加学生、列出某个学生的所有成绩、列出某门课程每个学生的总评成绩及所有学生总评成绩的总评分、分区段统计某门课程的学生总评成绩等功能。
在 main 函数中,我们创建了两个学生和两个课程,并测试了系统的功能。你可以根据需要修改代码以适应你的需求。
阅读全文