-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse.java
More file actions
53 lines (46 loc) · 1.37 KB
/
Course.java
File metadata and controls
53 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package edu.dvc.comsc256.rs;
public class Course {
private String name;
private Instructor instructor;
private Student[] students;
private int size;
public Course(String name, Instructor instructor) {
this.name = name;
this.instructor = instructor;
this.students = new Student[10];
this.size = 0;
}
public String getName() {
return name;
}
public void addStudent(Student student) {
ensureCapacity();
students[size++] = student;
student.addCourse(this);
}
public void dropStudent(Student student) {
for (int i = 0; i < size; i++) {
if (students[i] == student) {
students[i] = students[size - 1];
students[--size] = null;
student.dropCourse(this);
return;
}
}
}
private void ensureCapacity() {
if (size == students.length) {
Student[] temp = new Student[students.length * 2];
System.arraycopy(students, 0, temp, 0, students.length);
students = temp;
}
}
public void print() {
System.out.println("Course: " + name);
System.out.print("Students: ");
for (int i = 0; i < size; i++) {
System.out.print(students[i].getName() + " ");
}
System.out.println("\n");
}
}