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 () { } |
In JAVA
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();
}
}
}
package DS;
import java.util.Scanner;
// Defines class Employee
class Employee
{
// Instance variable to store employee
information
private String name;
private double salary;
// Parameterized constructor
Employee(String na, double sal)
{
name = na;
salary = sal;
}
// Getter method to return salary
double getSalary()
{
return salary;
}
// To string to return employee information
public String toString()
{
return "\n Name: " + name + "\n
Salary: $" + salary;
}
}// End of class employee
// Defines class LinkedList
class LinkedList
{
// Creates head node
private Node list;
// Default constructor
public LinkedList()
{
list = null;
}
// Method to return list
public Node getList()
{
return list;
}
// Method to create a node and returns it
private Node createNode()
{
// Scanner class object
created
Scanner sc = new
Scanner(System.in);
// Accepts employee
information
System.out.print("\n Enter employee
name: ");
String name = sc.next();
System.out.print("\n Enter employee
salary: ");
double sal = sc.nextDouble();
// Creates a employee node using
parameterized constructor
Node node = new Node(new
Employee(name, sal));
// Assigns null to next to the
current node
node.next = null;
// Returns the node
return node;
}// End of method
// Method to insert a item node
void insert()
{
// Checks if head is null then
empty list
// First employee to insert
if (list == null)
{
// Calls the
method to create item node
list =
createNode();
}// End of if condition
// Otherwise not empty
else
{
// Creates a
node pointing to head
Node current =
list;
// Calls the
method to create a node
Node newNode =
createNode();
// Loops till
end of the list
while
(current.next != null)
// Move to next node
current = current.next;
// Assigns
the newly created node to current node next
current.next =
newNode;
}// End of else
}// End of method
// Method to return average salary
private double getAverageSalary()
{
double total = 0;
// Checks if head is null then
display error message
if (list == null)
{
System.out.println("\n Empty Linked List.");
return
0.0;
}// End of if condition
// Otherwise not empty
else
{
// Creates a
node pointing to head
Node current =
list;
// Counter for
number of nodes
int count =
0;
// Loops till
end of the linked list
while (current
!= null)
{
// Increase the counter by one
count++;
// Calculates total
total += current.data.getSalary();
// Move to next item
current = current.next;
}// End of while
loop
// Calculates
and returns average salary
return total /
count;
}// End of else
}// End of method
// Method to return true if parameter node salary is
greater then
// average salary
boolean isHighEarner(Node newSal)
{
// Calls the method to get average
salary
double avgSal =
getAverageSalary();
// Initial status is false
boolean status = false;
// Checks if head is null then
display error message and sets the status to false
if (list == null)
{
System.out.println("\n Empty Linked List.");
status =
false;
}// End of if condition
// Otherwise checks if parameter
node salary is grater than average salary
// sets the status to true
else if(newSal.data.getSalary()
> avgSal)
status =
true;
// returns the status
return status;
}// End of method
// Method to display the highest paid salary employee
information
void printHighEarners()
{
// Creates a node points to head
node as highest paid salary employee
Node highEarn = list;
// Checks if head is null then
display error message
if (list == null)
{
System.out.println("\n Empty Linked List.");
}// End of if condition
// Otherwise not empty
else
{
// Creates a
node pointing to head
Node current =
list;
// Loops till
end of the linked list
while (current
!= null)
{
// Checks if current node salary is greater than
the
// earlier high earn employee salary
if(current.data.getSalary() >
highEarn.data.getSalary())
// Assigns the current node
to high earn
highEarn = current;
// Move to next item
current = current.next;
}// End of while
loop
}// End of else
// Display the high earn
employee information
System.out.println("\n High earned
employee: " + highEarn);
}// End of method
// Method to display the lowest paid salary employee
information
void lowestPaidEmployeeRec()
{
// Creates a node points to head
node as lowest paid salary employee
Node lowEarn = list;
// Checks if head is null then
display error message
if (list == null)
{
System.out.println("\n Empty Linked List.");
}// End of if condition
// Otherwise not empty
else
{
// Creates a
node pointing to head
Node current =
list;
// Loops till
end of the linked list
while (current
!= null)
{
// Checks if current node salary is less than
the
// earlier low earn employee salary
if(current.data.getSalary() <
lowEarn.data.getSalary())
// Assigns the current node
to low earn
lowEarn = current;
// Move to next item
current = current.next;
}// End of while
loop
}// End of else
// Display the low earn employee
information
System.out.println("\n Lowest
earned employee: " + lowEarn);
}// End of method
void printAll()
{
// Checks if head is null then display error
message
if (list == null)
{
System.out.println("\n Empty Linked
List.");
}// End of if condition
// Otherwise not empty
else
{
// Creates a node pointing to
head
Node current = list;
// Loops till end of the linked
list
while (current != null)
{
// Displays
current employee information
System.out.println(current);
// Move to next
item
current =
current.next;
}// End of while loop
}// End of else
}// End of method
// Creates a inner class Node
private class Node
{
// Declares an object of class Employee
public Employee data;
// To point to next node employee
public Node next;
// Parameterized constructor to create a node
public Node(Employee emp)
{
data = emp;
next = null;
}
// Overrides method toString() to return node information
public String toString()
{
return data.toString();
}// End of method
}// End of class Node
}// End of LinkedList
// Driver class definition
public class LinkedListEmployee
{
// main method definition
public static void main(String ss[])
{
// Creates and object of class
LinkedList
LinkedList ll = new
LinkedList();
// Calls the method to insert
node
ll.insert();
ll.insert();
ll.insert();
ll.insert();
// Calls the method to display all
nodes
ll.printAll();
// Calls the method to display high
earned employee nodes
ll.printHighEarners();
// Calls the method to display low
earned employee nodes
ll.lowestPaidEmployeeRec();
}// End of main method
}// End of driver class
Sample Output:
Enter employee name: Pyari
Enter employee salary: 98000
Enter employee name: Sasmita
Enter employee salary: 78000
Enter employee name: Sita
Enter employee salary: 12000
Enter employee name: Gita
Enter employee salary: 65000
Name: Pyari
Salary: $98000.0
Name: Sasmita
Salary: $78000.0
Name: Sita
Salary: $12000.0
Name: Gita
Salary: $65000.0
High earned employee:
Name: Pyari
Salary: $98000.0
Lowest earned employee:
Name: Sita
Salary: $12000.0