Question

In: Computer Science

Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class...

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();

          }

      }     

}                                                                                                                                                                                                                                                                               

Solutions

Expert Solution

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


Related Solutions

Write an iterative method printHighEarners() to print all high earner employees. Method printHighEarners()is in the class...
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...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that returns a partial string: public static String getPartOfString(int n, String firstOrLast, String inWord) Write a test program that uses this method to display the first or last number of characters of a string provided by the user. The program should output an error if the number requested is larger than the string. SAMPLE RUN #1: java PartOfString Enter a string: abracadabra↵ Enter the number...
Iterative method of solving large systems Is there any example for which Jacobi method fails and...
Iterative method of solving large systems Is there any example for which Jacobi method fails and Gauss-Seidel method succeeds?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT