In: Computer Science
Write an iterative method countHighEarners() to return the number of nodes storing an high earner employee. Method countHighEarners()is in the class LinkedList that 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 int countHighEarners () { } |
public class LinkedList
{
private Node list;
public LinkedList()
{
list = null;
}
public Node getList()
{
return list;
}
. . .//other methods
// insert method countHighEarners here
private class Node
{
public Employee data;
public Node next;
public Node(Employee emp)
{
data = emp;
next = null;
}
public String toString()
{
return data.toString();
}
}
}
countHighEarners method implementation would be like this: define isHigherEarner in employee which will give true false when salary will be higher than average.
public int countHighEarners() {
int count = 0;
Node n = getList();
while (n != null) {
System.out.print(n.data + " ");
if
(n.data.isHigherEarner()) {
count++;
}
n = n.next;
}
return count;
}
signature for employee's class method
public boolean isHigherEarner() {
if(salary>avg) {
return
true;
}else {
return
false;
}
}