• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not showing student  RSS feed

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I thought I was done with this project when of course something does not go as planned (of course). So what I want to happen is when I hit the show section button that it displays the section name, seats left, and whos taking the class. I have the name and seats left down, but a no go on the name part. I got a tip to call section.registerStudent(this); in the student Class. I was going to put it in the addSection which made sense to me. However my addSection passes a string, and my registerStudent passes a student so that doesnt work...unless I just make a Student student object at the top?. Any thoughts?

Student Class



My improved register method, and method to return the name




Code for my window to show all the section info just incase

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what problem you are facing?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What does that call to getStudentNameList return?

That's the point I would be debugging it.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my getStudentName.




My problem was that I couldnt get the student name to show up if they registered for a particular class. I was told to add a registerStudent method (which should be included in my original post), and that it had to be called in my student class. I was thinking about it more last night, I decided to call the method in the constructor that way the student itself is responsible for registering for the class.

So now my student look like this. Which still wont work because my section is going to be null. Not sure exactly what my section should be set to..


 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a Section?
What does it represent?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have dead code here:
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave: more info on what OP is doing here: https://coderanch.com/t/671196/java/java/Finding-Sections-Id-number

@Cody: there are still a few things in your code / design that don't make sense.

In the Student class:

Line 8 says that "each student can be registered in multiple sections"
Line 9 says that "each student has one section"

These are contradictory. Which one is it, multiple sections or just one section? The contradiction makes your addSection() method code make no sense at all:

Line 45 says to add the section that was passed in to the list of registered sessions.

Line 47 then checks if registeredSections list is not null. Well, if the list was null, then a NullPointerException would have already been thrown on line 45 so this check is basically pointless and line 52 is essentially unreachable. In other words, line 52 will never be executed. Ever. Neither will lines 49-50, for that matter.

But let's just say for argument's sake that line 52 were to be executed somehow.  Given the way you are calling the method, the section variable you are referencing there needs to be a Section object. Therefore, what you are really trying to get at is the section instance variable declared on line 9, not the section parameter on line 43.

You have to remember that the computer follows very precise rules when executing instructions. Each word in your program has a specific meaning and significance. The computer can't read your mind and figure out what you meant. YOU have to understand what you're saying in your program and how the computer will interpret what you said. Bugs are simply manifestations of differences between what you thought you told the computer to do versus what the computer actually understands your instructions to mean. Only one of these can be wrong and it's never the computer.
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some more strangeness:

On line 28, you ask a Catalog object to get a Major by name, passing it the name of the Major you want. You assign whatever the result of that call is to the local variable m.
On line 29, you make sure that m is not null.
On line 31, you again call that Catalog object to get a Major by name, passing it the same name as before. You then assign whatever the result of that call is to the instance variable major.

Do you see what doesn't make sense in this series of instructions?

If not, it's not unlike this imaginary conversation at a fast food restaurant:

You: Can I get a cheeseburger, please?
Attendant: Ok. (gets a cheeseburger and hands it to you) Here you go buddy.
You: (Looking at cheeseburger) Oh, so you do have cheeseburgers! (throwing away the one you were just handed) In that case, can I please have a cheeseburger?
Attendant: (gets another cheeseburger and hands it to you) Here you go buddy. (WTH...?!)
You: (putting the cheeseburger in your pocket for later) Ok, bye!
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. Maybe I should of been a little more clear. In my last post I mentioned I removed the registerStudent out of the addSection method. I realized that I needed to get the student object, but put it in a method that was giving me a string. So the only way I could see to get that student object was to put section.registerStudent(this); in the constructor.

As far as the Section section I just put that there so I could use the section registerStudent line. I know I still need to set that section to something, and thats what I was wondering.
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:I know I still need to set that section to something, and thats what I was wondering.

I think your first priority is to decide what the relationship between Student and Section is.  Obviously, a Section can have many Students, which is why you have section.registerStudent(Student s)

But you need to go back to lines 8 and 9.  Again, these are contradicting each other. One of these lines must be deleted. So, either a Student can belong to multiple Sections or a Student can belong to only one Section. It can't be both. Which one is it?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as the setMajor goes I honestly couldnt tell you...I remember working that out with my instructor early in the semester, hes never said anything to me about it for as many times as he has seen it
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:As far as the setMajor goes I honestly couldnt tell you...

Surely you can at least figure out how to say something in plain English that makes sense...
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:I remember working that out with my instructor early in the semester, hes never said anything to me about it for as many times as he has seen it

I find that very disturbing and disappointing.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well then I would have to get rid of the Section section line.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Cody Biggs wrote:I remember working that out with my instructor early in the semester, hes never said anything to me about it for as many times as he has seen it

I find that very disturbing and disappointing.

I think hes fine with everything as long as it runs, thats probably his biggest thing
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to approach the design. How you do things exactly will depend entirely on the design choices you make. Are you going to make each Student keep a List<String> for registered sections? If so, then you're going to need an object that has a getSectionByName(String sectionName).  If you don't already have a reference to such an object, you're going to need its class to have a getInstance() method, much like what you have with the Catalog class.
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:I think hes fine with everything as long as it runs, thats probably his biggest thing

That's even more disturbing. That kind of attitude is why we have fiascos like healthcare.gov in the real world.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my getSectionByName is in my TermSchedule Class, and yes it does have getinstance()

 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:So my getSectionByName is in my TermSchedule Class, and yes it does have getinstance()


So, draw up a little "conversation" in plain English that you can translate to Java. I'll get you started:

To make a Student "remember" what sections he's registered in:
1. Add the section name to the list of registered sections
2. Ask the TermSchedule object to find the Section object
3. ...

You finish the story, then translate it to Java.
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, this might be less confusing for you:

To make a Student "remember" what sections he/she is registered in, have each Student keep a list of section names.

Every time a Student is registered in a new section, do the following:
1. Add the new section's name to the Student's list of registered sections
2. Ask the TermSchedule object to find the Section object with a matching name
3. ... you finish it...
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another tip about the design. It seems to me that your instructor has a design in mind that's similar to a double entry accounting system.  That is, on the one hand, each Student tracks what all the Sections he/she is registered with. On the other hand, each Section object needs to track all the Students who are registered in it.  So whenever a Student is registered, you have to "book" entries in two places: one entry goes in the Student to associate it with a Section and the other entry goes into the Section, to associate it with a new Student.

There are complications that this design presents but I won't bring those up now so as not to confuse you further. However, it's important that you understand this double-entry booking concept so that you can write the correct code that needs to go into that addSection() method. This understanding will eventually lead you to write the code that will correctly display the list of Students who are registered in a Section.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not gonna lie you already lost me. I'm kinda of out of it today. Being sick sucks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Come back when you are better. Phone your school office and tell them you have been ill and consider asking for an extension.
 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To make a Student "remember" what sections he/she is registered in, have each Student keep a list of section names.

Every time a Student is registered in a new section, do the following:
1. Add the new section's name to the Student's list of registered sections
2. Ask the TermSchedule object to find the Section object with a matching name
3. ... you finish it...

Cody Biggs wrote:Not gonna lie you already lost me. I'm kinda of out of it today. Being sick sucks

Ok, let me correlate the above narrative with what you already wrote then, maybe that will clear things up for you when you feel better and come back to it.

"To make a Student "remember" what sections he/she is registered in, have each Student keep a list of section names."  is translated to Java as:

As others have probably pointed out to you before, an instance member is normally declared private and the type declaration should be the interface type (List), not the implementation type (ArrayList). The element type on the right hand side is optional in Java 7 and greater.

Next, "Every time a Student is registered in a new section, do the following" suggests a method call. What method? Well, this one:

Next, "Add the new section's name to the Student's list of registered sections" is this line:

So, you need to figure out what the equivalent of "Ask the TermSchedule object to find the Section object with a matching name" is in Java. Remember what we already discussed regarding calling getInstance() and getSectionByName() methods. After you have done that, you can call section.registerStudent(this) but you have to figure out how to reference the Section object that you got back instead of the session parameter that was passed in to the method. The easiest way to do that is to use a different variable name.

Then you have to finish the rest of the narrative.

I have given you just about everything you need to complete that part of your program, short of giving you the actual code.  You have to THINK about what exactly you want the computer to do in plain English terms, then translate that to the equivalent Java commands.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well still feel like crap, but this is what I came up with and it works. I ran it, and it gives me the student name now

 
Junilu Lacar
Sheriff
Posts: 11480
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:Well still feel like crap, but this is what I came up with and it works. I ran it, and it gives me the student name now


Yeah, you're probably going to have to revisit this when you get asked to list out all the sections that a Student is registered in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!