Task 5: Composition

Task Last Updated on: Oct. 21, 2020

A class may have references to objects of other classes as members, its called composition. For example Car is composed of Engine and Wheels objects. User class has name, Address and age attributes, etc. Composition is also referred as "has-a" relation because we can make statement like "Student has-a name", "Rectangle has-a length", "Car has-a Engine" etc. Below task is to make sure you well understand how to use other types as attributes or fields in our classes. How to initialize these attributes, how to change and read the values of them.

Task 5.1

For each attributes described below, choose appropriate data type. All the attributes of each class should be private and exposed via get/set methods. Also define at least one constructor that allows to initialize 2-3 attributes of object.
  1. Define a class Course with courseCode and courseTitle instance variable.
  2. Define PhoneNumber class with countryCode, cityCode and lineNumber attributes.
  3. Define a class Address with streetAddress, town, city, country and phoneNumber attributes. The phoneNumber shall be of type PhoneNumber.
  4. Define a class Student with name, email, CNIC, course1, course2 and address attributes. Where course1 and course2 should be of type Course and address shall be of type Address. Define a constructor in Student class that shall take CNIC, name and address only.
Create a StudentTest class. In its main method, create a Student object named student1. Fully initialize its all attributes. CNIC, name and address shall be initialized using constructor. Other attributes shall be initialized using setter methods. All attributes' values shall be given by user. After the object is fully initialized, print all attribute values using get methods.

Make another object student2, assume the user also live at same address as student1. Reuse the address object of student1 to initialze student2 address. You don't need to take attributes from user input for student2 object. Print the values of all instance variables.

Task 5.2
In an institute, there are departments. Each department offers one ore more degree programs or courses. Each program has certain attributes. Try to think in these lines, and identify the classes and attributes you may need to define to manifest such environment in software. It may not be represented well without arrays, I would add more details into this after covering arrays topic. But, I want your focus, to start analyzing, things/concepts/entities in your surroundings in an object oriented design. It would help you in identifying right entities, their attributes and relations when you would be developing real softwares.

Task 5.3
Define a Date class with day, month and year attributes as int. Encapsulate all the fields and make a fully parameterized constructor as well. Then make Employee class with id, name and hireDate and birthDate attibutes, where last two attributes shall be of type Date that you defined yourself. When user set a date, make sure passed data in its set methods and constructor is valid day, month and year.


Post a Comment