给指定课程加入学员的程序
代码如下:
第一部分
package Day06;
public class Course {
private String courseName;
private String[] students = new String [100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
this.students[numberOfStudents] = student;
this.numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
int i = 0;
while(i < numberOfStudents) {
if (student.equals(students[i])) {
break;
}
i++;
}
if (i < numberOfStudents) {
while (i < numberOfStudents) {
this.students[i] = this.students[i + 1];
i++;
}
numberOfStudents--;
this.students[numberOfStudents] = null;
}
}
}
第二部分
package Day06;
public class TestCourse {
public static void main(String[] args) {
Course course1 = new Course("Java Programming");
course1.addStudent("John");
course1.addStudent("Peter");
System.out.println("Number of Students is: " + course1.getNumberOfStudents());
for (int i = 0; i< course1.getNumberOfStudents(); i++) {
System.out.println("Student " + i
+ " is: " + course1.getStudents()[i]);
}
}
}