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

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 ....................................................\ Some Examples from Nature e.g.               Animal            Mammals            Reptiles    Human           Cow     Snake         Croc Student Teacher             Cobra Python     e.g                     Fruits      Mango      

Composition in Java with code examples

Composition  "Have a " Relationship  OR  "Must have" relationship                    between two or more classes. Container class contains component's class object For example a  Car is composed of engine and body. Where Car is a container class and both Engine and Body are components class.  Life Time Issue in Composition Life time of component objects depends on container class If Container object destroyed, component objects will be also destroyed Car Example  There is a composition relationship between car's body and engine. Car must have Engine and body If Car destroyed, both Engine and Body will be destroyed class  Engine {   Engine  () {    System.out.println ( "Engine created" ) ; }   void  start () {    System.out.println ( "Engine Start" ) ;   } } class  Body {   Body  () { System.out.println ( "Body created" ) ; }   void  type () {    System.out.println ( "Sa

Aggregation in java with example codes

What is aggregation?   "Have a" or "May have" relationship  Aggregation is special type of association  Life time of Component object is independent from its  container object E.g A school has one student class  Student  { String name; Student  () { name= null ;  } Student  ( String n ) { name=n;  } void  setName ( String n ) { name=n; } String getName () {  return  name; } } class  School { Student  st; School  (  ) {  st= null ; } School  ( Student s ){ st=s; } void  studentName (){    System.out.println ( st.getName ()) ; } } class  Post { public static  void  main  ( String a []){ Student st= new  Student ( "Imran" ) ; School s=  new  School ( st ) ; s.studentName () ; } } Output Hello Problem : A Car must has Engine and body.           A Car may or may not has Ac Adding Ac as an Aggregate object in car class  Engine { void  start () { System.out.println ( &