In: Computer Science
The directions say to "create two methods:
-enrollStudent (Student newStudent) which adds newStudent into the enrolled list and increment numOfEnroll by 1, if the section is full, print out an error message and no change with numOfEnroll.
-removeStudent (String studentId) which removes the student with studentId from the enrolled list and decrement numOfEnrollby 1, if the target student is not found in the enrolled list, print out an error message and no change with numOfEnroll."
//Section.java
package campus;
public class Section {
private String id;
private Faculty instructor;
private Student[] enrolled;
private int numofEnroll;
private int capacity;
private String location;
private String time;
private String semester;
public Section(String id, Faculty instructor, int capacity, String
location, String time, String semester) {
}
public Section(String id, int capacity) {
this.id = id;
this.capacity = capacity;
// remaining will be default
this.numofEnroll = 0;
this.enrolled = new Student[capacity];
}
public String getId() {
return id;
}
public Faculty getInstructor() {
return instructor;
}
public void setInstructor(Faculty instructor) {
this.instructor = instructor;
}
public Student[] getEnrolled() {
return enrolled;
}
public int getNumofEnroll() {
return numofEnroll;
}
public int getCapacity() {
return capacity;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSemester() {
return semester;
}
public void enrollStudent(Student newStudent)
{
// check is capacity is equal to enrolled list
if(numofEnroll == capacity) {
System.out.println("Section
capacity is full");
} else {
enrolled[numofEnroll] =
newStudent;
numofEnroll++;
}
}
public void removeStudent(Student newStudent)
{
// first check if the student is exist in the section
or not
boolean found = false;
for(int i = 0; i < enrolled.length; i++) {
if(enrolled[i].getId().equalsIgnoreCase(newStudent.getId()))
{
// found, now
delete the student
enrolled[i] =
null;
found =
true;
}
}
if(!found)
System.out.println("requested
Student is not found in the section");
}
public void displayStudents() {
for(int i = 0; i < enrolled.length; i++) {
if(enrolled[i] != null) {
System.out.println(enrolled[i]);
}
}
}
}
// Test.java
package campus;
public class Test {
public static void main(String[] args) {
// now test the method in
Section
//create the Section with capacity
and section id...creating capacity as 2
Section section =
new Section("1", 2);
//create the Student and add to
list
Student s1 = new
Student("1", "John", "Pal", "JAVA", 5);
System.out.println("adding student:
"+s1);
section.enrollStudent(s1);
//display the students in
section
System.out.println("\nAll Student
in the Section:");
System.out.println("---------------------------");
section.displayStudents();
// create one more student
Student s2 = new
Student("2", "Andy", "Robert", "C", 3);
System.out.println("\nadding
student: "+s2);
section.enrollStudent(s2);
System.out.println("\nAll Student
in the Section:");
System.out.println("---------------------------");
section.displayStudents();
// now add one more student it
should say section is full;
Student s3 = new
Student("1", "Ben", "Stokes", "C++", 8);
System.out.println("\nadding
student: "+s3);
section.enrollStudent(s3);
System.out.println("\nAll Student
in the Section:");
System.out.println("---------------------------");
section.displayStudents();
// now delete the student from
enroll list
System.out.println("\nremoving
student: "+s1);
section.removeStudent(s1);
System.out.println("\nAll Student
in the Section:");
System.out.println("---------------------------");
section.displayStudents();
}
}
Output:
Please specify if any changes required. Thanks