views:

424

answers:

2

Hi

Following on from this Q of mine: http://stackoverflow.com/questions/754184/whats-the-best-way-to-make-this-java-program

I was recommended to store a list in Lecturer class and Course class. So I did, and it looks something like this:

public class Lecturer
{
    private String id;  
    private String name;  
    List<Course> courses = new ArrayList<Course>();  // a list to hold the courses


    public Lecturer(String idIn, String nameIn)     // arguments of the constructor
    {
        id = idIn;
        name = nameIn;

    }

}

Same thing for the Course class except it has Lecturer list. But what I dont get is what does placing a list there exactly do? cos I dont know where to put the methods of the ArrayList such as adding and removing lecturers from it?

Can someone explain the purpose of this?

I use another method which is basically placing the arraylists and its methods in two seperate classes for the lecturer and course, and then I simply add into the Course and Lecturer class as an attribute eg:

public class Lecturer
{
    private String id;  
    private String name;  
    private CourseList courses;  // COurseList is the class with the arraylist and methods


    public Lecturer(String idIn, String nameIn)     // arguments of the constructor
    {
        id = idIn;
        name = nameIn;
        courses = new CourseList();
    }

}

I hope i'm making sense, cos ive been stuck on one thing for the last 2 weeks which no seems to understand.

Thank you

A: 

I'd advise using Map<Lecturer, List<Course>>, as someone replied in the previous question, which means, "an association (Map) between different lecturers (Lecturer) to a list of the courses they teach (List<Course>), instead, which you would instantiate as new HashMap<Lecturer, List<Course>>. Otherwise, storing a list for each, you'll be duplicating functionality.

Once you declared the Courses c1, c2, ..., cn a Lecturer l teaches, you associate them in the Map m as m.put(l, c) where c is a List of the courses, declared as new LinkedList<Course>(), and added as c.add(c1); c.add(c2); ... c.add(cn);.

If I explain myself clearly.

You can read http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html and http://java.sun.com/docs/books/tutorial/collections/implementations/map.html for more help on using Maps.

To obtain the reverse association, you can easily use the following code:

Collection<Course> courses = m.values();
Map<Course, List<Lecturer>> reverseAssociation = new HashMap<Course, List<Lecturer>>;

    for (Course course : courses) {
        List<Lecturer> lecturersTeachingCourse = new LinkedList<Lecturer>();

        for (Lecturer lecturer : m.keySet()) {
         if (lecturer.teaches(course)) {
          lecturersTeachingCourse.add(lecturer);
         }
        }

        coursesTaught.put(course, lecturersTeachingCourse);
    }

Which, as long as Lecturer.teaches(Course) queries whether the lecturer teaches the passed course, sets reverseAssociation as the courses-lecturers association. (Naturally, you should encapsulate that code as a method in Lecturers).

Good luck JavaNoob! We were all there once!

Beau Martínez
So shouldnt I have a list of lecturers? cos i also need to be able to view the list of lecturers teaching a course.thanks your for ur reply
In principle you should avoid such circularity. I'll update my answer...
Beau Martínez
+1  A: 

With the first method, you need to expose methods that allow client code to add stuff to those lists. So, you could have:

public class Lecturer
{
    List<Course> courses = new ArrayList<Course>();  // a list to hold the courses

    public Lecturer(String idIn, String nameIn)
    {
        /* do stuff */
    }

    public void addCourse(Course newCourse)
    {
        this.courses.add(newCourse);
    }
}

You can do something similar for the Course class. Once you have those set up, you can do something like this:

public static void main(String[] args)
{
    Lecturer bob = new Lecturer(1, "Bob Smith");
    Course math = new Course("Math 101");

    // wire them together such that Bob teaches Math 101
    bob.addCourse(math);
    math.addLecturer(bob);
}

I think that solves what you're asking, but having this 2-way, circular relationship is sometimes a sign of a bad design. Only you know what you're real assignment is, though, so I hope this helps!

Outlaw Programmer
Hi thank you for your reply, I have already something very similar but it doesnt work, i posted about this but no one seemed to understand what I was saying, here it is: http://stackoverflow.com/questions/765937/how-do-allocate-something-to-an-object-in-a-arraylist