Skip to main content

Association in Java Programming

What is Association in java


  • Relationship between one or more classes
  • Aggregation and composition is a special type of association


For example, Student Belongs to a specific Faculty and One faculty has many students

Code Example:



class Student {
  String name;
  Student () {name=null}
  Student (String n) {name=n; }
  void setName(String n) {name=n;}
  String getName() { return name;}
}
class Faculty{
  Student student[];
  int size;
  Faculty (int s){size=0;
           student=new Student[s];}
void belongsTo(Student p){
    student[size]=p;
    size++;
}
void print(){
   for (int i=0;i<size;i++)
      System.out.println(student[i].getName());
}
}
class Test{
public static void main (String a[]){
    Faculty FIT= new Faculty(5);
    Faculty FMS= new Faculty(5);
    
    Student s1=new Student("Hamza");
    FMS.belongsTo(s1);
    
    Student s2=new Student("Kamil");
    FIT.belongsTo(s2);
    
    Student s3=new Student("Shoaib");
    FMS.belongsTo(s3);
    
    System.out.println("FIT students");
    FIT.print();
    System.out.println("FMS students");
    FMS.print();
    }
}






Practice 2

  • Student register for one or more courses
  • One student can register max 5 courses
  • Each course may have a maximum of 50 registered students


class Student {
String name;
Student () {name=null}
Student (String n) {name=n; }
void setName(String n) {name=n;}
String getName() { return name;}
}
class Course {
String title;
Course  () {title=null}
Course  (String n) {title=n; }
void setTitle(String n) {title=n;}
String getTitle() { return title;}
}

class Registration{
Student s;
Course  c;
void register(Student s,Course c ) {
            this.s=s;
            this.c=c;
}
void print(){
 System.out.print(s.getName() " Register ");
 System.out.println(c.getTitle());
}
}

class Test{
public static void main (String a[]){
Registration r[]=new Registration[10];
Student s1= new Student("Jhon");
Student s2= new Student("Alex");
Student s3= new Student("Jawad");
Course  c1 = new Course ("DSA");
Course  c2 = new Course ("Algo");
r[0new Registration();
r[0].register(s1,c1);
r[1new Registration();
r[1].register(s2,c1);
r[2new Registration();
r[2].register(s3,c2);
r[3new Registration();
r[3].register(s1,c2);
for (int i=0;i<4;i++)
   r[i].print();

}
}

Comments

  1. How to get to Harrah's in Atlantic City - JTM Hub
    The quickest way 목포 출장마사지 to 충청북도 출장안마 get to Harrah's in Atlantic City costs 평택 출장마사지 only $1, and the quickest way takes 군산 출장샵 just 익산 출장마사지 15 mins.

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Basics Quick Review with examples

Java Programming Language High Level Strongly Typed means no automatic type conversions in java like C++ Case Sensitive Successor of c++ Pure Object Oriented Language  Hello World Problem in java class Test{ public static void main(String a[]) { System.out.println("Hello World");  } } More than one Classes class Test1{ void print () { System.out.println("Test class"); } } class Test{ public static void main(String a[]) { Test1 t=new Test1(); t.print();  } } Rule of Thumb: Don't write extra functions in driver class  Extra function and data in driver class  class Test{ int sq(int p){    return p*p; } public static void main(String a[]) {     Test t = new Test(); System.out.println(t.sq(4));  } } .................................. call without object instantiation .................................. class Test{ static int sq(int p){    return p*p; } public static voi...

Life Time of Objects in Java with code Examples

Lifetime Concept in Java Programming Time between allocation and de-allocation of memory Life time depends on block i.e { }  life time starts when  declaration statement elaborated and expires at end of block   There is no delete keyword in java  Garbage collector is responsible for all deletes Java is free from dangling reference and garbage problem Why java does not support delete keyword? Java does not support delete keyword because there is a garbage collector in java This automatically runs and collects dangling or un-referenced objects.That is why there is no delete keyword in java like C++. Garbage collector can be explicitly like that System.gc();  When a life of object ends garbage collector runs automatically Example 1 { int a=5;// life Time start here a++; } //life time expire Example 2   Let Student is a class Student s;   // creating a reference of Student class    {Student t; //...

Generalization in java with examples

      Generalization basic points  Type/Subtype Relationship  "IS A " Relationship  One class has many subclasses   General class can have many Special classes as  subclasses  General names can be used to Address special names  Top to bottom hierarchy goes towards specialization  Bottom to top Hierarchy goes towards generalization e.g         Soap     Bath Soap and Washing Soap is a subclass of Soap e.g COldDrink   Pepsi      Team -Cold Drink is a general class -Pepsi and team are special classes e.g    Person Teacher       Student e.g            Car     Sedan           Hatchback Honda   Suzuki     Suzuki   Toyota civic        Liana      Alto      vitz ..........................................