In: Computer Science
Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class LinkedList. LinkedList has private instance variable list of Node type. Class Node is private inner class inside of class LinkedList, and has public instance variables: data and next of Employee and Node type, respectively. In addition, class Node has constructor and toString method. See LinkedList class bellow on the right. Class Employee has getters for all data, method String toString() that returns string of all employee data, and method boolean isHighEarner() that returns true if employee's salary is above average, and false otherwise.
public void printHighEarners () { } |
public class LinkedList
{
private Node list;
public LinkedList()
{
list = null;
}
public Node getList()
{
return list;
}
. . .//other methods
// insert method printHighEarners here
// insert method lowestPaidEmployeeRec
private class Node
{
public Employee data;
public Node next;
public Node(Employee emp)
{
data = emp;
next = null;
}
public String toString()
{
return data.toString();
}
}
}
Here's the updated code including the method which is asked:
class LinkedList
{
private Node list;
public LinkedList()
{
list = null;
}
public Node getList()
{
return list;
}
// method to print to high earners
public void printHighEarners()
{
// temp node for traversing the list
Node trav = list;
while (trav != null)
{
// check if current Employee is a high earner then print it else continue
if (trav.data.isHighEarner())
System.out.println(trav.data.toString());
trav = trav.next;
}
}
private class Node
{
public Employee data;
public Node next;
public Node(Employee emp)
{
data = emp;
next = null;
}
public String toString()
{
return data.toString();
}
}
}
Added comments for the method printHighEarners() for better understanding. Do refer them.
Hope it helps, for any further doubt, feel free to reach out!
Consider giving a thumbs up if it helped! :)
Cheers!