java 代码编写一个学生成绩管理系统。学生的属性包括学号、姓名、年龄等。每个学生要学习若干课程,每门课程有平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩,其中平时成绩的数目不定(因为不同课程的教师布置的平时作业不同),而总评成绩是其它成绩的平均值。请先设计合理的类存放这些信息,然后设计程序完成以下功能: (1)列出某个学生的所有成绩; (2)列出某门课程每个学生的总评成绩,及所有学生总评成绩的总评分; (3)分区段统计某门课程的学生总评成绩,例如60分以下的学生人数、60至70分的学生人数等。
时间: 2023-06-25 10:04:02 浏览: 190
以下是一个Java代码示例,实现了学生成绩管理系统的基本功能:
```
import java.util.ArrayList;
class Student {
private int id;
private String name;
private int age;
private ArrayList<Course> courses;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
courses = new ArrayList<>();
}
public void addCourse(Course course) {
courses.add(course);
}
public ArrayList<Course> getCourses() {
return courses;
}
public String toString() {
return "Student ID: " + id + "\nName: " + name + "\nAge: " + age;
}
}
class Course {
private String name;
private ArrayList<Double> dailyGrades;
private double midtermGrade;
private double internshipGrade;
private double finalGrade;
public Course(String name) {
this.name = name;
dailyGrades = new ArrayList<>();
}
public void addDailyGrade(double grade) {
dailyGrades.add(grade);
}
public void setMidtermGrade(double grade) {
this.midtermGrade = grade;
}
public void setInternshipGrade(double grade) {
this.internshipGrade = grade;
}
public void setFinalGrade(double grade) {
this.finalGrade = grade;
}
public double getTotalGrade() {
double total = midtermGrade + internshipGrade + finalGrade;
for (double grade : dailyGrades) {
total += grade;
}
double avg = total / (dailyGrades.size() + 3);
return avg;
}
public String toString() {
return "Course Name: " + name;
}
}
class GradeManager {
private ArrayList<Student> students;
private ArrayList<Course> courses;
public GradeManager() {
students = new ArrayList<>();
courses = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void addCourse(Course course) {
courses.add(course);
}
public void printStudentGrades(Student student) {
System.out.println(student);
for (Course course : student.getCourses()) {
System.out.println(course);
System.out.println("Total Grade: " + course.getTotalGrade());
}
}
public void printCourseGrades(Course course) {
System.out.println(course);
double totalGrades = 0;
for (Student student : students) {
for (Course c : student.getCourses()) {
if (c == course) {
double grade = c.getTotalGrade();
System.out.println(student + "\nTotal Grade: " + grade);
totalGrades += grade;
break;
}
}
}
double avgGrade = totalGrades / students.size();
System.out.println("Average Total Grade: " + avgGrade);
}
public void printGradeDistribution(Course course) {
int[] count = new int[11];
for (Student student : students) {
for (Course c : student.getCourses()) {
if (c == course) {
double grade = c.getTotalGrade();
int index = (int) (Math.floor(grade / 10) - 5);
count[index]++;
break;
}
}
}
System.out.println(course);
int lowerBound = 0;
for (int i = 0; i < 11; i++) {
int upperBound = lowerBound + 9;
if (i == 10) {
upperBound = 100;
}
System.out.println(lowerBound + " - " + upperBound + ": " + count[i]);
lowerBound = upperBound + 1;
}
}
}
public class Main {
public static void main(String[] args) {
GradeManager manager = new GradeManager();
// add students
Student s1 = new Student(1, "Alice", 20);
Student s2 = new Student(2, "Bob", 21);
manager.addStudent(s1);
manager.addStudent(s2);
// add courses
Course c1 = new Course("Math");
Course c2 = new Course("English");
manager.addCourse(c1);
manager.addCourse(c2);
// add grades
s1.addCourse(c1);
c1.addDailyGrade(80);
c1.addDailyGrade(70);
c1.addMidtermGrade(85);
c1.setInternshipGrade(90);
c1.setFinalGrade(95);
s1.addCourse(c2);
c2.addDailyGrade(90);
c2.addMidtermGrade(80);
c2.setInternshipGrade(95);
c2.setFinalGrade(85);
s2.addCourse(c1);
c1.addDailyGrade(75);
c1.addMidtermGrade(90);
c1.setInternshipGrade(80);
c1.setFinalGrade(85);
s2.addCourse(c2);
c2.addMidtermGrade(70);
c2.setInternshipGrade(80);
c2.setFinalGrade(75);
// print grades
manager.printStudentGrades(s1);
manager.printCourseGrades(c1);
manager.printGradeDistribution(c2);
}
}
```
这个程序实现了以下功能:
1. 添加学生和课程,并为每个学生添加所学的课程以及对应的成绩;
2. 计算并打印出某个学生的所有成绩;
3. 计算并打印出某门课程每个学生的总评成绩,以及所有学生总评成绩的平均值;
4. 分区段统计某门课程的学生总评成绩,并打印出不同分数段的学生人数。
阅读全文