This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Class association problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class association problem" Watch "Class association problem" New topic
Author

Class association problem

Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Howdy!

Thank you for volunteering your time!

I'm working on uni assignment and I'm kindna struck...it's a simple console student record system. I've created three classes:

- A student class
student name
student id
an arraylist for storing course objects
a constructor... (also Instantiating three course objects through course constructor and add to an arrayList that store course objects)
other getters and setter methods here...
an arraylist to store course objects

- A Course class

course name
grade
course fees
enrolment status
a constructor..
other getters and setter methods here...


- A driver class to kickstart the application.
some menu driven methods...
some loops for program flow..
input validations...
an ArrayList to store student objects


The student class has got some basic instance variables, constructor and other getter and setter methods, and the same for Course class.

In the driver class, I've got menu-driven program flow. In driver class, I've instantiate an 2 student objects and store them into an ArrayList. Now, I'm not too sure how should I actually get a student object to hold or link several Course objects to it. I'm assuming instantiated course objects through the student class constructor, would be somehow linked to student object. No? how do I access the course objects created through the student constructor? I had added the course objects to an arraylist within the student class. Please advise.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
It is difficult to give really good advice without knowing more about your program; I am going to make some guesses and go from there.

Since it is quite reasonable that you might have a student who was not yet taking (or registered for) any courses, I wouldn't expect the courses to be available when you construct a Student object. I would rather expect that either a course had a list of students, or that each student has a list of courses, or both, and that you add one to the other as the program progresses.

So if your menus do something like "pick a student", then "pick a course to add to this student", then the driver class should, at that point, have a reference to one student and one course. I would expect the code, at that point, to call something like currentStudent.add(currentCourse);, or currentCourse.add(currentStudent);, or (again) both of those.

Is that helpful?

rc
Randy Smith
Ranch Hand

Joined: Mar 27, 2011
Posts: 44
Ralph Cook wrote:It is difficult to give really good advice without knowing more about your program; I am going to make some guesses and go from there.

Since it is quite reasonable that you might have a student who was not yet taking (or registered for) any courses, I wouldn't expect the courses to be available when you construct a Student object. I would rather expect that either a course had a list of students, or that each student has a list of courses, or both, and that you add one to the other as the program progresses.

So if your menus do something like "pick a student", then "pick a course to add to this student", then the driver class should, at that point, have a reference to one student and one course. I would expect the code, at that point, to call something like currentStudent.add(currentCourse);, or currentCourse.add(currentStudent);, or (again) both of those.

Is that helpful?

rc


Hi Ralph,

Thank you for responding. the student has a one to many course relationship. when I create a new course object, what sort of data structure is recommended for linking the reference of the new course object with the student? I have created an arrayList of all the courses that are available to a student when an student object is created. I've tried to using a getter method to return the arraylist but I'm not quite there yet.




ArraylistofStudentObjects(student1(arrayList of course objects).student2...(arrayList of course objects))

I hope you get the picture.

Problem is how should I access the student's arraylist of courses that is created?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
One of the (private) variables in the student class should be another collection of courses. Which collection you use depends on things in your application; ArrayList is all right, but it can have duplicates. If your students are allowed to have a course in their list more than once, this is fine.

Below I've used a TreeSet, which doesn't allow duplicates.

It seems reasonable to me that you would want methods for adding a course, removing a course, and returning the collection of courses for the student being represented.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class association problem
 
Similar Threads
Array Problem
Hashing Index
Arrays: Calling input from other methods and classes
Arraylist and for enchanced loop question
help in assignment please! =)