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

My code is not matching up with the results. The output for the code should be:

ID: 3935772 • Letter: M

Question

My code is not matching up with the results.

The output for the code should be:

>>> Test Teacher class
Teacher: Paul Tan(8 sunset way)
IM101 added.
IM102 added.
IM101 cannot be added.
IM101 removed.
IM101 removed.

IM102 removed.

IM102 cannot be removed.

Person class:

public class Person {
  
   private String name;
   private String address;
  
   public Person(String name, String address) {
       this.name = name;
       this.address = address;
   }
   public Person(String address){
       this.address = address;
   }
  

   public String getName(){
       return name;
   }
  
   public String getAddress(){
       return address;
   }
  
   public void setAddress(String address){
       this.address = address;
   }
  
   public String toString() {
       return name + "(" + address + ")";
   }
}

Teacher sub class

import java.util.ArrayList;

public class Teacher extends Person {
   private int numCourses = 0;
   private ArrayList<String> courses = new ArrayList<String>();
  
   public Teacher(String name, String address){
       super(name,address);
   }
   @Override
   public String toString() {
       return "Teacher: " + super.toString();
   }
   public boolean addCourse (String course){
       for (int i = 0; i < numCourses; i++) {
       if (courses.get(i).equals(course))
       return false;
   }
       courses.add(course);
       numCourses++;
       return true;
   }
   public boolean removeCourse (String course){
       boolean found = false;
       int courseIndex = -1;
       for (int j = 0; j < numCourses; j++) {
           if (courses.get(j).equals(course)) {
           courseIndex = j;
           found = true;
           break;
           }
       }
       if (found) {
           for (int k = courseIndex; k < numCourses-1; k++) {
           courses.get(k+1);
           }
           numCourses--;
           return true;
       }
       else {
       return false;
   }
}
}

Person Launcer

System.out.println(" ");
       System.out.println(">>> Test Teacher class");
       Teacher tch = new Teacher ("Paul Tan", "8 sunset way");
       System.out.println(tch);
       String[] courses = {"IM101", "IM102", "IM101"};
       for (String course: courses) {
       if (tch.addCourse(course)) {
       System.out.println(course + " added.");
       } else {
       System.out.println(course + " cannot be added.");
       }
       }
       for (String course: courses) {
       if (tch.removeCourse(course)) {
       System.out.println(course + " removed.");
       } else {
       System.out.println(course + " cannot be removed.");
       }
       }
       System.out.println(" ");

Explanation / Answer

// edit the below method like this :

public boolean removeCourse (String course){
       boolean found = false;
       int courseIndex = -1;
       for (int j = 0; j < numCourses; j++) {
           if (courses.get(j).equals(course)) {
           courseIndex = j;
           found = true;
           break;
           }
       }
       if (found) {
courses.remove(course);
           numCourses--;
           return true;
       }
       else {
       return false;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote