• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Drop and Add Students to Course

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:



 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect, thanks so much Riaan Nel! I've solved this problem! Everything compiled perfectly!
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Paul Clapham
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. :/



 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic