Question & Answer: please answer in java make comments on each line of the code show output on Eclipse console…..

please answer in java make comments on each line of the code show output on Eclipse console

5. Problem Statement Create a Priority Queue Integer with defult Comparator e Add some elements in the PriorityQueue object Remove the elements from Head e Remove the elements from PrioirityOQueue from Head Create a PriorityQueue Employee> class with Customized Comparator Add the objects of Employee class in the PriorityQueue object. Use the Comparator to sort the employees as per their salaries .

5. Problem Statement Create a Priority Queue Integer with defult Comparator e Add some elements in the PriorityQueue object Remove the elements from Head e Remove the elements from PrioirityOQueue from Head Create a PriorityQueue Employee> class with Customized Comparator Add the objects of Employee class in the PriorityQueue object. Use the Comparator to sort the employees as per their salaries .

Expert Answer

 

IntegerQueue.java

import java.util.*;

public class IntegerQueue

{

 

public static void main(String[] args)

{

PriorityQueue<Integer> integerQueue=new PriorityQueue<Integer>();

integerQueue.add(5);

integerQueue.add(4);

integerQueue.add(2);

System.out.print(“Removing elements from head of queue:”);

while(integerQueue.size()!=0){

System.out.print(integerQueue.poll()+” “);

}

System.out.println();

}

}

Output:

EmployeeQueue.java

import java.util.*;

import java.io.*;

public class EmployeeQueue

{

static class Employee {

String name;

double salary;

Employee(String n,double s){

name=n;

salary=s;

}

}

static class EmployeePriority implements Comparator<Object> { public int compare(Object o1, Object o2) { Employee e1 = (Employee) o1; Employee e2 = (Employee) o2; return Double.compare(e1.salary, e2.salary); } }

public static void main(String[] args)

{

EmployeePriority priority=new EmployeePriority() ;

PriorityQueue<Employee> employeeQueue=new PriorityQueue<Employee>(10,priority);

Employee e1=new Employee(“Joerge”,25000.0);

Employee e2=new Employee(“Micheal”,5000.0);

Employee e3=new Employee(“David”,35000.0);

Employee e4=new Employee(“Jhon”,80000.0);

employeeQueue.add(e1);

employeeQueue.add(e2);

employeeQueue.add(e3);

employeeQueue.add(e4);

Employee temp;

System.out.println(“Employees sorted on salary:”);

while(!employeeQueue.isEmpty()){

temp=employeeQueue.poll();

System.out.println(“Name:”+temp.name+” “+”Salary:”+temp.salary);

}

System.out.println();

}

}

Output:

Still stressed from student homework?
Get quality assistance from academic writers!