Question

In: Computer Science

The directions say to "create two methods:

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."

 

Solutions

Expert Solution

//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


Related Solutions

UESTION 6 Two or more parts rotating in opposite directions create a __________. a. inrunning b....
UESTION 6 Two or more parts rotating in opposite directions create a __________. a. inrunning b. shear point c. nip point d. pinch point The process of replacing a process to reduce exposure to a hazard is ___________. a. substitution b. location c. safeguarding d. worker rotation The most hazardous type of woodworking machinery is_________? a. jointers b. lathes c. saws d. power feed planers Who may remove a lockout/tagout device? a. any authorized employee b. only the person who...
1.Two balls of mass 5 kg each moving in opposite directions with equal speed say 10...
1.Two balls of mass 5 kg each moving in opposite directions with equal speed say 10 m/s collide head on with each other. Predict the outcome of the collision assuming it to be Perfectly elastic? Perfectly inelastic?
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with...
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with exactly the same mean. What type of boundaries can be defined? show a picture of the options
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no...
create an interface called Visible that has two methods called makeVisiible, makeInvisible. both methods take no parameters and should return a boolean result. HTML EditorKeyboard Shortcuts 12pt Paragraph 0 words Flag this Question Question 58 pts The following classes have been created with the given behaviors: public class Leo extends Don { public void method1() { System.out.print("Leo 1 "); } public void method3() { System.out.print("Leo 3 "); } public String toString() { return "Leo 1 "; } } class Mike...
could someone describe briefly the two main methods of tuning a PID controlled system and say...
could someone describe briefly the two main methods of tuning a PID controlled system and say which one would not be suitable for tuning a critical system such as a power plant. The answer should not be too long (maximum 500 words) and preferably not hand written. Thanks
Use the Excel directions below to create a scatterplot for the Temperature in Fahrenheit and the...
Use the Excel directions below to create a scatterplot for the Temperature in Fahrenheit and the number of cricket chirps per sec. Temp 88.6 71.6 92.3 85.3 80.6 75.2 69.7 81 69.4 83.3 79.6 82.6 80.6 (x) Chirps 20.5 16 19.8 18.4 17.1 15.5 14.7 17.1 15.4 16.2 15 17.2 16 (y) 3.) Find the linear regression equation using Excel. Slope uses the equation =slope(highlight y column, highlight x column). Y-intercept uses the equation =intercept(highlight y column, highlight x column)....
Use the Excel directions below to create a scatterplot for the Temperature in Fahrenheit and the...
Use the Excel directions below to create a scatterplot for the Temperature in Fahrenheit and the number of cricket chirps per sec. Temp 88.6 71.6 92.3 85.3 80.6 75.2 69.7 81 69.4 83.3 79.6 82.6 80.6 (x) Chirps 20.5 16 19.8 18.4 17.1 15.5 14.7 17.1 15.4 16.2 15 17.2 16 (y) 3.) Find the linear regression equation using Excel. Slope uses the equation =slope(highlight y column, highlight x column). Y-intercept uses the equation =intercept(highlight y column, highlight x column)....
Directions: Create a program that takes as input an employee's salary and a rating of the...
Directions: Create a program that takes as input an employee's salary and a rating of the employee's performance and then computes the appropriate raise for the employee based on that rating. The performance rating is entered into the program as a String. The three possible ratings are "Outstanding", "Acceptable", and "Needs Improvement". An employee who is rated as Outstanding will receive a 10.2% raise, one rated Acceptable will receive a 7% raise, and one rated as Needs Improvement will receive...
Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT