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

USING JAVA /* This class is a completed linked implementation of a queue. Unfort

ID: 3868408 • Letter: U

Question

USING JAVA

/* This class is a completed linked implementation of a queue.

Unfortunately, it contains some logic errors.

Your task is to correct the logic errors.

There are hints throughout the code to help you find the errors.

Create a 'TestLinkedQueue' class with a main method that creates

a LinkedQueue and tests its operations inorder to find the logic

errors.

*/

/**

* LinkedQueue represents a linked implementation of a queue.

*

* @author Lewis and Chase

* @version 4.0

*/

public class LinkedQueue<T> implements QueueADT<T>

{

private int count;

private LinearNode<T> head, tail;

/**

* Creates an empty queue.

*/

public LinkedQueue()

{

/* There is a logic error in this method.

Hint: One line of code is missing.

*/

head = tail = null;

}

/**

* Adds the specified element to the tail of this queue.

* @param element the element to be added to the tail of the queue

*/

public void enqueue(T element)

{

/* There is a logic error in this method.

Hint: some code is missing.

*/

LinearNode<T> node = new LinearNode<T>(element);

if (isEmpty())

head = node;

else

tail.setNext(node);

}

/**

* Removes the element at the head of this queue and returns a

* reference to it.

* @return the element at the head of this queue

* @throws EmptyCollectionException if the queue is empty

*/

public T dequeue() throws EmptyCollectionException

{

/* There is a logic error in this method.

Hint: Some code is missing.

*/

if (isEmpty())

throw new EmptyCollectionException("queue");

T result = head.getData();

if (isEmpty())

tail = null;

return result;

}

/**

* Returns a reference to the element at the head of this queue.

* The element is not removed from the queue.

* @return a reference to the first element in this queue

* @throws EmptyCollectionsException if the queue is empty

*/

public T first() throws EmptyCollectionException

{

return(head.getData());

}

/**

* Returns true if this queue is empty and false otherwise.

* @return true if this queue is empty

*/

public boolean isEmpty()

{

/* There is a logic error in this method.

Hint: Check the boolean expression that is returned.

*/

return(head != null);

}

/**

* Returns the number of elements currently in this queue.

* @return the number of elements in the queue

*/

public int size()

{

return(count);

}

/* There is an infinite loop in this method.

*/

/**

* Returns a string representation of this queue.

* @return the string representation of the queue

*/

public String toString()

{

LinearNode current = head;

String toReturn = "";

  

if (current == null)

return("");

else

while (current != null)

toReturn = toReturn + ", " + current.getData().toString();

  

return(toReturn);

  

}

}

Explanation / Answer

Some needed files (LinearNode, QueueADT, EmptyCollectionException) are missing in the question. But I have fixed the logical errors in the code. Please do rate the answer if it helped. Thank you very much.

/* This class is a completed linked implementation of a queue.
Unfortunately, it contains some logic errors.
Your task is to correct the logic errors.
There are hints throughout the code to help you find the errors.
Create a 'TestLinkedQueue' class with a main method that creates
a LinkedQueue and tests its operations inorder to find the logic
errors.
*/

/**
* LinkedQueue represents a linked implementation of a queue.
*
* @author Lewis and Chase
* @version 4.0
*/
public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private LinearNode<T> head, tail;

/**
* Creates an empty queue.
*/
public LinkedQueue()
{
/* There is a logic error in this method.
Hint: One line of code is missing.
*/
head = tail = null;
count = 0;
  
}

/**
* Adds the specified element to the tail of this queue.
* @param element the element to be added to the tail of the queue
*/
public void enqueue(T element)
{
/* There is a logic error in this method.
Hint: some code is missing.
*/
LinearNode<T> node = new LinearNode<T>(element);

if (isEmpty())
head = tail = node;
else
{
tail.setNext(node);
tail = node;
}
count++;
}

/**
* Removes the element at the head of this queue and returns a
* reference to it.
* @return the element at the head of this queue
* @throws EmptyCollectionException if the queue is empty
*/
public T dequeue() throws EmptyCollectionException
{
/* There is a logic error in this method.
Hint: Some code is missing.
*/
if (isEmpty())
throw new EmptyCollectionException("queue");

T result = head.getData();
  
head = head.getNext();
if (isEmpty())
tail = null;
count--;
return result;
}

/**
* Returns a reference to the element at the head of this queue.
* The element is not removed from the queue.
* @return a reference to the first element in this queue
* @throws EmptyCollectionsException if the queue is empty
*/
public T first() throws EmptyCollectionException
{
   if (isEmpty())
throw new EmptyCollectionException("queue");
return(head.getData());
}

/**
* Returns true if this queue is empty and false otherwise.
* @return true if this queue is empty
*/
public boolean isEmpty()
{
/* There is a logic error in this method.
Hint: Check the boolean expression that is returned.
*/
return(head == null);
}

/**
* Returns the number of elements currently in this queue.
* @return the number of elements in the queue
*/
public int size()
{
return(count);
}

/* There is an infinite loop in this method.
*/
/**
* Returns a string representation of this queue.
* @return the string representation of the queue
*/
public String toString()
{
LinearNode current = head;
String toReturn = "";
  
if (current == null)
return("");
else
{
   toReturn = current.getData().toString();
   current = current.getNext();
while (current != null)
{
toReturn = toReturn + ", " + current.getData().toString();
current = current.getNext();
}
}
return(toReturn);
  
}
}