Please follow instructions and use provided code files for this step are located
ID: 3563665 • Letter: P
Question
Please follow instructions and use provided code
files for this step are located below
all of the java files for the code are already provided below. All you have to do is implement them.
package employee;
public class Employee implements Comparable
{
private int empId;
private String name;
private int age;
private Double salary;
public Employee(int empId, String name, int age, Double salary)
{
this.empId = empId;
this.name = name;
this.age = age;
this.salary = salary;
}
// getters & setters
public int getEmpId()
{
return empId;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public Double getSalary()
{
return salary;
}
/**
* Compare a given Employee with this object.
* If employee id of this object is
* greater than the received object,
* then this object is greater than the other.
*/
public int compareTo(Employee other)
{
return this.empId - other.empId ;
}
@Override
public String toString()
{
return getEmpId() + " " + getName() + " " + getAge() + " " + getSalary();
}
}
package employee;
import java.util.Comparator;
public class EmpSortByAge implements Comparator
{
public int compare(Employee e1, Employee e2)
{
return e1.getAge() - (e2.getAge());
}
}
package employee;
import java.util.Comparator;
public class EmpSortByEmpId implements Comparator
{
public int compare(Employee e1, Employee e2)
{
return e1.getEmpId() - (e2.getEmpId());
}
}
package employee;
import java.util.Comparator;
public class EmpSortByName implements Comparator
{
public int compare(Employee e1, Employee e2)
{
return e1.getName().compareTo(e2.getName());
}
}
package employee;
import java.util.Comparator;
public class EmpSortBySalary implements Comparator
{
public int compare(Employee e1, Employee e2)
{
return e1.getSalary().compareTo( e2.getSalary() );
}
}
package employee;
import java.util.Collections;
import java.util.List;
public class TestEmployeeSort
{
public static void main(String[] args)
{
List coll = Util.getEmployees();
// sort - natural order - Compare.compareTo()
Collections.sort(coll);
Util.printList(coll, ""Natural" order - by "empID"");
System.out.println();
//use comparator - sort by age
Collections.sort( coll, new EmpSortByAge() );
Util.printList(coll,"Sort by "age"");
System.out.println();
//use comparator - sort by name
Collections.sort( coll, new EmpSortByName() );
Util.printList(coll,"Sort by "name"");
System.out.println();
//use comparator - sort by empId
Collections.sort( coll, new EmpSortByEmpId() );
Util.printList(coll,"Sort by "Employee ID"");
System.out.println();
//use comparator - sort by salary
Collections.sort( coll, new EmpSortBySalary() );
Util.printList(coll,"Sort by "Employee Salary"");
}//end main()
}//end TestEmployees
package employee;
import java.util.*;
public class Util
{
public static List getEmployees()
{
List col = new ArrayList();
col.add(new Employee(5, "Frank", 28, 1234.55));
col.add(new Employee(1, "Jorge", 19, 4321.55));
col.add(new Employee(6, "Bill", 34, 2314.55));
col.add(new Employee(3, "Michael", 10, 1342.55));
col.add(new Employee(7, "Simpson", 8, 3124.55));
col.add(new Employee(4, "Claire",16, 4123.55));
col.add(new Employee(8, "Lee", 40, 3412.55));
col.add(new Employee(2, "Mark", 30, 2341.55));
return col;
}
public static void printList(List list, String sortBy)
{
System.out.printf("%s %s ", sortBy, "EmpId Name Age Salary");
for (Employee e: list)
{
System.out.println(e);
}
}//end printList()
}
Explanation / Answer
Running this code produces the same output as before: