Lab 04 - Composition
Lab 04 - Composition
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.
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?