Hamza Afzal Lab 5
Hamza Afzal Lab 5
Hamza Afzal Lab 5
Lab 05
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.
Code:
public class Adress
return streetnumber;}
this.streetnumber = streetnumber;
return city;
this.city = city;
return country;
}
this.country = country;
this.employeeId = employeeId;
this.employeeAdress = employeeAdress;
return name;
this.name = name;
}
Id = id;
Adress = adress;
return Id;
Id = id;
}
public static String getAdress()
return Adress;
Adress = adress;
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.
public class Date {
private int day;
private int month;
private int year;
EMPLOYEE:
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Name:");
String FirstName= sc.nextLine();
System.out.println("Enter Last name:");
String LastName=sc.nextLine();
System.out.println("Enter Birth Date: Day/Month/Year");
int day= sc.nextInt();
int month= sc.nextInt();
int year= sc.nextInt();
System.out.println("Enter Hiring Date: Day/month/year");
int day1= sc.nextInt();
int month1=sc.nextInt();
int year1= sc.nextInt();
// Making object....
System.out.println("First Name:"+employee1.getFirstName());
System.out.println("Last Name:"+employee1.getLastName());
System.out.println("Birthdate:"+Birthdate.getDay()+"/"+Birthdate.getMonth()
+"/"+Birthdate.getYear());
System.out.println("Hiringdate: "+Hiringdate.getDay()
+"/"+Hiringdate.getMonth()
+"/"+Hiringdate.getYear());
}
}
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.
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?
CODE:
this.courseCode = courseCode;
}
this.courseTitle = courseTitle;
return courseCode;
return courseTitle;
this.cityCode = cityCode;
}
public void setCountryCode(int countryCode) {
this.countryCode = countryCode;
this.lineNumber = lineNumber;
return cityCode;
return countryCode;
return lineNumber;
this.country = country;
this.city = city;
this.streetAddress = streetAddress;
this.town = town;
return country;
}
public String getCity() {
return city;
this.phoneNumber = phoneNumber;
return phoneNumber;
return streetAddress;
return town;
setCnic(cnic);
setName(name);
setPostalAddress(postalAddress);
this.cnic = cnic;
this.contactNumber = contactNumber;
this.course1 = course1;
this.email = email;
this.name = name;
this.postalAddress = postalAddress;
return postalAddress;
return course1;
return cnic;
return contactNumber;
return email;
return name;
import java.util.Scanner;
System.out.println("Enter postalStreetAddress,town,city,country");
ad1.setStreetAddress(sc.nextLine());
ad1.setTown(sc.next());
ad1.setCity(sc.next());
ad1.setCountry(sc.next());
number");
number.setCountryCode(sc.nextInt());
number.setCityCode(sc.nextInt());
number.setLineNumber(sc.nextInt());
student1.setContactNumber(number);
System.out.println("Enter Email:");
student1.setEmail(sc.next());
course1.setCourseCode(sc.nextInt());
course1.setCourseTitle(sc.next());
student1.setCourse1(course1);
course2.setCourseTitle(sc.next());
student1.setCourse2(course2);
System.out.println("Name:"+student1.getName());
System.out.println("Email:"+student1.getEmail());
System.out.println("Cnic:"+student1.getCnic());
System.out.println("PhoneNumber:"+student1.getContactNumber().getCountryCode(
+"-"+student1.getContactNumber().getCityCode()+"-"+
student1.getContactNumber().getLineNumber());
System.out.println("Course 1
code:"+student1.getCourse1().getCourseCode());
System.out.println("Course 1
title:"+student1.getCourse1().getCourseTitle());
System.out.println("Course 2
code:"+student1.getCourse2().getCourseCode());
System.out.println("Course 2
title:"+student1.getCourse2().getCourseTitle());
System.out.println("Postal
address:"+student1.getPostalAddress().getStreetAddress()+","+
student1.getPostalAddress().getTown()+","+student1.getPostalAddress().getCity
()+
","+student1.getPostalAddress().getCountry());
ad2=ad1;
ad2.setCountry("UK");
System.out.println("Country of Student
1:"+student1.getPostalAddress().getCountry());
System.out.println("Country of Student
2:"+student2.getPostalAddress().getCountry());