Drop and Add Students always returns False

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2697 Views - Last Post: 13 February 2012 - 09:18 PM Rate Topic: -----

#16 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 12:22 PM

Quote

then when would I use the remove student class?

I dint get what you mean here?

If the class is comparable, then he have to implements Comparable interface. But the implemented equals() can do the work here and he has a thread about that .
So calling the equals() method will do the trick. The doc said about implementing compareTo()

Quote

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

Was This Post Helpful? 0
  • +
  • -

#17 kikib92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 01:34 PM

Ok. So basically, my dropCourse method implements the removeStudent method in the Course class (which searches the array for the specified student using the .equals method and sets the student at that index to null). So first, I have to find where the course is in the array of courses. I would do that using the .equals method I will write in the course class. If removeStudent returns true, then the course is set to null and numEnrolled decreases. Does that sound correct?

I made those changes and I still get the same errors :/
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].equals(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 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 boolean equals(Object ob) {
    		if (ob instanceof Course)
      			return title.equals(((Course)ob).getTitle());
      		else 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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#18 v0rtex   User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 01:42 PM

You still have the if condition checking if the student has been removed from the course when you have not actually handled removing the student from the course in the first place. Here is your code:
 public boolean dropCourse(Course course) {

        boolean ret = false;

        for(int i=0; i<courses.length; i++) {

            if (courses[i].equals(course)) {

                if (courses[i].removeStudent(this)) { /*checking for if course has been removed and yet you have not actually handled removing the course itself */

                    courses[i] = null;

                    numCoursesEnrolled--;

                    ret = true;

                }

            }

        }

        return ret;

    }



Rather remove the course like so:

public boolean dropCourse(Course course) {

        boolean ret = false;

        for(int i=0; i<courses.length; i++) {

            if (courses[i].equals(course)) {
                courses[i].removeStudent(this); //actually removing the student from the course, if condition is gone

                    courses[i] = null;

                    numCoursesEnrolled--;

                    ret = true;

                }
             }
        return ret;
    }



the if condition will never execute as it will never evaluate to true because you are not removing the student from the course beforehand, I think that if is unnecessary, rather leave it out. Apart from that your code looks good, sorry if I missed something. I haven't actually run the code.

This post has been edited by v0rtex: 13 February 2012 - 01:50 PM

Was This Post Helpful? 1
  • +
  • -

#19 kikib92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 01:56 PM

I thought that would work. But I'm still getting this:

---Creating undergrad course1 with capacity 2---
--COP3503: Programming Fundamentals
Trying to add student1 to course1: false
Trying to add gs1 to course1: false
Trying to add student3 to course1: false
Trying to add student2 to course1: false
-Increasing capacity of course1 to 3--
Trying to add student2 to course1: false
Exception in thread "main" java.lang.NullPointerException
at Student.dropCourse(Student.java:55)
at TestDriver1.main(TestDriver1.java:51)

Thank you for all of your help so far. I really appreciate it.
Would it be helpful if I uploaded the driver I'm using?
Was This Post Helpful? 0
  • +
  • -

#20 kikib92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 08:09 PM

I'm on the verge of figuring this out! :)
I used println to discover the error I made when adding and dropping students. The only problem I have left is printing out the course data. I found out it has nothing to do with the toString method in my Course class. The problem is with this for-loop going through the array of course objects in the Student class. For some reason, I cant do i<courses.length. I get a null pointer exception, so I'm left with putting numCoursesenrolled. :/

	public String toString() {
		String s = super.toString() + "\nGPA: " + gpa + "\nCourses enrolled in: "; 
		for(int i=0; i<numCoursesEnrolled; i++) {
			s = s + "Course " + i + ":\n" + courses[i].toString();
		}
		return s;
	}


This post has been edited by kikib92: 13 February 2012 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#21 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 08:38 PM

And we answered that in your previous thread, so it was your duty to change it and you didnt:

View Postsmohd, on 13 February 2012 - 08:55 PM, said:

In your toString() method, you are looping through the course array. But remember that some students have no all courses to fill the array so your array will have some null indexes:
 for(int i=0; i<courses.length; i++) {

Instead loop only to the maximum number of courses enrolled.

Was This Post Helpful? 1
  • +
  • -

#22 kikib92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 08:48 PM

View Postsmohd, on 13 February 2012 - 08:38 PM, said:

And we answered that in your previous thread, so it was your duty to change it and you didnt:

View Postsmohd, on 13 February 2012 - 08:55 PM, said:

In your toString() method, you are looping through the course array. But remember that some students have no all courses to fill the array so your array will have some null indexes:
 for(int i=0; i<courses.length; i++) {

Instead loop only to the maximum number of courses enrolled.



I did change it to that and I got this:

Exception in thread "main" java.lang.NullPointerException
at Student.toString(Student.java:58)

But when I leave it as numCoursesEnrolled I don't get an error, but It doesn't go through the array. I tested the method and everything works but "courses[i].toString()"
Was This Post Helpful? 0
  • +
  • -

#23 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 08:54 PM

Quote

But when I leave it as numCoursesEnrolled I don't get an error

I that was the change which I was talking about!
If it is not looping to the array means you are not increasing numCoursesEnrolled when adding a new course to a student. Can I see the method you used to add courses to the courses array?
Was This Post Helpful? 0
  • +
  • -

#24 kikib92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 106
  • Joined: 26-September 11

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 09:10 PM

The arrays are created in the driver as so:

GradStudent[] tas = {gs1, gs2};
UndergradStudent[] students1 = {student1, student3};

Objects are created in the test driver and then put into an array.
The Student class houses the course array.
Was This Post Helpful? 0
  • +
  • -

#25 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Drop and Add Students always returns False

Posted 13 February 2012 - 09:18 PM

Now this depends on how you created your Students because the first constructor of the student class does not add courses with him/her. So you will have students with no courses. And you have no any addCourse() method may be to add courses to students when using the first constructor. That will mean those students have no courses with them. Can we see how you create your students in your test driver class?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2