设计类Course
假设需要处理课程信息,没门课程都有一个名字以及选课的学生。要能够向/从这个课程添加/删除一个学生,可以使用一个类来对课程建模。
向构造方法Course(String name)传递一门课程的名称来创建一个Course对象。你可以使用addStudemt(String student)方法来向某门课程添加学生,使用dropStudent(String student)方案从某门课程中删掉一个学生,而使用getStudents()方法可以返回选这门课程的所有学生。假设该类是可用的。下面的程序创建一个测试类,这个测试类创建了两门课程,并向课程中加入学生。
public class TestCourse {
public static void main(String args[]){
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");
course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");
System.out.println("Number of students in course1: " + course1.getNumberOfStudents());
String[] students = course1.getStudent();
for(int i = 0;i<course1.get

本文介绍了如何设计Course类来处理课程信息,包括构造方法、添加删除学生的方法以及获取学生列表。同时,文章探讨了设计堆栈类StackOfIntegers,用于实现后进先出的数据结构,并讨论了其实现细节,如数组存储和方法实现。
最低0.47元/天 解锁文章
2297

被折叠的 条评论
为什么被折叠?



