Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

implement methods in the provided Course class. I have provided a class called S

ID: 639920 • Letter: I

Question

implement methods in the provided Course class. I have provided a class called Student. A student is described by a first name, last name, and ID. Review this class and make sure you understand the code. Then implement the three missing methods in the Course class, as described below. A course is described by a name and a course roster. The roster is stored as an array of Student objects.

Add code to the constructor to initialize the roster.

Add any instance data variables you need to make the program function. Think about what variables you might need. For any variables you include, add getters and setters for those variables (as appropriate) and initialize the variables in the constructor.

Write a method to add a student to the course roster. The method should return true if the student is added successfully (meaning there is room for the student) and false otherwise.

Write a method to drop a student from the course roster. The method should return true if the student was successfully dropped (meaning they were in the course and no longer are) and false otherwise.

Implement the toString method. This method should return text that contains the course name, the number of students enrolled, the maximum number of student that can be enrolled, and a printed roster of enrolled students (if there are any).

I have provided a driver program you can use to test your code and sample output. You might add additional code to the driver to test your Course class.

# Add code to the constructor to initialize the roster.

# Add any instance data variables you need to make the program function. Think about what variables you might need. For any variables you include, add getters and setters for those variables (as appropriate) and initialize the variables in the constructor.

# Write a method to add a student to the course roster. The method should return true if the student is added successfully (meaning there is room for the student) and false otherwise.

# Write a method to drop a student from the course roster. The method should return true if the student was successfully dropped (meaning they were in the course and no longer are) and false otherwise.

# Implement the toString method. This method should return text that contains the course name, the number of students enrolled, the maximum number of student that can be enrolled, and a printed roster of enrolled students (if there are any).

I have provided a driver program you can use to test your code and sample output. You might add additional code to the driver to test your Course class.

*************************************************************************************


public class CourseTester {

   public static void main(String[] args) {
       Student s1 = new Student("Adam", "Ant", "S925");
       Student s2 = new Student("Bob", "Barker", "S713");
       Student s3 = new Student("Chevy", "Chase", "S512");
       Student s4 = new Student("Doris", "Day", "S513");
       Student s5 = new Student("Emilio", "Estevez", "S516");
       Student s6 = new Student("Farrah", "Fawcet", "S956");
      
       Course c1 = new Course("Media Studies", 5);
       System.out.println(c1);

       if(!c1.addStudent(s1))
           System.out.println(s1 + " could not be added.");
       if(!c1.addStudent(s2))
           System.out.println(s2 + " could not be added.");
       if(!c1.addStudent(s3))
           System.out.println(s3 + " could not be added.");
       if(!c1.addStudent(s4))
           System.out.println(s4 + " could not be added.");
       if(!c1.addStudent(s5))
           System.out.println(s5 + " could not be added.");
       if(!c1.addStudent(s6))
           System.out.println(s6 + " could not be added.");
      
       System.out.println(c1);
      
       c1.dropStudent(s6);
       System.out.println(c1);
      
       c1.dropStudent(s1);
       System.out.println(c1);
      
       c1.dropStudent(s2);
       System.out.println(c1);
      
       c1.dropStudent(s5);
       System.out.println(c1);
      
       c1.dropStudent(s3);
       System.out.println(c1);
      
       c1.dropStudent(s4);
       System.out.println(c1);
      

      
   }

}

**************************************************

public class Student {

   private String firstName, lastName, id;
   private boolean tuitionPaid;

   public Student(String firstName, String lastName, String id, boolean tuitionPaid) {
       setFirstName(firstName);
       setLastName(lastName);
       setID(id);
       setTuitionPaid(tuitionPaid);
   }

   public Student(String firstName, String lastName, String id) {
       this(firstName, lastName, id, true);
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public String getID() {
       return id;
   }

   public void setID(String id) {
       this.id = id;
   }

   public boolean isTuitionPaid() {
       return tuitionPaid;
   }

   public void setTuitionPaid(boolean tuitionPaid) {
       this.tuitionPaid = tuitionPaid;
   }

   @Override
   public boolean equals(Object obj) {
       if (obj instanceof Student) {
           Student otherStudent = (Student) obj;
           return id.equalsIgnoreCase(otherStudent.id);
       } else {
           return false;
       }
   }

   @Override
   public String toString() {
       return firstName + " " + lastName + " (" + id + ")";
   }

}

***********************************************************************


public class Course {

   private String name;
   private Student[] roster;
   private int maxStudents;
      
   public Course(String name, int maxStudents) {
       setName(name);
       setMaxStudents(maxStudents);
       // ADD CODE HERE (IF NECESSARY)
   }


   public int getMaxStudent() {
       return maxStudents;
   }

   public void setMaxStudents(int maxStudents) {
       if(maxStudents > 0) {
           this.maxStudents = maxStudents;
       }
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
  
   public boolean addStudent(Student s) {
       //IMPLEMENT THIS METHOD
       return false;
   }
   public boolean dropStudent(Student s) {
       // IMPLEMENT THIS METHOD
       return false;
   }
  
   @Override
   public String toString() {
       // IMPLEMENT THIS METHOD
       return "";
   }
  
}

*****************************************************

Please help!

Explanation / Answer

public class Course {

   private String name;
   private Student[] roster;
   private int maxStudents;
   private int count;

   public Course(String name, int maxStudents) {
       setName(name);
       setMaxStudents(maxStudents);
       roster = new Student[this.maxStudents];
       count = 0;
   }

   public int getMaxStudent() {
       return maxStudents;
   }

   public void setMaxStudents(int maxStudents) {
       if (maxStudents > 0) {
           this.maxStudents = maxStudents;
       }
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public boolean addStudent(Student s) {
//IMPLEMENT THIS METHOD
   if(count < maxStudents){
       roster[count++] = s;
       return true;
   }
return false;
}

   public boolean dropStudent(Student s) {
       // IMPLEMENT THIS METHOD
       if(count != 0){
           for(int i = 0; i < count; ++i){
               if(roster[i] == s){
                   for(int j = i; j < count - 1; ++j){
                       roster[j] = roster[j + 1];
                   }
                   count--;
                   return true;
               }
           }
       }
       return false;
   }

   @Override
   public String toString() {
       // IMPLEMENT THIS METHOD
       String ret = String.format("Course Name: %s " +
               "Number of students enrolled: %d " +
               "Number of Maximum number of students %d ", name, count, maxStudents);
       for(int i = 0; i < count; ++i){
           System.out.println(roster[i]);
       }
       return ret;
   }

}

Dr Jack
Hire Me For All Your Tutoring Needs
Quick quotes • Clear explanations • Study support
Chat Now And Get Quote