-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse.java
More file actions
63 lines (52 loc) · 1.62 KB
/
Course.java
File metadata and controls
63 lines (52 loc) · 1.62 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
54
55
56
57
58
59
60
61
62
63
import java.util.Arrays;
public class Course {
private String courseName;
String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
if (numberOfStudents == students.length) {
students = Arrays.copyOf(students, students.length * 2);
}
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return Arrays.copyOf(students, numberOfStudents);
}
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].equals(student)) {
for (int j = i; j < numberOfStudents - 1; j++) {
students[j] = students[j + 1];
}
numberOfStudents--;
return;
}
}
}
public void clear() {
students = new String[100];
numberOfStudents = 0;
}
// Test program
public static void main(String[] args) {
Course course = new Course("COSC 1047O");
course.addStudent("Student 1");
course.addStudent("Student 2");
course.addStudent("Student 3");
course.dropStudent("Student 2");
String[] remainingStudents = course.getStudents();
for (String student : remainingStudents) {
System.out.println(student);
}
}
}