|
| 1 | +import java.util.ArrayList; |
| 2 | + class University { |
| 3 | + |
| 4 | + String uname; |
| 5 | + ArrayList<Students> studentsList = new ArrayList<Students>(); |
| 6 | + ArrayList<Departments> deptList = new ArrayList<Departments>(); |
| 7 | + |
| 8 | + // one argunment Constructer when we pass the name of student in return the object |
| 9 | + University(String uni, String[] snames, String[] dnames) { |
| 10 | + uname=uni; |
| 11 | +// to find out number of names which are pass in this object we write for loop |
| 12 | + for (int i = 0; i < snames.length; i++) { |
| 13 | + studentsList.add(new Students(snames[i])); |
| 14 | + } |
| 15 | + for (int j = 0; j < dnames.length; j++) { |
| 16 | + deptList.add(new Departments(dnames[j])); |
| 17 | + } |
| 18 | + } // end of constructer |
| 19 | + |
| 20 | + // students class inside university class |
| 21 | + class Students { |
| 22 | + String snames; |
| 23 | + Departments dept; |
| 24 | + |
| 25 | + Students(String snames) { |
| 26 | + this.snames = snames; |
| 27 | + } |
| 28 | + |
| 29 | + void display() { |
| 30 | + System.out.print(" name of the student is : " + snames); |
| 31 | + |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + class Departments { |
| 36 | + String dnames; |
| 37 | + ArrayList<Students>deptstudents = new ArrayList<Students>(); |
| 38 | + |
| 39 | + Departments(String dnames) { |
| 40 | + this.dnames = dnames; |
| 41 | + } |
| 42 | + |
| 43 | + // display all the students name which are study in that department |
| 44 | + public void displayStudents() { |
| 45 | + for (int i = 0; i < deptstudents.size(); i++) { |
| 46 | + System.out.println(deptstudents.get(i).snames); |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + } // end of departments class |
| 51 | + |
| 52 | + Departments getDepartments(String name) { |
| 53 | + for (int i = 0; i < deptList.size(); i++) { |
| 54 | + if (deptList.get(i).dnames.equals(name)) |
| 55 | + return deptList.get(i); |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + Students getstudents(String name) { |
| 61 | + for (int j = 0; j < studentsList.size(); j++) { |
| 62 | + if (studentsList.get(j).snames.equals(name)) |
| 63 | + return studentsList.get(j); |
| 64 | + } |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + public void enrollIn(Departments d, Students s) { |
| 69 | + s.dept = d; |
| 70 | + d.deptstudents.add(s); |
| 71 | + } |
| 72 | + |
| 73 | + public void displayStudentsInDept(Departments d) { |
| 74 | + d.displayStudents(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + // university class end here |
| 79 | + public class Charyo{ |
| 80 | + public static void main(String[] args) { |
| 81 | + String [] snames={ "Ali","Mohsin","Ahsan","Muhsin","Zakir"}; |
| 82 | + String [] dept= {"SW","CS"}; |
| 83 | + University muet=new University("Mehran UET ",snames,dept); |
| 84 | + muet.enrollIn(muet.getDepartments("SW"),muet.getstudents("Ali") ); |
| 85 | + muet.displayStudentsInDept(muet.getDepartments("SW")); |
| 86 | + } |
| 87 | + } |
0 commit comments