Hamza Afzal Lab 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Course: Object Oriented Programming

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

private String streetnumber;

private String city;

private String country;

public String getStreetnumber() {

return streetnumber;}

public void setStreetnumber(String streetnumber) {

this.streetnumber = streetnumber;

public String getCity() {

return city;

public void setCity(String city) {

this.city = city;

public String getCountry() {

return country;
}

public void setCountry(String country) {

this.country = country;

public class employee

private String employeeId;

private String employeename;

private Adress employeeAdress;

public employee(String employeeId, Adress employeeAdress)

this.employeeId = employeeId;

this.employeeAdress = employeeAdress;

public class EmployeeTest{

public static String Id;

public static String Adress;

public String name;

public String getName()

return name;

public void setName(String name)

this.name = name;
}

public EmployeeTest(String id, String adress)

Id = id;

Adress = adress;

public static void main(String[] args)

EmployeeTest employee1= new EmployeeTest(Id, Adress);

EmployeeTest employee2= new EmployeeTest(Id, Adress);

public static String getId()

return Id;

public static void setId(String id)

Id = id;

}
public static String getAdress()

return Adress;

public static void setAdress(String 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;

public void setDay(int day) {


if(day<=30)
this.day = day;
else
System.out.println("invalid day");
}
public int getDay() {
return day;
}
public void setMonth(int month) {
this.month = month;
}
public int getMonth() {
return month;
}
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
}
public Date(int day,int month,int year){
setDay(day);
setMonth(month);
setYear(year);
}

EMPLOYEE:

public class Employee {


private String FirstName;
private String LastName;
private Date Birthdat;
private Date HiringDate;

public void setFirstName(String FirstName) {


this.FirstName=FirstName;
}
public String getFirstName() {
return FirstName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public String getLastName() {
return LastName;
}
public void setBirthdat(Date Birthdat) {
this.Birthdat = Birthdat;
}
public Date getBirthdat() {
return Birthdat;
}
public void setHiringDate(Date HringDate) {
this.HiringDate = HiringDate;
}
public Date getHiringDate() {
return HiringDate;
}
public Employee(String FirstName,String LastName,Date Birthdat,Date Hiringdate){
setFirstName(FirstName);
setLastName(LastName);
setBirthdat(Birthdat);
setHiringDate(Hiringdate);
}
}

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....

Date Birthdate=new Date(day,month,year);


Date Hiringdate=new Date(day1,month1,year1);
Employee employee1=new Employee(FirstName,LastName,Birthdate,Hiringdate);

// Printing by using Getter

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.

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.
4. 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?

CODE:

public class Course {

private int courseCode;

private String courseTitle;

public void setCourseCode(int courseCode) {

this.courseCode = courseCode;
}

public void setCourseTitle(String courseTitle) {

this.courseTitle = courseTitle;

public int getCourseCode() {

return courseCode;

public String getCourseTitle() {

return courseTitle;

public class PhoneNumber {

private int countryCode;

private int cityCode;

private int lineNumber;

public void setCityCode(int cityCode) {

this.cityCode = cityCode;

}
public void setCountryCode(int countryCode) {

this.countryCode = countryCode;

public void setLineNumber(int lineNumber) {

this.lineNumber = lineNumber;

public int getCityCode() {

return cityCode;

public int getCountryCode() {

return countryCode;

public int getLineNumber() {

return lineNumber;

public class Addres {

private String streetAddress;

private String town;


private String city;

private String country;

private PhoneNumber phoneNumber;

public void setCountry(String country) {

this.country = country;

public void setCity(String city) {

this.city = city;

public void setStreetAddress(String streetAddress) {

this.streetAddress = streetAddress;

public void setTown(String town) {

this.town = town;

public String getCountry() {

return country;

}
public String getCity() {

return city;

public void setPhoneNumber(PhoneNumber phoneNumber) {

this.phoneNumber = phoneNumber;

public PhoneNumber getPhoneNumber() {

return phoneNumber;

public String getStreetAddress() {

return streetAddress;

public String getTown() {

return town;

public class Student {

private String name;

private String email;

private int cnic;


private Course course1;

private Course course2;

private Addres postalAddress;

private PhoneNumber contactNumber;

public Student(int cnic,String name,Addres postalAddress)

setCnic(cnic);

setName(name);

setPostalAddress(postalAddress);

public void setCnic(int cnic) {

this.cnic = cnic;

public void setContactNumber(PhoneNumber contactNumber) {

this.contactNumber = contactNumber;

public void setCourse1(Course course1) {

this.course1 = course1;

public void setCourse2(Course course2) {


this.course2 = course2;

public void setEmail(String email) {

this.email = email;

public void setName(String name) {

this.name = name;

public void setPostalAddress(Addres postalAddress) {

this.postalAddress = postalAddress;

public Addres getPostalAddress() {

return postalAddress;

public Course getCourse1() {

return course1;

public Course getCourse2() {


return course2;

public int getCnic() {

return cnic;

public PhoneNumber getContactNumber() {

return contactNumber;

public String getEmail() {

return email;

public String getName() {

return name;

import java.util.Scanner;

public class StudentTest {

public static void main(String[]args)

Scanner sc=new Scanner(System.in);


Addres ad1=new Addres();

System.out.println("Enter postalStreetAddress,town,city,country");

ad1.setStreetAddress(sc.nextLine());

ad1.setTown(sc.next());

ad1.setCity(sc.next());

ad1.setCountry(sc.next());

System.out.println("Enter Cnic and name:");

Student student1=new Student(sc.nextInt(), sc.next(), ad1);

PhoneNumber number=new PhoneNumber();

System.out.println("Enter Number country code, city code,line

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());

System.out.println("Enter course 1 code and title:");

Course course1=new Course();

Course course2=new Course();

course1.setCourseCode(sc.nextInt());

course1.setCourseTitle(sc.next());

student1.setCourse1(course1);

System.out.println("Enter course 2 code and title");


course2.setCourseCode(sc.nextInt());

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());

Addres ad2=new Addres();

ad2=ad1;

Student student2=new Student(23124,"svd",ad2);

ad2.setCountry("UK");

System.out.println("Country of Student

1:"+student1.getPostalAddress().getCountry());

System.out.println("Country of Student

2:"+student2.getPostalAddress().getCountry());

You might also like