0% found this document useful (0 votes)
23 views6 pages

OOP Lab 06 20032024 090029am 1 09102024 122456pm 15102024 100637am

The document outlines a lab exercise for a Java programming course focused on association and aggregation concepts. It includes the implementation of classes for Student, Course, and Address, along with exercises for creating a Library Management System, Music Album Management System, and a Health Monitoring System. Additionally, it emphasizes the use of Java's toString() method and the modification of classes to demonstrate these programming principles.

Uploaded by

anasjadoon31
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)
23 views6 pages

OOP Lab 06 20032024 090029am 1 09102024 122456pm 15102024 100637am

The document outlines a lab exercise for a Java programming course focused on association and aggregation concepts. It includes the implementation of classes for Student, Course, and Address, along with exercises for creating a Library Management System, Music Album Management System, and a Health Monitoring System. Additionally, it emphasizes the use of Java's toString() method and the modification of classes to demonstrate these programming principles.

Uploaded by

anasjadoon31
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/ 6

CSL-210: Object-Oriented

Programming Lab
BS(IT)- Semester 02
Engr. Saba

Lab06: Association and Aggregation

Designing and implementing Java programs that deal with:


1. Association and
2. Aggregation

1. Association
Consider an association of student register for a course. Create the student class as follows:
/*
* Student Class
*/
public class Student {
private String name;
private String matrix;
private String major;
private Vector courseList; //declare vector of course
Student(String n,String m,String maj){
name=n;
matrix=m;
major=maj;
courseList = new Vector();//initialize the vector
}
public String getName()
{ return name; }
public void regCourse(Course course) //showing association
{courseList.addElement(course); }
public void printInfo()
{
System.out.println("\nSTUDENT NAME :"+ name);
System.out.println("NUMBER OF COURSE(s) TAKEN :" +
courseList.size());
System.out.println("LIST OF COURSE(s) TAKEN :");
for(int i=0;i<courseList.size();i++){
Course c=(Course)courseList.elementAt(i);
System.out.println(c.getTitle());
}
}

} // end of class Student

Create the course class as follows:


30/86

/*
* Course Class
*/
public class Course {
private String courseTitle;
private String courseCode;

Course(String title,String code)


{ courseTitle = title;
courseCode = code;
}
public String getTitle()
{
return courseTitle;
}
}//end of Cource class

Create the MyAppliction class according to the following code snippet.

/*
* MyApplcation Class
*/
public class MyApplication {
public static void main(String [] args){
Course cs1 = new Course("OOP","CSC-210");
Course cs2 = new Course("DATA STRUCTURE","CSC-320");
Student s1 = new Student("Tariq","DC0021","Software
Engineering");
Student s2 = new Student("Sarmad","DC0022","Multimedia");
Student s3 = new Student("Wasim","DC0023","Information Systems");
s1.regCourse(cs1);
s2.regCourse(cs1);
s2.regCourse(cs2);
s3.regCourse(cs1);

s1.printInfo();
s2.printInfo();
s3.printInfo();
}
}

You have noticed:


How the above code adds course objects into student class?
Now
Use ArrayList instead of Vector to add and retrieve course objects into Student class.

2. Aggregation
Consider an example of aggregation of address. Create the address class as follows:
31/86

/*
* Addresss class
*/
public class Address {
private String streetAddress,
city,
state;
private long zipCode;
public Address (String street, String town, String st,long zip)
{ streetAddress = street;
city = town;
state = st;
zipCode = zip;
}

public String toString()


{
String result;
result = streetAddress + "\n";
result += city + ", " + state + " " + zipCode;
return result;
}
}

Create a Student class that have the fields homeAddress and schoolAddress of type class Address.
/*
* Student2 class
*/
public class Student2
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
public Student2 (String first, String last, Address home, Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress + "\n";
return result;
}
}

toString()
Java's Object class provides a method called toString() that returns the string representation of the object on
which it is called. You can create your own toString() method by overriding toString() method to printout
your own objects as it is done above.
Modify the MyAppliction class according to the following code snippet and rename it as MyApplication2.
Exercise 1(a) (Department)

Consider a department scenario. Create a java program based on the given UML diagram of department
based scenario and implement the relation between the classes as shown in the diagram

Exercise 1(b)

Create Driver class named as Association_aggregation_1. Create proper Objects of all classes as
follows
 2 Objects of Course class
 3 Objects of Faculty class
 3 Objects of Department Class
And properly display all the information
Exercise 2 (Library Management System)
You're tasked with developing a Library Management System in Java. This system should facilitate the
management of libraries and their multiple branches. Each library can contain various books, and each
branch can maintain its own collection of books. So, Create Java classes to represent the entities
involved: Library, LibraryBranch, and Book. Implement methods within these classes to enable
functionality such as adding books to a library, removing books, displaying all books in a library branch,
etc. Ensure that the relationships between these entities are properly modeled without explicitly
mentioning the technical terms for those relationships.

Exercise 3 (Music Album Management System)

You're tasked with developing a music management system in Java. Here albums can have multiple
songs, but songs can exist independently from any album and may appear in multiple albums. Design
Java classes to represent Music Album and Song. Implement methods to:
 Add a song to an album.
 Remove a song from an album.
 Display all songs in an album.
 Display all albums a song is included in. Make sure that removing an album does not delete the
associated songs.
Sample Representation:

Exercise 4 (Person & Heartbeat Monitoring System)

You're tasked with creating a health monitoring system for tracking people and their heartbeats. Each
person has a unique heartbeat, and the heartbeat cannot exist without the person. Design Java classes for
Person and Heartbeat. Implement methods to:
 Monitor the heartbeat of a person.
 Display the heartbeat status of a person. Ensure that when a person object is removed, the associated
heartbeat data is also destroyed.
Also create a proper UML diagram for your implemented solution.

You might also like