Question

In: Computer Science

Must be using Java Eclipse Please 4. Test program LinkedList.java, and make sure that you understand...

Must be using Java Eclipse Please

4. Test program LinkedList.java, and make sure that you understand each operation in the
program. (refer to linkedListApplication.java)

6. (Programming) Use LinkedList as a reference, add the following operations in the class
LinkedList;

a) Find the average data values of the linked list.
b) Find the node with largest key, and then delete the node. (Note, you must return
the node, not just the largest key)
c) Test ALL operations (Search, Insert, Delete, Append/Remove to/from the header)
in the Main method. (Also display the average of the data values of the linked list,
the largest key, the linked list before and after deleting the node with the largest
key;


7. (Programming) Modify LinkedList.java programs so that it handles employee objects.
Make your program menu-driven.

a) Find the average salary of the employees in the linked list.
b) Find the employee with largest ID, and then delete the node. (Note, you must return
the employee, not just the largest key, and delete it)
c) Test ALL operations (Search, Insert, Delete, Append/Remove to/from the header) in
the Main method. (Also display the average salary, the employee with largest ID, the
linked list before and after deleting the employee with the largest key;

LinkedList.java

import java.util.*;

public class LinkedList
{
   public Node header;

   public LinkedList()
   {
       header = null;
   }

   public final Node Search(int key)
   {
       Node current = header;
       while (current != null && current.item != key)
       {
           current = current.link;
       }
       return current;
   }

   public final void Append(int newItem)
   {
       Node newNode = new Node(newItem);
       newNode.link = header;
       header = newNode;
   }

   public final Node Remove()
   {
       Node x = header;
       if (header != null)
       {
           header = header.link;
       }
       return x;
   }

   public final Node searchPrevious(int key)
   {
       if (header == null)
       {
           return header;
       }
       else
       {
           Node current = header;
           while (!(current.link == null) && (current.link.item != key))
           {
               current = current.link;
           }
           return current;
       }
   }

   public final void Insert(int newItem, int preKey)
   {
       Node current;
       Node newNode = new Node(newItem);
       current = Search(preKey);
       if (current == null)
       {
           System.out.println("there is no such preKey!");
       }
       else
       {
           newNode.link = current.link;
           current.link = newNode;
       }
   }


   public final void Delete(int key)
   {
       if (header == null) // The list is empty!
       {
           System.out.println("The list is empty!");
       }
       else
       {
           if (header.item == key) // header to be deleted.
           {
               header = header.link;
           }
           else
           {
               Node p = searchPrevious(key);
               if (p.link == null)
               {
                   System.out.println("There is no such item!");
               }
               else
               {
                   p.link = p.link.link;
               }
           }
       }
   }

public final void ShowLinkedList()
{
if (header == null)
   System.out.println("The list is empty!");
else
{
Node current = header;
System.out.printf("%1$s->", current.item);
while (!(current.link == null))
{
current = current.link;
System.out.printf("%1$s->", current.item);

}
System.out.printf("null");
System.out.println();
}
}
   public final void PrintList()
   {
       if (header == null)
       {
           System.out.println("The list is empty!");
       }
       else
       {
           Node current = header;
           System.out.println(current.item);
           while (!(current.link == null))
           {
               current = current.link;
               System.out.println(current.item);
           }
       }
   }
}

Solutions

Expert Solution


//----------- LinkedList.java -------

import java.util.*;

class Node {
   public int item;
   public Node link;

   Node(int num) {
       item = num;
       link = null;
   }
}

public class LinkedList {
   public Node header;

   public LinkedList() {
       header = null;
   }

   public final Node Search(int key) {
       Node current = header;
       while (current != null && current.item != key) {
           current = current.link;
       }
       return current;
   }

   public final void Append(int newItem) {
       Node newNode = new Node(newItem);
       newNode.link = header;
       header = newNode;
   }

   public final Node Remove() {
       Node x = header;
       if (header != null) {
           header = header.link;
       }
       return x;
   }

   public final Node searchPrevious(int key) {
       if (header == null) {
           return header;
       } else {
           Node current = header;
           while (!(current.link == null) && (current.link.item != key)) {
               current = current.link;
           }
           return current;
       }
   }

   public final void Insert(int newItem, int preKey) {
       Node current;
       Node newNode = new Node(newItem);
       current = Search(preKey);
       if (current == null) {
           System.out.println("there is no such preKey!");
       } else {
           newNode.link = current.link;
           current.link = newNode;
       }
   }

   public final void Delete(int key) {
       if (header == null) // The list is empty!
       {
           System.out.println("The list is empty!");
       } else {
           if (header.item == key) // header to be deleted.
           {
               header = header.link;
           } else {
               Node p = searchPrevious(key);
               if (p.link == null) {
                   System.out.println("There is no such item!");
               } else {
                   p.link = p.link.link;
               }
           }
       }
   }

   public final void ShowLinkedList() {
       if (header == null)
           System.out.println("The list is empty!");
       else {
           Node current = header;
           System.out.printf("%1$s->", current.item);
           while (!(current.link == null)) {
               current = current.link;
               System.out.printf("%1$s->", current.item);

           }
           System.out.printf("null");
           System.out.println();
       }
   }

   public final void PrintList() {
       if (header == null) {
           System.out.println("The list is empty!");
       } else {
           Node current = header;
           System.out.println(current.item);
           while (!(current.link == null)) {
               current = current.link;
               System.out.println(current.item);
           }
       }
   }
   //method getAverage() that returns the average of Node values
   //in linked list.
  
   public double getAverage()
   {
       //if header is null then average will be 0.
       if(header == null)
       {
           return 0;
       }
       //to store average and sum of node values
       double avg = 0.0;
       double sum = 0;
       //store header in current node.
       Node current = header;
       //count is the number of nodes in linked list.
       int count = 0;
       //till last node.
       while (current != null)
       {
           //add current node value to the sum variable
           sum += current.item;
           //move to next node.
           current = current.link;
           //increment the count of nodes.
           count++;
       }
       //average is the division of sum / number of nodes.
       avg = sum / count;
       //return the average.
       return avg;
   }
   //function that returns the largest Node in the list.
   public Node getLargestNode()
   {
       //if header is null then there is no Largest Node.
       if(header == null)
       {
           return null;
       }
      
       //to store current node while processing.
       Node current = header;
       //set largest node value of item as the header node value.
       Node largest = new Node(current.item);
       //till end of last node.
       while (current!=null)
       {
           //if current node value is greater than the largest item value.
           if(current.item > largest.item)
           {
               //then largest item value will be current node value.
               largest.item = current.item;
           }
           //move to next node.
           current = current.link;
       }
       //return the largest node.
       return largest;
   }
}

//-------- linkedListApplication.java ------
//class that tests all the functionalities of LinkedList class.
public class linkedListApplication {
   public static void main(String[] args)
   {
       //crate linked list object
       LinkedList list = new LinkedList();
       //add values using Append() method
       list.Append(5);
       list.Append(1);
       list.Append(2);
       list.Append(3);
       list.Append(4);
       //display linked list.
       System.out.println("\nLinked List After insertion using Append()-----");
       list.ShowLinkedList();
      
       //insert using Insert(10,3) which inserts 10 after key 3.
       System.out.println("\nInserting 10 at previous key 3 using Insert() method");
       list.Insert(10,3);
       System.out.println("\nLinked List After insertion using Insert()-----");
       list.ShowLinkedList();
      
       //call getAverage() and store the result
       double avg = list.getAverage();
       System.out.println(String.format("\nAverage of Linked List values using getAverage() method : %.2f",avg));
      
      
       //call getLargestNode() and store the result
       Node largest = list.getLargestNode();
       System.out.println("\nLargest value in Linked list: "+largest.item);
      
       System.out.println("\nLinked List before deleting largest node: "+largest.item);
       list.PrintList();
      
       //remove the largest item value in linked list.
       list.Delete(largest.item);
       System.out.println("\nLinked List After Deleting Largest Node Value using Delete() method: "+largest.item+" -----");
       list.ShowLinkedList();
      
       System.out.println("\nLinked List before performing Remove() method");
       list.ShowLinkedList();
      
       //call Remove() method to remove head from linked list.
       list.Remove();
       System.out.println("\nLinked List After performing Remove() method");
       list.ShowLinkedList();
   }

}

//---------- OUTPUT -------------

// ----------------------- PART 7 ---------------------------


//----------- LinkedList.java -------

import java.util.*;

class Employee {
   public String name;
   public int id;
   public double salary;
   public Employee link;

   Employee(String name,int id,double salary) {
       this.name =name;
       this.id = id;
       this.salary = salary;
       link = null;
   }

   @Override
   public String toString() {
       return "Employee [ name=" + name + " , id=" + id + " , salary=" + salary + " ]";
   }
  
}

public class LinkedList {
   public Employee header;

   public LinkedList() {
       header = null;
   }

   public final Employee Search(int key) {
       Employee current = header;
       while (current != null && current.id != key) {
           current = current.link;
       }
       return current;
   }

   public final void Append(String name,int id,double salary) {
       Employee newEmployee = new Employee(name,id,salary);
       if(Search(newEmployee.id) == null)
       {
           newEmployee.link = header;
           header = newEmployee;
       }
       else
       {
           System.out.println("\nDuplicate User.User with id: "+id+" is already Existed");
       }
      
   }

   public final Employee Remove() {
       Employee x = header;
       if (header != null) {
           header = header.link;
       }
       return x;
   }

   public final Employee searchPrevious(int key) {
       if (header == null) {
           return header;
       } else {
           Employee current = header;
           while (!(current.link == null) && (current.link.id != key)) {
               current = current.link;
           }
           return current;
       }
   }

   public final void Insert(String name,int id,double salary, int preKey) {
       Employee current;
       Employee newEmployee = new Employee(name,id,salary);
       current = Search(preKey);
       if (current == null) {
           System.out.println("there is no such preKey!");
       } else {
           if(Search(id) == null)
           {
               newEmployee.link = current.link;
               current.link = newEmployee;
           }
           else
           {
               System.out.println("\nDuplicate User.User with id: "+id+" is already Existed");
           }
       }
   }

   public final void Delete(int key) {
       if (header == null) // The list is empty!
       {
           System.out.println("The list is empty!");
       } else {
           if (header.id == key) // header to be deleted.
           {
               header = header.link;
           } else {
               Employee p = searchPrevious(key);
               if (p.link == null) {
                   System.out.println("There is no such item!");
               } else {
                   p.link = p.link.link;
               }
           }
       }
   }

   public final void ShowLinkedList() {
       if (header == null)
           System.out.println("The list is empty!");
       else {
           Employee current = header;
           System.out.printf("[ %s,%d ] -> ", current.name, current.id);
           while (!(current.link == null)) {
               current = current.link;
               System.out.printf("[ %s,%d ] -> ", current.name, current.id);

           }
           System.out.printf("null");
           System.out.println();
       }
   }

   public final void PrintList() {
       if (header == null) {
           System.out.println("The list is empty!");
       } else {
           Employee current = header;
           System.out.println(current);
           while (!(current.link == null)) {
               current = current.link;
               System.out.println(current);
           }
       }
   }
   //method getAverage() that returns the average of Employee Salary values
   //in linked list.
  
   public double getAverageSalary()
   {
       //if header is null then average will be 0.
       if(header == null)
       {
           return 0;
       }
       //to store average and sum of employee salary
       double avg = 0.0;
       double sum = 0;
       //store header in current node.
       Employee current = header;
       //count is the number of nodes in linked list.
       int count = 0;
       //till last node.
       while (current != null)
       {
           //add current node salary value to the sum variable
           sum += current.salary;
           //move to next node.
           current = current.link;
           //increment the count of nodes.
           count++;
       }
       //average is the division of sum / number of nodes.
       avg = sum / count;
       //return the average.
       return avg;
   }
   //function that returns the largest Employee in the list.
   public Employee getLargestEmployeeId()
   {
       //if header is null then there is no Largest Employee.
       if(header == null)
       {
           return null;
       }
      
       //to store current node while processing.
       Employee current = header;
       //set largest node value of item as the header node value.
       Employee largest = new Employee(current.name,current.id,current.salary);
       //till end of last node.
       while (current!=null)
       {
           //if current node value is greater than the largest item value.
           if(current.id > largest.id)
           {
               //then largest item value will be current node value.
               largest.id = current.id;
               largest.name = current.name;
               largest.salary = current.salary;
           }
           //move to next node.
           current = current.link;
       }
       //return the largest node.
       return largest;
   }
}

//-------- linkedListApplication.java ------
//class that tests all the functionalities of LinkedList class.
public class linkedListApplication {
   public static void main(String[] args)
   {
       //crate linked list object
       LinkedList list = new LinkedList();
       //add values using Append() method
       list.Append("John wick",1,3000);
       list.Append("Tony Stark",2,40000);
       list.Append("Barry Allen",2,20000);
       list.Append("Rambo",3,40000);
       //display linked list.
       System.out.println("\nEmployees List After insertion using Append()-----");
       list.ShowLinkedList();
      
       //insert using Insert() which inserts Barray Allen with id 4.
       System.out.println("\nInserting Barry Allen with id 4 at previous key 3 using Insert() method");
       list.Insert("Barry Allen",4,25000,3);
       System.out.println("\nEmployees List After insertion using Insert()-----");
       list.ShowLinkedList();
      
       //call getAverage() and store the result
       double avg = list.getAverageSalary();
       System.out.println(String.format("\nAverage Salary of Employees List values using getAverage() method : %.2f",avg));
      
      
       //call getLargestNode() and store the result
       Employee largest = list.getLargestEmployeeId();
       System.out.println("\nLargest Id in Linked list: "+largest.id);
       System.out.println("Employee Details: "+largest);
      
       System.out.println("\nEmployees List before deleting largest node with id: "+largest.id);
       list.ShowLinkedList();
      
       //remove the largest item value in linked list.
       list.Delete(largest.id);
       System.out.println("\nEmployees List After Deleting Largest Node with Id Value using Delete() method: "+largest.id+" -----");
       list.ShowLinkedList();
      
       System.out.println("\nEmployees List before performing Remove() method");
       list.ShowLinkedList();
      
       //call Remove() method to remove head from linked list.
       list.Remove();
       System.out.println("\nEmployees List After performing Remove() method");
       list.PrintList();
   }

}

//-------------- OUTPUT ----------------


Related Solutions

Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or run error.) Q. 1 Calculate the expression a)         x= 7.0 + (12 %3)*5 – 3;                    b)         x= 7 + (11 / 2)*5 + 3;            Q 2: Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.) Q 3. Write a program that prompts the capacity, in gallons,...
THIS IS A VERY IMPORTANT QUESTION. PLEASE MAKE SURE YOU DO IT COMPLETELY AND UNDERSTAND THE...
THIS IS A VERY IMPORTANT QUESTION. PLEASE MAKE SURE YOU DO IT COMPLETELY AND UNDERSTAND THE CONCEPTS. VNA Enterprises, Inc. manufactures and sells a single product. A schedule of units sold for 3 months and related expenses are as follows: January February March Production in units 18900 22680 13230 Cost A $47327 $56512 $33549 Cost B $61425 $61425 $61425 Cost C $39123 $46948 $27386 Inspect the data in the table above. 1. Which cost is a variable cost? (Type A,...
20.1: Checking on the Drought [(JAVA, ECLIPSE), Please keep the program as short as possible] The...
20.1: Checking on the Drought [(JAVA, ECLIPSE), Please keep the program as short as possible] The California drought is a topic that is weighing heavily on the minds of many people in this state. With all the rain we have gotten this year, we are all hoping the drought is over Let's examine some rainfall data for Silicon Valley to get a sense of how this year's rainfall compares to the average rainfall. For this assignment, you will be practicing...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse...
Write a Java Program to determine if a candidate qualifies for a FHA Mortgage. (using eclipse IDE) The qualifications given are: Residence: The home must be your primary residence. Down payment: They must have a credit score of at least 563. The Java Program needs to do the following. This can all be done in the main() method. Create a boolean variable to store the Residence requirement. Prompt the user to "Enter 1 if the Home will be the primary...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT