Object Oriented Programming- 14011102-4 Lab #7 Complete the following tasks in J
ID: 3735455 • Letter: O
Question
Object Oriented Programming- 14011102-4 Lab #7 Complete the following tasks in Java within the lab time using NetBeans Task 1: Create class Employee with the Employee ID and name as private data members. You must add any needed methods and constructors Task 2: Create class Department with the Department ID, name as data members.in addition to a list of the Employee's who work at the Department as a ArrayList from class Employee. Again all these data members must be declared private and you should add any needed methods or constructors. Task 3: Create class Company with a main method in it. In the main method create an ArrayList of Employees. Fill it with at least 5 Employee objects. Then print the Department information (Name, id and the list of employees) sorted by their ids. Hint: In order to sort the s of employee use Collections.sort( ListName)Explanation / Answer
/*Put the below source code in mentioned files. Turn on auto imports on ide */
/*Put the following code in Employee.java file. */
public class Employee {
public Employee(int employeId,String name){
this.employeId = employeId;
this.name = name;
}
@override
public String toString(){
return "Emp id: " +employeeId +". "+"Emp Name: "+name+". ";
}
public int getEmployeeId(){
return employeId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void setEmpId(int employeId){
this.employeId= employeId;
}
private int employeId;
private String name;
}
/*Department.java */
public class Department {
public Employee(int deptId,String name,ArrayList<Employee>employee){
this.deptId = deptId;
this.name = name;
this.employee = employee;
}
public ArrayList<Employee> getEmployee(){
return employee;
}
public void setEmployee(ArrayList<Employee> employee){
this.employee = employee;
}
public int getDeptId(){
return deptId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void setDeptId(int deptId){
this.deptId= deptId;
}
private int deptId;
private String name;
private List<Employe> employee;
}
/*Company.java*/
public class Company{
public static void main(String[] args){
List<Employe>employees = new ArrayList<Employee>();
employees.add(new Employee(1,"John"));
employees.add(new Employee(4,"Dany"));
employees.add(new Employee(3,"Khal"));
employees.add(new Employee(2,"Jora"));
employees.add(new Employee(5,"Drogo"));
Collections.sort(employees,
new Comparator<Employee>{
public int compare(Employee s1,Employee s2){ if(s1.getEmployeeId()==s2.getEmployeeId()) return 0; else if(s1.getEmployeeId()>s2.getEmployeeId()) return 1; else return -1; }
}());
Department dept = new Department(1,"Production",employees);
System.out.println("Dept Id : " +dept.getDeptId());
System.out.println("Dept Name : " +dept.getName());
for(Employee emp : dept.getEmployee()){
System.out.println(emp.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.