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("Steven Smith");
System.out.println("Number of students in course1: " +
course1.getNumberOfStudents());
String[] students = course1.getStudents();
for(int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
course1.dropStudent("Peter Jones");
System.out.println("Number of students in course1: " +
course1.getNumberOfStudents());
students = course1.getStudents();
for(int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
System.out.print("Number of students in course2: " +
course2.getNumberOfStudents());
}
}
public class Course {
private String courseName;
private String[] students = new String[0];
private int numberOfStudents = 0;
public Course(String courseName) {
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
public void addStudent(String studentName) {
if(students.length <= numberOfStudents ) {
String[] newStudents = new String[2 * students.length + 1];
for(int i = 0; i < numberOfStudents; i++)
newStudents[i] = students[i];
students = newStudents;
}
students[numberOfStudents++] = studentName;
}
public void dropStudent(String studentName) {
for(int i = 0; i < numberOfStudents; i++)
if(students[i].equals(studentName))
for(int k = i; i < numberOfStudents - 1;)
students[i] = students[++i];
numberOfStudents--;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String[] getStudents() {
return students;
}
}
Number of students in course1: 3
Peter Jones, Brian Smith, Anne Kennedy,
Number of students in course1: 2
Brian Smith, Anne Kennedy,
Number of students in course2: 2
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("Steven Smith");
System.out.println("Number of students in course1: " +
course1.getNumberOfStudents());
String[] students = course1.getStudents();
for(int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
course1.dropStudent("Peter Jones");
System.out.println("Number of students in course1: " +
course1.getNumberOfStudents());
students = course1.getStudents();
for(int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
System.out.print("Number of students in course2: " +
course2.getNumberOfStudents());
}
}
public class Course {
private String courseName;
private String[] students = new String[0];
private int numberOfStudents = 0;
public Course(String courseName) {
this.courseName = courseName;
}
public String getCourseName() {
return courseName;
}
public void addStudent(String studentName) {
if(students.length <= numberOfStudents ) {
String[] newStudents = new String[2 * students.length + 1];
for(int i = 0; i < numberOfStudents; i++)
newStudents[i] = students[i];
students = newStudents;
}
students[numberOfStudents++] = studentName;
}
public void dropStudent(String studentName) {
for(int i = 0; i < numberOfStudents; i++)
if(students[i].equals(studentName))
for(int k = i; i < numberOfStudents - 1;)
students[i] = students[++i];
numberOfStudents--;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String[] getStudents() {
return students;
}
}
Number of students in course1: 3
Peter Jones, Brian Smith, Anne Kennedy,
Number of students in course1: 2
Brian Smith, Anne Kennedy,
Number of students in course2: 2