Arrays and Iteration - Employee with Second Highest Age Hands-on Solution | TCS Fresco Play | Fresco Play
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
All Question of the MCQs Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
Arrays and Iteration - Employee with Second Highest Age Hands-on Solution | TCS Fresco Play | Fresco Play
/******Employee.java********************/
import java.util.Comparator;
public class Employee implements Comparable<Employee> {
public int id;
public String name;
public int age;
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getAge() {
return this.age;
}
@Override
public int compareTo(Employee employee) {
return (this.age - employee.age);
}
}
/******Solution.java****/
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Employee e1 = new Employee(1, "aaa", 22);
Employee e2 = new Employee(2, "bbb", 33);
Employee e3 = new Employee(3, "ccc", 55);
Employee e4 = new Employee(4, "ddd", 44);
Employee[] employees = new Employee[]{ e1, e2, e3, e4 };
Employee bija_mota_vadil = employeeWithSecondLowestAge(employees);
System.out.println(bija_mota_vadil.id+" "+bija_mota_vadil.name+a" "+bija_mota_vadil.age+".0");
}
private static Employee employeeWithSecondLowestAge(Employee[] employees) {
Employee[] sortedEmployees = new Employee[employees.length];
Arrays.sort(employees);
return employees[1];
}
}
Post a Comment