Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I\'m having a hard time understanding linkedlist and queues. Any help woul

ID: 3842724 • Letter: H

Question

Hello I'm having a hard time understanding linkedlist and queues. Any help would be appreciated .

I have to create a Java class , instantiate several objects of that class, add those object to a LinkedList, and manipulate that list using several methods from the LinkedList class.
   Create a class of your own selection. It should have at least 1 instance variable, a constructor, toString(), and equals().
   In a different class, create a main method.
   In main, instantiate at least 5 objects of the class you created in step 1 above.
   Go to the Java API for LinkedList and read about the LinkedList class, along with its constructors and methods:
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html
   In main, demonstrate the use of the following methods of the LinkedList class:
   a constructor
   add(E e), addFirst(E e), and addLast(E e)
   remove(int index) and remove(Object o)
   contains(Object o)
   indexOf(Object o)
   size()
   toArray() – please print the contents of the new array
   Use a ListIterator to list all of the elements in your LinkedList
   Submit a copy of the class containing main and the class you created in step 1 above. Include a PrintScreen of the results of running main, Please indicate what you are demonstrating in your output to the user. (You can assume that the user does not have access to your source code.)

Also I need to Look up at the top of the API of Java’s LinkedList class, and notice that it implements the Queue interface. This means that we can use a LinkedList as if it were a queue.
Go to the Java API for Queue (you can just click on the link at the top of the LinkedList class API) and read about the requirements of the Queue interface:
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
In this part of the lab you will use the same class created in Project 1, instantiate several objects of that class, add those object to a LinkedList using those methods that are required of any class which implements the Queue interface..
   In a new class, create another main method (separate from the one in Project 1).
   In this main, instantiate at least 5 new objects of the class you created in step 1 from Project 1.
   Add those objects to a LinkedList which uses your class as its base type
   Do not use the add method – instead, use the offer method.
   In your class’s documentation, identify the difference between the add and offer methods.
   Use an iterator to list the elements in the LinkedList / Queue
   Note: this is normally not a function of a queue
   Use the peek method to display the element at the head of the LinkedList / Queue without actually removing that element.
   Use the poll method to retrieve and print all of the elements in the LinkedList / Queue until the list is empty.
   Do not use the remove method – instead, use the poll method.
   In your class’s documentation, identify the difference between the remove and poll methods.
   Submit a copy of the class containing main, and a PrintScreen of the results of running your program. Again, indicate what you are demonstrating in your output to the user.

Explanation / Answer

Find the programs below.

class:

Employee.java:

public class Employee {

   private String name;
   private String idNumber;
   private String department;
   public Employee() {}
  
   public Employee(String name, String idNumber, String department) {
       this.name = name;
       this.idNumber = idNumber;
       this.department = department;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getIdNumber() {
       return idNumber;
   }

   public void setIdNumber(String idNumber) {
       this.idNumber = idNumber;
   }

   public String getDepartment() {
       return department;
   }

   public void setDepartment(String department) {
       this.department = department;
   }

   @Override
   public String toString() {
       return "Employee [name=" + name + ", idNumber=" + idNumber + ", department=" + department + "]";
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Employee other = (Employee) obj;
       if (department == null) {
           if (other.department != null)
               return false;
       } else if (!department.equals(other.department))
           return false;
       if (idNumber == null) {
           if (other.idNumber != null)
               return false;
       } else if (!idNumber.equals(other.idNumber))
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }

  
}

LinkedListDemo:

package com.chegg.emp;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListdemo {
public static void main(String[] args) {
   Employee e1 = new Employee();
   Employee e2 = new Employee();
   Employee e3 = new Employee();
   Employee e4 = new Employee();
   Employee e5 = new Employee();
   e1.setName("bag");
   e1.setName("bag1");
   e1.setName("bag3");
   e1.setName("bag4");
   e1.setName("bag5");
   e1.setDepartment("dep");
   e2.setDepartment("dep2");
   e3.setDepartment("dep3");
   e4.setDepartment("dep4");
   e5.setDepartment("dep5");
   LinkedList<Employee> list = new LinkedList<Employee>();
   list.add(e1);
   list.add(e2);
   list.addLast(e3);
   list.add(e4);
   list.addFirst(e5);
   list.addAll(list);
   list.contains(e2);
   System.out.println(list.toString());
   list.removeLast();
   System.out.println(list.toString());  
   System.out.println(list.indexOf(e5));
   System.out.println(list.size());

   Iterator x = list.listIterator(1);
   // print list with the iterator
   while (x.hasNext()) {
   System.out.println(x.next());
   }
   }
}

OUTPUT for LinkedList class:

[Employee [name=null, idNumber=null, department=dep5], Employee [name=bag5, idNumber=null, department=dep], Employee [name=null, idNumber=null, department=dep2], Employee [name=null, idNumber=null, department=dep3], Employee [name=null, idNumber=null, department=dep4], Employee [name=null, idNumber=null, department=dep5], Employee [name=bag5, idNumber=null, department=dep], Employee [name=null, idNumber=null, department=dep2], Employee [name=null, idNumber=null, department=dep3], Employee [name=null, idNumber=null, department=dep4]]
[Employee [name=null, idNumber=null, department=dep5], Employee [name=bag5, idNumber=null, department=dep], Employee [name=null, idNumber=null, department=dep2], Employee [name=null, idNumber=null, department=dep3], Employee [name=null, idNumber=null, department=dep4], Employee [name=null, idNumber=null, department=dep5], Employee [name=bag5, idNumber=null, department=dep], Employee [name=null, idNumber=null, department=dep2], Employee [name=null, idNumber=null, department=dep3]]
0
9
Employee [name=bag5, idNumber=null, department=dep]
Employee [name=null, idNumber=null, department=dep2]
Employee [name=null, idNumber=null, department=dep3]
Employee [name=null, idNumber=null, department=dep4]
Employee [name=null, idNumber=null, department=dep5]
Employee [name=bag5, idNumber=null, department=dep]
Employee [name=null, idNumber=null, department=dep2]
Employee [name=null, idNumber=null, department=dep3]

------------------------------------------

QUEUE:

QueueExample.JAVA:

// Java orogram to demonstrate working of Queue
// interface in Java
import java.util.LinkedList;
import java.util.Queue;

public class QueueExample
{
public static void main(String[] args)
{
   Queue<Integer> q = new LinkedList<>();

   // Adds elements {0, 1, 2, 3, 4} to queue
   for (int i=0; i<5; i++)
   q.offer(i);

   // Display contents of the queue.
   System.out.println("Elements of queue-"+q);

   // To remove the head of queue.
   int removedele = q.poll();
   System.out.println("removed element-" + removedele);

   System.out.println(q);

   // To view the head of queue
   int head = q.peek();
   System.out.println("head of queue-" + head);

   // Rest all methods of collection interface,
   // Like size and contains can be used with this
   // implementation.
   int size = q.size();
   System.out.println("Size of queue-" + size);
}
}

OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote