class Student{//学生表
private intstuid;privateString sname;private intage;private StudentCourse studentCourse[];//学生成绩信息
publicStudent(){}public Student(int stuid,String sname,intage){this.stuid =stuid;this.sname =sname;this.age =age;
}public voidsetStudentCourse(StudentCourse[] studentCourse) {this.studentCourse =studentCourse;
}publicStudentCourse[] getStudentCourse() {returnstudentCourse;
}publicString getInfo(){return "学号:" + this.stuid + ",学生姓名:" + this.sname + ",年龄:" + this.age;
}
}classCourse{//课程表privateString name;private intcid;private intcredit;privateStudentCourse studentCourse[];publicCourse(){}public Course(String name,int cid,intcredit){this.cid =cid;this.name =name;this.credit =credit;
}publicStudentCourse[] getStudentCourse() {returnstudentCourse;
}public voidsetStudentCourse(StudentCourse[] studentCourse) {this.studentCourse =studentCourse;
}publicString getInfo(){return "课号:" + this.credit + ",名称:" + this.name + ",学分:" + this.credit;
}
}class StudentCourse{//学生选课表
privateStudent student;privateCourse course;private double score;//成绩
publicStudentCourse(){
}public StudentCourse(Student student,Course course,doublescore){this.course =course;this.score =score;this.student =student;
}publicCourse getCourse() {returncourse;
}publicStudent getStudent() {returnstudent;
}public doublegetScore() {returnscore;
}
}public classMain {public static voidmain(String[] args) {//创建各自的独立对象
Student stu1 = new Student(1,"张三",18);
Student stu2= new Student(1,"李四",20);
Student stu3= new Student(1,"王五",19);
Course ca= new Course("高等数学",1001,5);
Course cb= new Course("线性代数",1002,4);//设置各自的关系//设置学生和课程的关系
stu1.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,ca,92.5),new StudentCourse(stu1,cb,94.0)
});
stu2.setStudentCourse(newStudentCourse[]{new StudentCourse(stu2,ca,89.0)
});
stu3.setStudentCourse(newStudentCourse[]{new StudentCourse(stu3,cb,95.0),new StudentCourse(stu3,ca,90.5)
});//设置课程和学生的关系
ca.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,ca,92.5),new StudentCourse(stu2,ca,89.0)new StudentCourse(stu3,ca,90.5)
});
cb.setStudentCourse(newStudentCourse[]{new StudentCourse(stu1,cb,94.0),new StudentCourse(stu3,cb,95.0)
});//找到一门课程,并且输出学习此课程的所有学生的信息及成绩
System.out.println(ca.getInfo());for(int x = 0;x < ca.getStudentCourse().length;x++){
System.out.println("\t"+ca.getStudentCourse()[x].getStudent().getInfo()+",成绩:"+ca.getStudentCourse()[x].getScore());
};
System.out.println("***********");//根据学生输出其信息及选修的课程信息和所得成绩
System.out.println(stu1.getInfo());for(int x = 0;x < stu1.getStudentCourse().length;x++){
System.out.println("\t选修课程:" +stu1.getStudentCourse()[x].getCourse().getInfo()+ ",所得成绩:" +stu1.getStudentCourse()[x].getScore());
}
}
}