import java.util.*;
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) {
if (numberOfStudents >= students.length) {
String[] temp = new String[numberOfStudents+1];
System.arraycopy(students, 0, temp, 0, students.length);
students = temp;
}
students[numberOfStudents]=student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
for(int i=0; i<numberOfStudents; i++) {
if(students[i]==student){
students[i]="";
numberOfStudents--;
for(int j=i; j<numberOfStudents; j++){
students[j]=students[j+1];
}
}
else {
}
}
}
public void clear() {
while(numberOfStudents!=0) {
students[0]="";
numberOfStudents--;
for(int j=0; j<numberOfStudents; j++){
students[j]=students[j+1];
}
}
}
}
import java.util.*;
public class TestCourse {
public static void main(String[] args) {
Course course1 = new Course("Object-Oriented Programming");
Course course2 = new Course("Data Structures");
course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("S1");
course2.addStudent("S2");
course2.addStudent("S3");
course2.addStudent("S4");
System.out.println("Number of students in course1: "
+ course1.getNumberOfStudents());
String[] students1 = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students1[i] + ", ");
System.out.println();
System.out.println("Number of students in course2: "
+ course2.getNumberOfStudents());
String[] students2 = course2.getStudents();
for (int i = 0; i < course2.getNumberOfStudents(); i++)
System.out.print(students2[i] + ", ");
System.out.println();
course1.dropStudent("Peter Jones");
System.out.println("Number of students in course1 after drop Peter Jones: "
+ course1.getNumberOfStudents());
String[] students3 = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students3[i] + ", ");
System.out.println();
course1.clear();
System.out.println("Number of students in course1 after clear: "
+ course1.getNumberOfStudents());
course2.dropStudent("S2");
System.out.println("Number of students in course2 after drop S2: "
+ course2.getNumberOfStudents());
String[] students4 = course2.getStudents();
for (int i = 0; i < course2.getNumberOfStudents(); i++)
System.out.print(students4[i] + ", ");
System.out.println();
course2.clear();
System.out.println("Number of students in course2 after clear: "
+ course2.getNumberOfStudents());
}
}
Comments (0)