importjava.io.*;importjava.util.*;// student class classStudent{Stringname;intid;Stringdept;Student(Stringname,intid,Stringdept){this.name=name;this.id=id;this.dept=dept;}}/* Department class contains list of student
Objects. It is associated with student
class through its Object(s). */classDepartment{Stringname;privateList<Student>students;Department(Stringname,List<Student>students){this.name=name;this.students=students;}publicList<Student>getStudents(){returnstudents;}}/* Institute class contains list of Department
Objects. It is asoociated with Department
class through its Object(s).*/classInstitute{StringinstituteName;privateList<Department>departments;Institute(StringinstituteName,List<Department>departments){this.instituteName=instituteName;this.departments=departments;}// count total students of all departments // in a given institute publicintgetTotalStudentsInInstitute(){intnoOfStudents=0;List<Student>students;for(Departmentdept:departments){students=dept.getStudents();for(Students:students){noOfStudents++;}}returnnoOfStudents;}}// main method classAgg{publicstaticvoidmain(String[]args){Students1=newStudent("Mia",1,"CSE");Students2=newStudent("Priya",2,"CSE");Students3=newStudent("John",1,"EE");Students4=newStudent("Rahul",2,"EE");// making a List of // CSE Students. List<Student>cse_students=newArrayList<Student>();cse_students.add(s1);cse_students.add(s2);// making a List of // EE Students List<Student>ee_students=newArrayList<Student>();ee_students.add(s3);ee_students.add(s4);DepartmentCSE=newDepartment("CSE",cse_students);DepartmentEE=newDepartment("EE",ee_students);List<Department>departments=newArrayList<Department>();departments.add(CSE);departments.add(EE);// creating an instance of Institute. Instituteinstitute=newInstitute("BITS",departments);System.out.print("Total students in institute: ");System.out.print(institute.getTotalStudentsInInstitute());}}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)