File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Drop and Add Students to Course Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Drop and Add Students to Course" Watch "Drop and Add Students to Course" New topic
Author

Drop and Add Students to Course

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
So, I have multiple classes modeling a university. I have Student which has subclasses GradStudent and UndergradStudent. I also have a course class. I am having issues figuring out what to pass to add and drop students and to add and remove courses.
I keep getting errors like this:
./Student.java:39: cannot find symbol
symbol : variable student
location: class Student
if (course.removeStudent(student)) {
^
^
./GradStudent.java:23: cannot find symbol
symbol : variable student
location: class GradStudent
if(course.getNumber() >= 5000 && numCoursesEnrolled < maxCourses && course.addStudent(student)) {

If someone could help, I would really appreciate it!
Here are my classes:




And methods of the subclasses:



Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Hi KD

In the below method, which student are you referring to? The compiler doesn't know what you're talking about. Do you mean the student represented by this instance of the Student class? (I.e. the student for whom a course is being added)


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I suppose I am doing this student. The instructions given are somewhat vague and just say that it will add student and have Student student as a parameter. I will be using a driver to test the program. So should I just have course.removeStudent(this.student)?
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Thank you so much for pointing that out! So I changed the methods to pass this.student.
It worked for my Instructor class:

But for undergrad and grad I still get theses errors:
./Student.java:39: cannot find symbol
symbol : variable student
location: class Student
if (course.removeStudent(this.student)) {
^
./Student.java:50: cannot find symbol
symbol : variable course
location: class Student
String s = super.toString() + "\nGPA: " + gpa + "\nCourses enrolled in: " + course.toString();
^
GradStudent.java:23: cannot find symbol
symbol : variable student
location: class GradStudent
if(course.getNumber() >= 5000 && numCoursesEnrolled < maxCourses && course.addStudent(this.student)) {

And for Student I get these:

Student.java:39: cannot find symbol
symbol : variable student
location: class Student
if (course.removeStudent(this.student)) {
^
Student.java:50: cannot find symbol
symbol : variable course
location: class Student
String s = super.toString() + "\nGPA: " + gpa + "\nCourses enrolled in: " + this.course.toString();


These are the changes I made:
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Kd Martin wrote:I suppose I am doing this student. The instructions given are somewhat vague and just say that it will add student and have Student student as a parameter. I will be using a driver to test the program. So should I just have course.removeStudent(this.student)?

Close, but no cigar. 'this' refers to the Student instance which is executing your code. So by writing 'this.student' in your Student class you are effectively saying "this student's student property" instead of just saying "this student".
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Alright then, I'm not sure of what else to do instead of this.student. Sorry, I'm still new to this.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Kd Martin wrote:Alright then, I'm not sure of what else to do instead of this.student. Sorry, I'm still new to this.

Well, 'this' is a student. I'm trying to help you figure the answer out, instead of just giving it to you - you'll learn more that way. So, to reinforce what I've just said - you need a student; 'this' is a student. Get it?

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Perfect, thanks so much Riaan Nel! I've solved this problem! Everything compiled perfectly!
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Kd Martin wrote:Perfect, thanks so much Riaan Nel! I've solved this problem! Everything compiled perfectly!

You're most welcome! I'm glad you got everything figured out!
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Well, every class in this project compiles, and the logic of my methods seems correct to me, but when I run my driver, I always get false for when I try to add and drop students. Could someone help me pinpoint the error(s) in my logic?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18715
    
    8

You do that by looking at the code. But first you have to figure out what code to look at: I don't know what method is called when you try to "drop a student", as you say, because I'm not sure if "removeStudent" or "dropCourse" is really where I should be looking. So that would be your first step.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Paul Clapham wrote:You do that by looking at the code. But first you have to figure out what code to look at: I don't know what method is called when you try to "drop a student", as you say, because I'm not sure if "removeStudent" or "dropCourse" is really where I should be looking. So that would be your first step.


Well, I've gone through both my add/removeStudent and add/dropCourse methods and made edits to check for equality and nothing has helped. Here they are:

>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18715
    
    8

I see. Then what I said was wrong -- your first step should be to find out what the problem is. I don't believe I saw a description of your latest problem, so how about if you produce that?
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Ok. So I should be able to create courses and students and drop and add courses. Everything correctly compiles and I can create objects, but every time I try to add a student, the method returns false, and as soon as I try to drop students from courses, I get a null pointer exception. I'm assuming I get this because Students are not being added correctly in the first place, but I can't figure out why that would be.

These are my instructions:
For the Course class:
-addStudent(Student student) - This is a public method which returns a boolean value depending on whether the student was successfully added or not.
It will only add a student if the current enrollment is less than the capacity. If there is enough space, it
adds a student to the array in the first index where there is a null object. It returns true in this case. Otherwise, it returns false.

-removeStudent(Student student) - Removes a student from the students array if the student exists (HINT: use the .equals() method)
Sets the object at that student's index to be a null object, and then decreases the currentEnrollment. It returns true in this case.
If students is not found it returns false.
For the Student class:
-dropCourse(Course course) - This is a public method which returns a boolean value.
It removes a student from the course (HINT: Use the Course class' removeStudent(...) method)
If student has been removed from the course, it updates the courses array by setting the course at that
index in the array to null, and updates numCoursesEnrolled appropriately.
^^I have an equals method for this class that I use in the course class, but we were not told to make an equals method for the Course class.

Undergrad subclass:
-addCourse(Course course) - Implements the addCourse method declared in the super-class. Only adds a course if all of these conditions are satisfied:
1) The course number is < 5000 (greater than or equal to 5000 implies a graduate course)
2) if the number of courses the student is enrolled in is less than the max number of courses the student can take
3) if the course can accomodate the student (HINT: Use the addStudent(...) method in the Course class)

If these conditions are satisfied, the course is put inside the courses array - this is done by putting the course in the first spot it finds
a null object. Number of courses the student is enrolled in is increased by 1 and the method returns true

false otherwise.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

Kd Martin wrote:Ok. So I should be able to create courses and students and drop and add courses. Everything correctly compiles and I can create objects, but every time I try to add a student, the method returns false, and as soon as I try to drop students from courses, I get a null pointer exception. I'm assuming I get this because Students are not being added correctly in the first place, but I can't figure out why that would be.


Do one problem at a time. As an example, get add working first.... Now that you know that add is always returning false, you now need to determine why? Add debugging statements, or step through the execution. Figure out why the problem is happening. And if it seems like too much, then break the problem down to simpler components, and debug each one separately.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18715
    
    8

That's better. Yes, the way you wrote addStudent and removeStudent is a sure-fire way of producing NullPointerExceptions. But let's start with the first problem: when you call addStudent(Student), it always returns false. (Did I understand that correctly?) In that case, let's look at the code:

So when would that method return false? Well, that could only be if the else-clause were run, right? So that's what is happening. And when would the else-clause be run? That could only be because the boolean expression in the if-statement is false, right? So that's what is happening.

And that boolean expression?

We determined that it's false. So it's not the case that the currentEnrollment variable is less than the capacity variable. And you're going to be surprised by that because you expected currentEnrollment to be zero initially and capacity to be some positive number which is the size of an array. But if that's how it was, then that boolean expression would be true. And it's false. So now you have cognitive dissonance because that wasn't what you expected. This is the time to believe what the computer says, regardless of what you think should be the case. Something is fishy about one or both of those two variables at run time, so now it's time to do some debugging and find out just what their values are when you call that method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
We determined that it's false. So it's not the case that the currentEnrollment variable is less than the capacity variable. And you're going to be surprised by that because you expected currentEnrollment to be zero initially and capacity to be some positive number which is the size of an array. But if that's how it was, then that boolean expression would be true. And it's false. So now you have cognitive dissonance because that wasn't what you expected. This is the time to believe what the computer says, regardless of what you think should be the case. Something is fishy about one or both of those two variables at run time, so now it's time to do some debugging and find out just what their values are when you call that method.


And this is the part where so many beginners get hung up. You write the code, you have certain assumptions about what each piece of it does, and from that you build assumptions about what the overall behavior will be, and then when that overall behavior isn't what you expected, you become lost. That's when you need realize, "One or more of my assumptions was wrong. Now I need to find out which one." And the way you do that is by looking at what each little piece of the code is doing--such as with print statements or a debugger. Once you see a little piece that doesn't behave according to your assumptions, you have a starting point.

The problem is when people just stare at the whole code at once and go, "I have no idea." Gotta remember: Big things are made of little things. :)
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Paul Clapham wrote:Yes, the way you wrote addStudent and removeStudent is a sure-fire way of producing NullPointerExceptions.

What do you mean by this? And I'm not sure how to go about debugging a project this large with print statements
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:
Paul Clapham wrote:Yes, the way you wrote addStudent and removeStudent is a sure-fire way of producing NullPointerExceptions.

What do you mean by this?


What he means is that your code has bugs that will give you NullPointerExceptions when you call addStudent() or removeStudent().

And I'm not sure how to go about debugging a project this large with print statements


The same way you'd debug a smaller program with print statements, but just with more of them.

A better approach would be to start fresh. Keep this code handy for reference and to add pieces in, but start with the smallest program you can make work. Then add a little to it, like the add feature that Paul mentioned. If that gives you trouble, add print statements for debugging. Once you get it working, you can take them out if you wish. Then get another little piece working independently. Then combine the two. And so on.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
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. :/



Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
I'm confused by your Course Class constructors:

The first constructor doesn't seem to initialize the 'students' array, so this could potentially lead to problems if you construct your Course object using this constructor. Similarly, capacity would = 0 since it is not initialized to an integer > 0...this would also always give you false for: (currentEnrollment < capacity). If you do infact need to use the first constructor, you need to initialize these values to some defaults that would not break the rest of your code.

Sorry if I've misunderstood your code, but this is what I noticed when taking a look at it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Drop and Add Students to Course