# 实验二 学生选课模拟系统 **Repository Path**: zhang-miao-2002/shiyaner ## Basic Information - **Project Name**: 实验二 学生选课模拟系统 - **Description**: 实验二 学生选课模拟系统 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-10-10 - **Last Updated**: 2022-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: 实验二 ## README # 实验二 学生选课模拟系统 ### 实验目的 初步了解分析系统需求,从学生选课角度了解系统中的实体及其关系,学会定义类中的属性以及方法; 掌握面向对象的类设计方法(属性、方法); 掌握通过构造方法实例化对象; ### 业务要求 学校人员分为“教师”和“学生”,教师教授“课程”,学生选择“课程”。初期为了设计简单,每名教师讲1门课程,每名学生选1门课程。 对象: 教师(编号、姓名、所授课程);学生(编号、姓名、所选课程);课程(编号、课程名称、上课地点、时间); ### 实验要求 1.编写上述实体类以及测试主类 2.在测试主类中,实例化多个类实体,模拟 1)教师开设某课; 2)学生选课、退课 3)打印学生课表信息(包括:编号、课程名称、上课地点、时间、授课教师等) 3.编写实验报告。 ### 解题思路 首先创建一个选课的包,名为Xuanke,再创建Teacher、Student、Course、ClassResult类。 1)在Teacher类中创建四个属性及其所属类型,分别为id;teacherName;teacherSex;其构造函数: ``` public Teacher() { super(); courses = new Course[1]; } public Teacher(int id,String teacherName,String teacherSex){ this.id=id; this.teacherName=teacherName; this.teacherSex=teacherSex; courses = new Course[1]; } ``` 2)在Student类中有studentId;studentName;major;几个属性,其构造函数: ``` public Student() { super(); courses = new Course[1]; } public Student(int studentId,String studentName) { super(); this.studentId=studentId; this.studentName=studentName; courses = new Course[1]; } ``` ``` public Student(int studentId,String studentName,String major) { super(); this.studentId=studentId; this.studentName=studentName; this.major = major; courses = new Course[1]; } ``` 显示学生所选的课程 ``` public void displayCourse(){ System.out.println("学生"+this.studentName+"所选课程有:"); for(Course c:courses){ if(c!=null){ System.out.print(c.getName()+" "); } } System.out.println(); } } ``` 3)Course的构造函数: ``` public Course() { super(); students = new Student[1]; } public Course(int courseId,String courseName) { super(); this.courseId=courseId; this.courseName=courseName; students = new Student[1]; } public Course(int courseId,String courseName,String courseAddress) { super(); this.courseId=courseId; this.courseName=courseName; this.courseAddress=courseAddress; students = new Student[1]; } public Course(int courseId,String courseName,String courseAddress,float credit) { super(); this.courseId=courseId; this.courseName=courseName; this.courseAddress=courseAddress; this.credit=credit; students = new Student[1]; } public Course(int courseId,String courseName,String courseAddress,float credit,Teacher teacher) { super(); this.courseId = courseId; this.courseName = courseName; this.courseAddress = courseAddress; this.credit = credit; this.setTeacher(teacher); students = new Student[1]; } ``` 显示选择课程的学生: ``` public void displayStudent(){ System.out.println("选择的课程:"+this.courseName+"的学生有:"); for(Student s:students){ if(s!=null){ System.out.print(s.getStudentName()+" "); } } System.out.println(); } ``` 4)输出课程信息 ``` Student stu1 = new Student(2021310839, "小张","JAVA"); Teacher tea1= new Teacher(11,"小明","男"); Course cou1 = new Course(303,"JAVA"); System.out.println("学生信息:" + " " + stu1.getStudentId() + " " + stu1.getStudentName() + " "+stu1.getMajor()); System.out.println("老师信息:" + " " + tea1.getId() + " " + tea1.getTeacherName() + " " + tea1.getTeacherSex()); System.out.println("课程信息:" + " " + "教室" + cou1.getId() +" 10:00-12:00" +" " + cou1.getName()); ``` ### 实验流程图 ![输入图片说明](%E5%9B%BE%E7%89%871.png) ### 实验效果 ![输入图片说明](%E5%9B%BE%E7%89%87.png) ### 实验感想 这次实验让我掌握了定义以及调用类中的属性以及方法,面向对象的类设计方法等,在过程中参考了csdn以及在网络上查找了很多资料最后才顺利完成。