My classes compile but for some reason, I always get false when I try to add a student to a course. could someone please help me?

public abstract class Student extends Person {
	protected Course[] courses;
	protected final int maxCourses = 4;
	protected int numCoursesEnrolled;
	protected double gpa;
	
	public Student(String name, int UFID, String dob, double gpa) {
		super(name, UFID, dob);
		this.gpa = gpa;
		courses = new Course[maxCourses];
		numCoursesEnrolled = 0;
	}
	public Student(String name, int UFID, String dob, double gpa, Course[] courses) {
		super(name, UFID, dob);
		this.gpa = gpa;
		this.courses = courses;
		for(int i=0; i<courses.length; i++) {
			if (courses[i] != null) 
				numCoursesEnrolled++;
		}
	}
	
	public int getNumCoursesEnrolled() {
		return numCoursesEnrolled;
	}
	public void setNumCoursesEnrolled(int numCoursesEnrolled) {
		this.numCoursesEnrolled = numCoursesEnrolled;
	}
	public double getGpa() {
		return gpa;
	}
	public void setGpa(double gpa) {
		this.gpa = gpa;
	}
	
	public abstract boolean addCourse(Course course);
	
	public boolean dropCourse(Course course) { 
		boolean ret = false;
		for(int i=0; i<courses.length; i++) {
			if (courses[i] == course) {
				if (courses[i].removeStudent(this)) {
					courses[i] = null;
					numCoursesEnrolled--;
					ret = true;
				}
			}
		}
		return ret;
	}



	public String toString() {
		String s = super.toString() + "\nGPA: " + gpa + "\nCourses enrolled in: "; 
		for(int i=0; i<numCoursesEnrolled; i++) {
			s += "Course " + i + ":\n" + courses[i].toString();
		}
		return s;
	}
}
public class UndergradStudent extends Student {
	public UndergradStudent(String name, int UFID, String dob, double gpa) {
		super(name, UFID, dob, gpa);
	}
	public UndergradStudent(String name, int UFID, String dob, double gpa, Course[] courses) {
		super(name, UFID, dob, gpa, courses);
	}
	
	public boolean addCourse(Course course) {
		if(course.getNumber() < 5000 && numCoursesEnrolled < maxCourses && course.addStudent(this)) {
			for(int i=0; i<courses.length; i++) {
				if(courses[i] == null) {
					courses[i] = course;
					break;
				}
			numCoursesEnrolled++;
			}
			return true;
		}
		else return false;
	}
	
	public String toString() {
		return "Undergrad Student: " + super.toString();
	}
}
public class Course {
	private String type;
	private String title;
	private int number;
	private int numCredits;
	private Instructor instructor;
	private GradStudent[] TAs;
	private Student[] students;
	private int capacity;
	private int currentEnrollment;
	
	public Course(String type, int number, String title, int numCredits) {
		this.type = type;
		this.number = number;
		this.title = title;
		this.numCredits = numCredits;
	}
	public Course(String type, int number, String title, int numCredits,
			Instructor instructor, GradStudent[] TAs, int capacity) {
		this(type, number, title, numCredits);
		this.instructor = instructor;
		this.TAs = TAs;
		students = new Student[capacity];
		currentEnrollment = 0;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public int getNumCredits() {
		return numCredits;
	}
	public void setNumCredits(int numCredits) {
		this.numCredits = numCredits;
	}
	public int getCapacity() {
		return capacity;
	}
	public void setCapacity(int capacity) {
		Student[] students1 = new Student[capacity];
		for(int i=0; i<students.length; i++) 
			students1[i] = students[i];
		students = students1;
	}

	public int getCurrentEnrollment() {
		return currentEnrollment;
	}
	public void setCurrentEnrollment(int currentEnrollment) {
		this.currentEnrollment = currentEnrollment;
	}
	public Student[] getStudents() {
		return students;
	}
	public void setStudents(Student[] students) {
		this.students = students;
	}
	public Instructor getInstructors() {
		return instructor;
	}
	public void setInstructors(Instructor instructor) { //set course variable as well
		this.instructor = instructor;
	}
	public GradStudent[] getTAs() {
		return TAs;
	}
	public void setTAs(GradStudent[] TAs) {
		this.TAs = TAs;
	}
	
	public boolean addStudent(Student student) {
		if(currentEnrollment < capacity) {
			students[currentEnrollment] = student;
         		currentEnrollment++;
			return true;
		}
		else return false;
	}
	
	public boolean removeStudent(Student student) {
		for(int i=0; i<students.length; i++) {
			if(students[i].equals(student)) {
				students[i] = null;
				--currentEnrollment;
				return true;
			}
		}
		return false;
	}


	public String toString() {
		String s = "Course Info: " + type + number + "\nTitle: " + title + "\nInstructor: " + instructor.getName() + "\nTAs: ";
		for(int i=0; i<TAs.length; i++) {
			s += "\n" + TAs[i].getName();
		}
		 s += "\nNumber Of Students: " + currentEnrollment + "\nCapacity: " + capacity;
		 return s;
	}
}

For debugging and seeing what code is doing as it executes, add lots of printlns that print out the values of variables and conditions used in if tests and to show execution flow through the methods..
The print outs will show you what the program is doing so you can fix it.

public abstract class Student extends Person {
protected Course[] courses;

and

public class Course {

private Student[] students;

I think this MIGHT be the reason. If you first create student object, course will be empty and if you first create course object student there will be no students for initialization. I am not sure , because you have also provided constructors without them . Maybe if you could post your main method where you have created objects and called functions on them.

And you can always go with what NormR1 just suggested

This article has been dead for over six months. Start a new discussion instead.