• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Sections by name, Id, and course number  RSS feed

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am on the last part of this whole registration project (thank God). I had a similiar question like this, but that part of the project got tossed out for some reason. Now I cant find the code for that part I did actually have. I think I got the majority of it, and just need help with finding the sections by Id. However im gonna post what I have done and see if its all right anyway.

Ok so now the real stuff. I have a list of sections that look like CPT-237,W01,35  For the last part I want to get all the sections available(W01) by the course ID(Cpt-237). The get sectionByCourseID method that is there is just something I did without really thinking about it. Because that would still just return all the sections instead of the sections available to that courseId

Here is my section class


and my term schedule class



[HENRY: Fixed quote tags to code tags]
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just realized I used quote tags instead of the code tags....my bad
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 64, you shouldn't compare strings with ==.  Use equals() instead.

Same problem on line 77.

Don't compare Java String with ==
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, there are better idioms for iterating over a collection. What you wrote is kind of clunky.

Instead of this:

Prefer a combination of the following idioms:

Declaring lists:

Iterating over a collection:
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does make everything look alot better. I have seen the enhanced for loops before, but for some reason I just like using the normal for loop. What about my getSectionByCourseId? I know what I have right now, cant be right..maybe on the right track. but I cant return jut the sections, because that would give me the whole section list. I just want the sections that fall under that courseId. So it would look like CPT-172,W01,001,002..ect
 
Junilu Lacar
Sheriff
Posts: 11494
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:That does make everything look alot better. I have seen the enhanced for loops before, but for some reason I just like using the normal for loop.

I suppose you also still wear bell-bottomed jeans and tie-dyed T-shirts because for some reason you just like them.

What about my getSectionByCourseId? I know what I have right now, cant be right..maybe on the right track.

On line 12, your constructor takes a value for courseID but you never do anything with it except essentially throw it away by ignoring it.

One line 77 you check the section number against the courseId (again, don't use == when comparing strings, use equals()).  That's like checking if your driver's license number is the same as your social security card number.  I don't think either of those pairs will ever match.  And even if by some wild coincidence they did, it's still not logically correct.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Cody Biggs wrote:That does make everything look alot better. I have seen the enhanced for loops before, but for some reason I just like using the normal for loop.

I suppose you also still wear bell-bottomed jeans and tie-dyed T-shirts because for some reason you just like them.

What about my getSectionByCourseId? I know what I have right now, cant be right..maybe on the right track.

On line 12, your constructor takes a value for courseID but you never do anything with it except essentially throw it away by ignoring it.

If they are comfortable then why not? As far as the courseId Im still thinking on how to use it. I was just following the instructions for the assignment "The constructor accepts two Strings, a courseId and a section number, and an integer representing the maximum capacity of the Section" Thats all he gave us...The only thing that has come to mind is using the courseId to look up the course in my Catalog class?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of things.

Check access modifiers of your fields. These really need to be private, unless you have strong reason not to have them as such.

Another thing, you have:
So, why the code as on lines 11-14 is needed? And what it has to do with method registerStudent? You need to re-think that part. However, for the future, such block could be substituted to:

How about if that whole method would look as:
Doesn't it look better?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're overcomplicating / overthinking this.  I think in this particular design, the courseId is used merely as a reference number, kind of like how you don't go walking around with all the details of your social security contributions and benefits. If you need to know what those details are, you go to the SS office and give your SSN, to somebody who can look up your complete records.  Your SSN is a reference number and that's the only piece of information you need to carry around with you in your head.

In this design, I think the Section object is supposed to "know" the courseId so that if you need to find something in the CourseCatalog, you can just ask the Section object, "What's the course id for the course you want details on? What's the ID of the course that is associated with this section?" and the Section object says, "Ok, this section is associated with the courseID BLAH-BLAH-BLAH."  And then the course catalog object takes that number and does findByCourseId(), returning the Course object that matches that courseId provided.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does! I mean in class the instructor is not really on us for looks of the program...which I honestly do not see why not. So that is why most of my code is not going to be the prettiest, I just follow his examples in class. As far as my registeredstudent method goes I know thats a mess..the student object is suppose to be passed through and if that student is over the limit then do not add him. Which I dont know if I did that right, because my array list does not like the fact im trying to add an object to it
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You're overcomplicating / overthinking this.  I think in this particular design, the courseId is used merely as a reference number, kind of like how you don't go walking around with all the details of your social security contributions and benefits. If you need to know what those details are, you go to the SS office and give your SSN, to somebody who can look up your complete records.  Your SSN is a reference number and that's the only piece of information you need to carry around with you in your head.

In this design, I think the Section object is supposed to "know" the courseId so that if you need to find something in the CourseCatalog, you can just ask the Section object, "What's the course id for the course you want details on? What's the ID of the course that is associated with this section?" and the Section object says, "Ok, this section is associated with the courseID BLAH-BLAH-BLAH."  And then the course catalog object takes that number and does findByCourseId(), returning the Course object that matches that courseId provided.

Soooo Would it be better if I just took away courseId from the constructor, and passed a course object instead?
 
Junilu Lacar
Sheriff
Posts: 11494
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:the student object is suppose to be passed through and if that student is over the limit then do not add him. Which I dont know if I did that right, because my array list does not like the fact im trying to add an object to it

It's not that your arraylist doesn't like it. Line 35 of your Student class doesn't make sense and it won't compile.

Your maxStudents variable is an integer. Your stud parameter is a Student object.  That's like asking the computer, "Is Cody greater than 5?" That's a totally nonsense question, right?
 
Junilu Lacar
Sheriff
Posts: 11494
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:Soooo Would it be better if I just took away courseId from the constructor, and passed a course object instead?

Would it be better for you to walk around with your complete Social Security records instead of memorizing your SSN?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does not make sense at all, but its the first thing that came into my head, trying to work on a million parts at once probably is not the best idea either. What do you mean the section object should know the courseId? Just the way im thinking in my head is asking the user for the course Id that way I would "know" the id.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me try another analogy. 

If you drive a car, do you drive around with a whole catalog of parts for your car? You don't, right?  If you need to buy a part for your car, you'll find out what the Part Number is, then go to the store, a guy looks up the part number in a Catalog, and then finds the part for you.

Likewise, this design doesn't have a Section object carrying around a whole Course object in it. In this design, it's not supposed to. All a Section object is supposed to "remember" or "know" is the ID of the course that it's associated with. That way, if you need to find the Course that a Section is associated with, you have to ask the Section object "What's the courseID that you're associated with?" When you get that bit of information, you go to the CourseCatalog, pass in the courseId to the findByCourseId() and then back the full Course object from the CourseCatalog.

Does that make sense?

This is what you already have:

In this code, you declared instance variables (line 3 and 4) so that a Section can "remember" what its maxStudents and sectionNumber is.  In the constructor, you pass in the courseId as the first parameter. However, there's nothing in the constructor that "tells" the Section to remember what the courseID is that it's associated with.

Calling the method getSectionNumber() (line 12) is like asking a Section object, "What's your section number?" and getting the response back, "Oh, I remember, it's BLAH-BLAH (whatever sectionNumber on line 4 is)"

I don't see any method in your code that would answer the question, "Hey Section, what is the course ID that you're associated with?"
I don't see any code in your Section constructor that tells it to remember the value of the courseId that was passed in on line 6.
I don't see any instance variable after line 4 that would allow a Section to "remember" what the courseID was that was passed to the constructor.

Does this make sense? Do you think you can make the changes needed to make your Section remember all that stuff and answer the question "What is the courseID that you're associated with?"


 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to get my section to "know" it would be like



I just got an email back from the instructor he said " The course id will be used to look up the
course in the catalog and assign the results to the course variable.

Now im even more confused....I wish this class was longer then 6 weeks. When he says look up the course in the catalog im thinking of somehow looping through the course list in the catalog to find my course Id
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to think of objects as people/beings that know certain information and can do things. Objects "know" things via their instance variables. Objects "do" things via their methods.  That's why instance variable names are usually nouns or noun phrases and method names verbs or verb phrases (convey a sense of action or doing something).  Even if something is not a living, sentient person/thing/being in the real world, the magic of object-oriented programming and OO thinking makes the abstractions that represent them so. Therefore, you can imagine a Section as something that "knows" things about itself and can "do" things with what it knows.


 
Junilu Lacar
Sheriff
Posts: 11494
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 to get my section to "know" it would be like



You got it.  Aligning your braces {} properly would make your code easier to read. And spelling out the full words, too. You can use the this keyword on the left side of assignments to refer to the instance variable.

this refers to the current Section object being created. In instance methods, this refers to the current Section object that the method is getting called on.
 
Junilu Lacar
Sheriff
Posts: 11494
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 just got an email back from the instructor he said " The course id will be used to look up the
course in the catalog and assign the results to the course variable.

Now im even more confused....

Again, go back to imagining these magical "things" that are alive and thinking in your program. Kind of like an animated movie where pots and brooms can talk and dance. In this program, it's Sections and CourseCatalogs that are animated.

So, when a Section object is created, it is told, "This the courseID that you're associated with" and then the Section object goes, "Ok, I'll just go ask the CourseCatalog to find that Course for me. Yoohoo, oh CourseCatalog, can you give the the Course object that's associated with the courseID BLAH-BLAH-BLAH? Please?" (section object calls CourseCatalog.findByCourseId() method, passing in the info it was given)

That business about "assign the results to the course variable" is like the CourseCatalog answering, "Here, this is the course that I found with the courseID you gave me." (returns a Course object). Section object then goes, "Ok, now I'm going to file that away so that I can remember later..." (you should know how a Section object "remembers" things by now).

I know it may seem silly, but that's the easiest way I know to visualize how all these things go down in the program.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your good. This is my 2nd programming class, so that is why it may take me 500000 times ti really understand. Sp your saying I need to make a call to my getCourseById method in my Catalog class passing it the course Id. Where would  I do that? This is my Catalog method.

 
Junilu Lacar
Sheriff
Posts: 11494
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:Sp your saying I need to make a call to my getCourseById method in my Catalog class passing it the course Id. Where would  I do that? This is my Catalog method.

It's a little kind of hard to say given the code you have shared. If you go back to that little story line I last gave you about a Section object being created and told "This is the courseID you're associated with, your section number, and the maximum number of students you can put on your roster", it kind of make sense for a Section object to be calling on a CourseCatalog right there in the constructor. After the constructor is done executing, the only information that the Section object will be able to "recall" is whatever it socked away into its instance variables from the constructor. So, if you have a Course course instance variable instead of a String courseId instance variable, the only time you have a chance to call a CourseCatalog and pass it the courseId is in the constructor, when that information is still available.  Are you following so far?

The complication is that I don't know how you would get a reference to a CourseCatalog object from the constructor. The Section constructor isn't passed a CourseCatalog catalog parameter so you can't get a reference that way. So, if you were to do this course lookup from the Section constructor, I would think you'd have to call something like CourseCatalog.getInstance() which is what's called a factory method because it's a class method that "manufactures" or creates instances of CourseCatalog.  Does your CourseCatalog have such a method?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well then that part in the story that goes, "Yoohoo, oh Catalog,...." translates to Catalog.getInstance() in Java. I already said where I think that should happen. It's your job to translate the rest of the narrative to Java.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would make a call to that Catalog method getCourseById in the constructor as well? The getCourseById already returns a course...so I want to call that method and return another Course?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or would it go into my course method like this?


 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If So then my getSectionByCourseId method would look like this instead Right? (trying to keep this thread bumped)

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't read the whole thread, but there is a problem with your code:

Do you see the problem?  This is a really good case for the rule "always use braces{} with if statements.".
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't disagree with advocating the use of braces, but I don't think it would have helped with this case. You'd just get

which is still broken.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it helped me more since I use K&R formatting.  It would be very hard to write the following by accident:
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not even see that. I do have a bad habit of not thinking about the braces when I do this...actually is why my last one I didn't work right until I actually stood back and noticed that. But I think I just got about everything I need done here. Worked on it all day yesterday so I think I'm good to go, but thank you knute and carey for all the help in the past assignments. Realized I never thanked y'all.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sometimes see people who say things like, “I'll sort out the indentation later.” You have here learnt an important lesson: that sort of thing doesn't work. If you get the {} and indentation right as you write the code for the first time, you will find it so much easier to understand what is going on and so much less easy to make that sort of mistake. You can get a lot of help from your text editor, and you write backwards, as in that← link.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!