Lab Assignment - 2 Statement
Lab Assignment - 2 Statement
Question 1:
Recall the concept of Composition and write down the code according to requirements.
Learning Outcome: PLO5→CLO4 (P2, P3)
University
Department
Name Student
Departments
Name
Name
Students
ID
Department
Write a program for the COMSATS university in which different departments exist and in each
department number of students studies e.g., CS, EE, Physics. Etc.
In this program there are three entities University, Department and Student and fourth one is the
test class. In University class there are two members Name and departments field that has a
reference to Object or more than one Object of the Department class. That means COMSATS
University class is associated with Department class through its Object(s). And Department class
has also a reference to Object or Objects of the Student class means it is associated with the
Student class through its Object(s). In the department class reference to student class is private.
In the Student class there are three fields Name, Id and department. Each class has its own
constructors.
In the COMSATS University class, a method totalnoOfStudents exist which return the total
number of students currently enrolled in the university.
In the test class, add students only for the two departments EE and CS and call the
totalnoOfStudents method to print the total number of students enrolled in these two
departments.
Hint: In the test class create an ArrayList of Student and Department classes and add data
through these classes.
SOLUTION:
import java.util.ArrayList;
}return noOfStudents;
}
}
import java.util.ArrayList;
public class Department {
public String name;
public ArrayList<Student>students;
import java.util.ArrayList;
ArrayList<Department>departments=new
ArrayList<Department>();
departments.add(cs1);
departments.add(ee1);
University uni=new
University("Comsats",departments);
System.out.println("total students are "+
uni.totalnoOfStudents());
}}
OUTPUT: