5

I'm currently having some trouble implementing ternary associations in Code. I get the binary ones, but I am unsure about ternary Associations.

this is typical scenario in a university.

Lecturer can teach a one subject to one or more students
student can teach one subject from only one Lecturer
Lecturer can teach one student to only one subject

There exists a ternary association between these three classes.

Relation among these three classes shown in below UML class diagram and also multiplicities are there

enter image description here

I've read on different sources regarding this all across the internet and coudn't find a solution

How do I implement the association between these three classes ? or, In general What are the possible ways to implement association between classes (in java) ?

Susantha7
  • 898
  • 1
  • 20
  • 38

3 Answers3

3

As always, it depends.

The usual way to implement association classes is by using associative arrays. In your example you'd have (eventually) a combination of Lecturer/Subject to access a list of Students. If your requirements are different you could for example return a list of Subject/Students when supplying a Teacher.

When using a database you will have the primary keys of Lecturer/Subject/Student in a table Teaching. This allows for individual selects like for the ones mentioned above.

Some pseudo code out of my hat:

class Teaching {
  private Hash lectRef; // assoc. array 

  public void addTeaching(Lecturer lect, Student stud, Subject subj) {
    if lectRef[lect.hash] == None { lectRef[lect.hash] = []; }  
    // if none, default an empty array here
    // lect.hash is a unique hash code for the Lecturer object

    lectRef[lect.hash].append((stud, subj); 
    // tuple of student/subject referenced

    // if you need other fast results (e.g. subjects per student or the like) you need to hash them here too
  }

  public [(Stud, Subj)] studSubj (Lecturer lect) {
    return lectRef[lect.hash];  
    // returns the array of student/subject tuples
  }

  // add other result operations like subjects per student as needed
}
qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • appreciate if you can provide sample code (dummy code ) for Lecture teach subject to student(s). teach means assigned subject class to a class variable `learnedSubjectList` in student class – Susantha7 Oct 16 '18 at 03:14
2

according to Amadan i was prepare a answer

since i'm new to java , syntax can be incorrect if this solution is wrong please correct me

//Lecturer class
class Lecturer{
    private List<Klass> klassList; 

    public List<Klass> getKlasses(Klass klassList){
        retrun klassList;
    }

    public List<Klass> addKlasses(Klass klassList){
        this.klassList = klassList;
    }

    teachStudents(){
        klassList = getKlasses()

        for (int i = 0; i < klassList.size(); i++) {
            (klassList.get(i)).teachSubjectToStudent();
        }
    }
}

//Student class
class Student{
    private Klass klass;
    private List<Subject> learnedSubjectList; 

    public void learn(Subject subject){
        learnedSubjectList.add(subject);
    }
}

//Subject class
class Subject{
    private Klass klass;
}

//Klass class
class Klass{
    private Lecturer lecturer;
    private Student student;
    private Subject subject;

    Klass(Lecturer lecturer,Student student,Subject subject){
        private this.lecturer = lecturer;
        private this.student  = student;
        private this.subject  = subject;
    }

    //implimentation of how subject was teach to a student
    public void teachSubjectToStudent(){
        this.student.learn(subject)
    }
}

//this class is use for code execution
public class TestA {
    public static void main(String[] args) {
        Lecturer lecturer = new Lecturer();
        Subject subject   = new Subject();

        Student student1   = new Student();
        Student student2   = new Student();
        Student student3   = new Student();

        Klass klass1 = new Klass(lecturer,student1,subject);
        Klass klass2 = new Klass(lecturer,student2,subject);
        Klass klass3 = new Klass(lecturer,student3,subject);

        List<Klass> list = new ArrayList<Klass>();
        list.add(klass1);
        list.add(klass2);
        list.add(klass3);

        //create association link between lecturer class and klass class list
        lecturer.addKlasses(list);
    }
}
Doktor OSwaldo
  • 5,732
  • 20
  • 41
Susantha7
  • 898
  • 1
  • 20
  • 38
0

Typically, you'd model the association itself as an entity in its own right (can't call its class Class, as that's already taken by java.lang.Class; usually people name such a class Klass, as it's obvious what it refers to; you can also use your Teaching, if you wish). Then you just have binary associations between Klass and Teacher, Klass and Subject, Klass and Student. Teacher and Student, in this view, are not associated at all, except indirectly through their involvement with Klass (similarly with other pairs).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • appreciate if you can provide sample code (dummy code ) specially for `Lecture` class `teach()` method and also it's delegation code located in `klass` class – Susantha7 Oct 15 '18 at 09:18
  • Nope, we never talked about `Lecture` class (which to me would imply one specific period of time when all the entities involved in one `Klass` come together, but that's just me), and I have no idea what `teach` method is supposed to do (I don't think I can make a convincing artificial intelligence that students would listen to in five minutes on Stack Overflow :P ). I was talking in general on how non-binary relationships are handled in real life; but there's not enough specifics for me in the question to provide any code. – Amadan Oct 15 '18 at 09:24
  • Lecture is the one who doing teaching.therefore `Lecture` class must have `Teaching()` method am i correct – Susantha7 Oct 15 '18 at 09:49
  • I thought that was `Lecturer`. – Amadan Oct 15 '18 at 09:51
  • sorry.. Lecturer is the one who doing teaching.therefore Lecturer class must have Teaching() method am i correct – Susantha7 Oct 15 '18 at 09:53
  • Not really, but mostly because of conventions. First, methods in Java are in snakeCase. Secondly, property accessors by convention start with `set` and `get` (or `is`), and would be named in plural if the result can be a list. Since one `Lecturer` can teach several different instances of `Klass`, I’d call it `getKlasses()` (I’m not sold on “teachings”, sorry :p ). If you ignore conventions, `Teaching()` is fine, but then so is `Yoda__provides666YOGHURT____()`. – Amadan Oct 15 '18 at 10:00