0% found this document useful (0 votes)
52 views5 pages

Lab Assignment - 2 Statement

The document describes an assignment to write an object-oriented program in Java to model a university with departments and students. It provides requirements for classes to represent the University, Department, and Student entities and their relationships. It also provides a sample solution code that defines the classes and tests getting the total number of students by iterating through departments and students.

Uploaded by

abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Lab Assignment - 2 Statement

The document describes an assignment to write an object-oriented program in Java to model a university with departments and students. It provides requirements for classes to represent the University, Department, and Student entities and their relationships. It also provides a sample solution code that defines the classes and tests getting the total number of students by iterating through departments and students.

Uploaded by

abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMSATS University Islamabad, Lahore Campus

Department of Computer Science

Lab Assignment 2 – Semester Fall 2022


Course Title: Object Oriented Programming Course Code: CSC241 Credit Hours: 4(3,1)
Course Instructor/s: Mr. Imran Latif Program Name: BCE
Semester: 3rd Section: A&B Batch FA21
Total Marks: 10 Obtained Marks: 60 Minutes
Due Date: November 7
Student’s Name: ABDULLAH TARIQ Reg. No. FA20-BCE-029
Important Instruction:

 Student is himself/herself responsible for successful submission of assignment on Microsoft Teams.


 Your submission must include the following in a single pdf file.
1. Code of all classes
2. Snapshot of the output of submitted code.
 Copied assignment will get zero credit.
 Deadline: November 7, 2022 till 11:30 PM

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;

public class University {


public String name;
public ArrayList<Department> departments;

public University(String name,


ArrayList<Department> departments) {
this.name = name;
this.departments = departments;
}
public int totalnoOfStudents(){
int noOfStudents = 0;
ArrayList<Student> students;

for (Department dept : departments) {


students = dept.getStudents();
for (Student s : students) {
noOfStudents++;

}return noOfStudents;

}
}

import java.util.ArrayList;
public class Department {
public String name;
public ArrayList<Student>students;

public Department(String name,


ArrayList<Student> students) {
this.name = name;
this.students = students;
}

public ArrayList<Student> getStudents() {


return students;
}
}

public class Student {


public int id;
public String name;
public String department;

public Student(int id, String name, String


department) {
this.id = id;
this.name = name;
this.department = department;
}
}

import java.util.ArrayList;

public class StudentTest {


public static void main(String[] args) {
Student s1 = new Student(1, "ahmed", "CS");
Student s2 = new Student(2, "shan", "CS");
Student s3 = new Student(3, "laiba", "EE");
Student s4 = new Student(4, "ali", "EE");

ArrayList<Student> cs=new ArrayList<Student>();


cs.add(s1);
cs.add(s2);
ArrayList<Student> ee=new ArrayList<Student>();
ee.add(s3);
ee.add(s4);

Department cs1=new Department("sc",cs);


Department ee1=new Department("sc",ee);

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:

You might also like