Page |1 High Level Language Lab Lab # 06: Composition and Inheritance Objectives: Constructor Object Relation Composition Inheritance Instructions: You are allowed to use your own laptops. You can consult the books, manuals and class lectures. You should have stationary like register and ballpoint to analyze the tasks first. Ensure that your working environment is working properly. Only in practice session, consultation is allowed. Attempt all questions yourself and follow programming standards. Write down your task on register and submit your work on 13/04/2022. High Level Language Lab By: Mr. Uzair Saeed Page |2 Introduction In OOP, various kinds of relationships (i.e. inheritance, association and composition) exist between various classes/objects. Inheritance is modeled as is-a relationship. Inheritance exists between classes and main goal of inheritance is make object that would represent specialized version of original objects. For example, a car is vehicle, student is person etc. On the other hand, association and aggregation/composition represent uses-a and has-a relationship, respectively between objects. It is based on the object-oriented design where an object can acquire properties of another objects. In advance, aggregation and composition are two types of association representing weak and strong relationship. Composition is special kind of aggregation which represents ownership between objects along with the existence of life-cycle dependency. A department employee relationship is an example of aggregation whereas university-department relationship is an example of composition. Experiment 1: Department/Employee (Use of Composition) // // // // // // // File: MyMain.java Version: V1.0 Date: 05-04-2022 Name: Uzair Saeed ID: BS-Teacher-01 This is runnable class for Department Demonstrate use of composition public class MyMain { public static void main(String[] args) { Department cs = new Department("Computational Sciences"); cs.DepInfo(); } } //File: Department.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is department class which contain name and teacher info class Department{ private String depName;//store department name private Teacher[] teacher;//teacher object composition public Department(String depName) { High Level Language Lab By: Mr. Uzair Saeed Page |3 this.depName = depName; teacher = new Teacher[2];//adding two teacher in department teacher[0] = new Teacher("Ehsan",2,3,2020); teacher[1] = new Teacher("Naveed",31,1,2022); } //method for displaying department info public void DepInfo() { System.out.print("Department : "+depName); System.out.print(" has following faculty "); for(int i = 0 ; i<teacher.length;i++) { System.out.println("\n\n");//adding new line System.out.print(i+1); teacher[i].TeacherInfo();//calling teacher method } } } //File: Teacher.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is teacher class which contain name and joining date class Teacher{ private String name;//store teacher name private Date joiningDate;//composition of Date class object //paramerterize constructor of date class public Teacher(String name, int day, int month, int year) { this.name = name; //calling date parameterize constructor this.joiningDate = new Date(day,month, year); } //method for displaying TeacherInfo public void TeacherInfo() { System.out.print(" Name :"+name); joiningDate.JoinInfor();//calling date public method } } //File: Date.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is date class which contain date, month and year High Level Language Lab By: Mr. Uzair Saeed Page |4 class Date{ private int day;//hold day of month private int month;//store month private int year;//hold year //paramerterize constructor of date class public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } //method for displaying joining date public void JoinInfor() { System.out.print(" Joining Date: "+day+"/"+month+"/"+year); } } Experiment 2: Employee Program (Use of inheritance) // File: TestEmployee.java // Version: V1.0 // Date: 05-04-2022 // Name: Uzair Saeed // ID: BS-Teacher-01 // This is runnable class for Employee // Demonstrate use of inheritance public class TestEmployee { public static void main(String args[]) { SalaryComEmployee ali = new SalaryComEmployee("Muhammad","Ali","33100-222",100,0.1f,5000); ComissionEmployee majid = new ComissionEmployee("Majid","Khan","33101-45",500,0.5f); System.out.println("Salried Employee Detail "); System.out.println(ali);//call toString method System.out.println("Total Earning "+ali.earnings()); High Level Language Lab By: Mr. Uzair Saeed Page |5 System.out.println("\n\nCommission Employee Detail "); System.out.println(majid);//call toString method System.out.println("Total Earning "+majid.earnings()); System.out.println("\n\nChanging Ali Data"); ali.setFirstName("Waqas"); ali.setLastName("Jadon"); ali.setSalary(-200);//invalid data System.out.println("Salried Employee Detail "); System.out.println(ali);//call toString method System.out.println("Total Earning "+ali.earnings()); } } //File: Employee.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is Employee which define first, last name and cnic //Employee is parent class demonstrate use of inheritance class Employee{ private String firstName;//store firstName private String lastName;//store lastName private String cnic;//store cnic public Employee(String first, String last, String cnic) { firstName = first; lastName = last; this.cnic = cnic; } //getters and setters //which enforce encapsulation public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } High Level Language Lab By: Mr. Uzair Saeed Page |6 public String getCnic() { return cnic; } public void setCnic(String cnic) { this.cnic = cnic; } //toString is built-in method in java lang //performing method overriding //will call this method on object @Override public String toString() { String str = "First name is "+getFirstName(); str += "\nLast Name is "+getLastName(); str +="\nCnic is "+getCnic(); return str; } } //File: ComissionEmployee.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is ComissionEmployee which define sale and commission //ComissionEmployee is child class inherit properties of Employee class ComissionEmployee extends Employee{ private float grosSale;//weekly sale private float comRate;//commission rate public ComissionEmployee(String first, String last, String cnic, float sale, float rate) { super(first, last, cnic);//call parent class constructor setGrosSale(sale);//calling gross sale setter setComRate(rate);//commission rate setter } //getters and setters //which enforce encapsulation public float getGrosSale() { return grosSale; } public void setGrosSale(float sale) { if(sale > 0)//validate sale must be greater than zero grosSale = sale; } public float getComRate() { return comRate; High Level Language Lab By: Mr. Uzair Saeed Page |7 } public void setComRate(float rate) { if(rate > 0 && rate <= 1)//validate rate between 0 and 1 comRate = rate; } //earning method return earing of commission employee public float earnings() { return getComRate()*getGrosSale(); } //toString is built-in method in java lang //performing method overriding //will call this method on object @Override public String toString() { String str=super.toString();//employee class toString method str += "\nGross Sale is "+getGrosSale(); str +="\nCommission Rate is "+getComRate(); return str; } } //File: SalaryComEmployee.java //Version: V1.0 //Date: 05-04-2022 //Name: Uzair Saeed //ID: BS-Teacher-01 //This is SalaryComEmployee which define monthly salary //SalaryComEmployee is child class inherit properties of ComissionEmployee class SalaryComEmployee extends ComissionEmployee{ private int salary;//monthly salary public SalaryComEmployee(String first, String last, String cnic, float sale, float rate, int salary) { super(first, last, cnic,sale,rate);//call parent class constructor setSalary(salary); } //getters and setters //which enforce encapsulation public int getSalary() { return salary; } High Level Language Lab By: Mr. Uzair Saeed Page |8 public void setSalary(int salary) { if(salary > 0)//validate salary must be greater than zero this.salary = salary;//assigning salary } //earning method return earing of salaried employee public float earnings() { return salary+super.earnings(); } //toString is built-in method in java lang //performing method overriding //will call this method on object @Override public String toString() { String str=super.toString();//employee class toString method str += "\nMonthly Salary is "+getSalary(); return str; } } Salried Employee Detail First name is Muhammad Last Name is Ali Cnic is 33100-222 Gross Sale is 100.0 Commission Rate is 0.1 Monthly Salary is 5000 Total Earning 5010.0 Commission Employee Detail First name is Majid Last Name is Khan Cnic is 33101-45 Gross Sale is 500.0 Commission Rate is 0.5 Total Earning 250.0 Changing Ali Data Salried Employee Detail First name is Waqas Last Name is Jadon High Level Language Lab By: Mr. Uzair Saeed Page |9 Cnic is 33100-222 Gross Sale is 100.0 Commission Rate is 0.1 Monthly Salary is 5000 Total Earning 5010.0 Task 1: University System (Object Relationship) Consider six classes i.e. Person, Professor, Researcher, Department, Laboratory and University has following specifications. Class University has Two attributes of string i.e. universityName and location. An attribute named dept from Department Class Department has Two attributes i.e. deptId and depName. A constructor overloading to initialize date fields A method display() to show all attributes Class Laboratory contains Two attributes labId and experimentNo. A constructor overloading to initialize date fields Class Person has Two attributes of i.e. name and age. A constructor overloading to initialize date fields A method display() to show all attributes Class Professor is a Person and has Attributes of profId. Attributes of dept of type Department. A constructor overloading to initialize date fields A method display() to show all attributes Class Researcher is a Professor and has A data field named lab of type Laboratory A constructor overloading to initialize date fields A method display() to show all attributes a) Draw UML diagram for each class and show inheritance between these classes and composition relationship b) Implement all these classes in Java while illustrating concept of inheritance and composition. High Level Language Lab By: Mr. Uzair Saeed