Ad

Task 8 (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 name 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.


//ADRESS CLASS

public class Adress
{
private String streetNumber;
private String city;
private String country;
//streetNumber getter setter
public void setstreetNumber(String streetNumber)
{
this.streetNumber = streetNumber;
}
public String getstreetNumber()
{
return streetNumber;
}

//city getter setter

public void setcity(String city)
{
this.city = city;
}
public String getcity()
{
return city;
}

//country getter setter

public void setcountry(String country)
{
this.country = country;
}
public String getcountry()
{
return country;
}
}

//EMPLOYE CLASS

public class Employ
{
private int employeID;
private String employeName;
private Adress employeAdress;

public void setemployeID(int employeID)
{
this.employeID = employeID;
}
public int getemployeID()
{
return employeID;
}

public void setemployeName(String employeName)
{
this.employeName = employeName;
}
public String getemployeName()
{
return employeName;

}
public void setemployeAdress(Adress employeAdress)
{
this.employeAdress= employeAdress;
}
public Adress getemployeAdress()
{
return employeAdress;
}
}

//MAIN CLASS

import java.util.*;

public class EmployeTest
{
public static void main(String args[])
{
Scanner obj1 = new Scanner(System.in);
Adress adress1 = new Adress();
adress1.setstreetNumber("s#10");
adress1.setcity("Chunian");
adress1.setcountry("Paistan");
Employ emp1 = new Employ();
Employ emp2 = new Employ();

emp1.setemployeID(123);
System.out.println("Employe1 ID:  " +emp1.getemployeID());
System.out.print("Enter the name of employe1: ");
String name = obj1.nextLine();
emp1.setemployeName(name);
     System.out.println("Employe Name:  " +emp1.getemployeName());
emp1.setemployeAdress(adress1);
System.out.println("jhor town  ," +emp1.getemployeAdress().getstreetNumber());
System.out.println(emp1.getemployeAdress().getcity());
System.out.println(emp1.getemployeAdress().getcountry());
//employe2
emp2.setemployeID(456);
System.out.println("Employe2 ID:  " +emp2.getemployeID());
System.out.print("Enter the name of employe2: ");
String name2 = obj1.nextLine();
emp2.setemployeName(name2);
     System.out.println("Employe Name:  " +emp2.getemployeName());
emp2.setemployeAdress(adress1);
System.out.println("jhor town  ," +emp2.getemployeAdress().getstreetNumber());
  System.out.println(emp2.getemployeAdress().getcity());
System.out.println(emp2.getemployeAdress().getcountry());
}
}


OUTPUT:

Post a Comment

0 Comments