Friday, June 10, 2022
HomeWordPress DevelopmentDistinction Between Aggregation and Composition in Java

Distinction Between Aggregation and Composition in Java


  

import java.io.*;

import java.util.*;

  

class Scholar {

  

    

    String identify;

    int id;

    String dept;

  

    

    Scholar(String identify, int id, String dept)

    {

  

        

        this.identify = identify;

        this.id = id;

        this.dept = dept;

    }

}

  

class Division {

    

    String identify;

    non-public Listing<Scholar> college students;

    Division(String identify, Listing<Scholar> college students)

    {

        

        this.identify = identify;

        this.college students = college students;

    }

  

    

    public Listing<Scholar> getStudents()

    {

        

        

        return college students;

    }

}

  

class Institute {

  

    

    String instituteName;

    non-public Listing<Division> departments;

  

    

    Institute(String instituteName,Listing<Division> departments)

    {

        

        this.instituteName = instituteName;

        this.departments = departments;

    }

  

    

    

    

    public int getTotalStudentsInInstitute()

    {

        int noOfStudents = 0;

        Listing<Scholar> college students;

  

        for (Division dept : departments) {

            college students = dept.getStudents();

  

            for (Scholar s : college students) {

                noOfStudents++;

            }

        }

  

        return noOfStudents;

    }

}

  

class GFG {

  

    

    public static void fundamental(String[] args)

    {

        

        Scholar s1 = new Scholar("Mia", 1, "CSE");

        Scholar s2 = new Scholar("Priya", 2, "CSE");

        Scholar s3 = new Scholar("John", 1, "EE");

        Scholar s4 = new Scholar("Rahul", 2, "EE");

  

        

        Listing<Scholar> cse_students = new ArrayList<Scholar>();

  

        

        cse_students.add(s1);

        cse_students.add(s2);

  

        

        Listing<Scholar> ee_students

            = new ArrayList<Scholar>();

  

        

        ee_students.add(s3);

        ee_students.add(s4);

  

        

        

        Division CSE = new Division("CSE", cse_students);

        Division EE = new Division("EE", ee_students);

  

        Listing<Division> departments = new ArrayList<Division>();

        departments.add(CSE);

        departments.add(EE);

  

        

        Institute institute = new Institute("BITS", departments);

  

        

        System.out.print("Complete college students in institute: ");

  

        

        

        System.out.print(institute.getTotalStudentsInInstitute());

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments