0% found this document useful (0 votes)
20 views

Lab 04 - Composition

This document outlines 3 tasks related to object-oriented programming concepts like classes, attributes, constructors, getters and setters. Task 1 involves creating Address, Employee classes with private attributes and getter/setter methods. EmployeeTest instantiates Employee objects and initializes/prints attributes. Task 2 creates Date and Employee classes with private attributes initialized in constructors. EmployeeTest instantiates objects and prints attributes. Task 3 defines classes like Course, PhoneNumber, Address, Student with appropriate data types and relationships. StudentTest creates Student objects, initializing some attributes via constructor and others via setters before printing. It also demonstrates how changing an attribute of a shared Address object affects multiple Student objects.

Uploaded by

shamoil.aslam6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 04 - Composition

This document outlines 3 tasks related to object-oriented programming concepts like classes, attributes, constructors, getters and setters. Task 1 involves creating Address, Employee classes with private attributes and getter/setter methods. EmployeeTest instantiates Employee objects and initializes/prints attributes. Task 2 creates Date and Employee classes with private attributes initialized in constructors. EmployeeTest instantiates objects and prints attributes. Task 3 defines classes like Course, PhoneNumber, Address, Student with appropriate data types and relationships. StudentTest creates Student objects, initializing some attributes via constructor and others via setters before printing. It also demonstrates how changing an attribute of a shared Address object affects multiple Student objects.

Uploaded by

shamoil.aslam6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Course: Object Oriented Programming

Lab 04
Composition

Task 1:
Make a class Address that have three attributes, streetNumber, city and
country. All attributes are private and will be exposed using getter and
setter method.
Make an Employee class, which have employeeID, employeeName and
emplyeeAddress. All attributes are private. Attributes employeeAddress
should be of type Address. In this class, define a constructor, which
takes two parameters i.e. employeeID and emplyeeAddress.
Make another class EmployeeTest, in main method, instantiate an
employee object named employee1. Initialize id and address attribute
using constructor. The attribute name should be initialized using setter
method. User should give value of name attribute. Also, print values of
all attributes using getter.
Make another employee object named employee2. Both employees share
same address. Print the id and address for employee2.
Expected Output:
Task 2:
Make a class Date with three attributes, day, month and year. All
attribute are private. In constructor, you have to validate day. If day is
out of range, you have to print a message, “Invalid Date”. Suppose all
months have 30 days.
Make a class Employee that has four instance variable firstName,
lastName, birthdate and hiringDate. firstNmae and lastName are
reference to String object while birthdate and hiringDate are references
to Date object. All instance variable are private. Employee class has a
fully parameterized constructor.
Make another class EmployeeTest. In main method, instantiate two
Date object to represent birthdate and hiringDate of employee. Now
instantiate one object for Employee named employee1 and initialize
firstName and lastName using constructor. Print values of all attributes
using getter method.
Expected Output:

Task 3:
For each class/attributes described below, choose appropriate data type.
All attributes of each class should be private and exposed via get/set
methods. Also define at least one constructor shall take and initialize 2-3
attributes of the object.

1. Define a class Course with courseCode and courseTitle attributes.


2. Define a class PhoneNumber with countryCode, cityCode and
lineNumber attributes.
3. Define a class Address with streetAddress, town, city, country and
phoneNumber attributes. Attribute phoneNumber shall be of type
PhoneNumber.

Define a class Student with name, email, cnic, course1, course2,


postalAddress and contactNumber attributes. Where course1 and
course2 should be of type Course, postalAddress shall be of type
Address; contactNumber should be of Type PhoneNumber. Define a
constructor in Student class that takes cnic, name and address only.
Create a StudentTest class, in its main method, create a Student object
i.e. student1. Fully initialize its’ all attributes. cnic, name and address
shall be initialized by constructor, other attributes shall be initialized
using setter methods. All attributes values shall be taken from user.
After the object is fully initialized, print all, attribute values from student
object reference.

Make another object student2, assume the student live at same address
as student1. Reuse the address object of student1 to initialize student2
address. You do not need to take attributes from user input for
student2 object. Change some attribute of address from student1 and
check, does it also change for student2, understand why and why not?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy