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

JAVA Code not compiling. Test driver error. ------------------------------------

ID: 3798386 • Letter: J

Question

JAVA Code not compiling. Test driver error.

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

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable temp might not have been initialized
   at LinkedQueue.toString(LinkedQueue.java:62)
   at LinkedQueueTest.main(LinkedQueueTest.java:7)
C:UsersUSER1AppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1

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

import support.LLNode;

public class LinkedQueue<T> implements QueueInterface<T>
{
   protected LLNode<T> front; // reference to the front of this queue
   protected LLNode<T> rear; // reference to the rear of this queue
   protected int numElements = 0; // number of elements in this queue
   public LinkedQueue()
   {
   front = null;
   rear = null;
   }
   public void enqueue(T element)
   // Adds element to the rear of this queue.
   {
   LLNode<T> newNode = new LLNode<T>(element);
   if (rear == null)
   front = newNode;
   else
   rear.setLink(newNode);
   rear = newNode;
   numElements++;
   }
   public T dequeue()
   // Throws QueueUnderflowException if this queue is empty;
   // otherwise, removes front element from this queue and returns it.
   {
   if (isEmpty())
   throw new QueueUnderflowException("Dequeue attempted on empty queue.");
   else
   {
   T element;
   element = front.getInfo();
   front = front.getLink();
   if (front == null)
   rear = null;
   numElements--;
   return element;
   }
   }
   public boolean isEmpty()
   // Returns true if this queue is empty; otherwise, returns false.
   {
   return (front == null);
   }

   public boolean isFull()
   // Returns false - a linked queue is never full.
   {
   return false;
   }
   public int size()
   // Returns the number of elements in this queue.
   {
   return numElements;
   }

   @Override
   public String toString(){
       LLNode<T> temp;
       String result = "";
       while(temp != null){
           result = result + temp.getInfo()+" ";
           temp = temp.getLink();
       }

       return result;
   }

   public void remove(int count) throws QueueUnderflowException{

       // if we have less number of items in queue, throw exception
       if(size() < count){
           throw new QueueUnderflowException();
       }
       // remove count number of elements from front
       for(int i=1; i<=count; i++)
           front = front.getLink();
   }

   public boolean swapStart(){
       // if we have lesser than 2 items in queue, return false
       if(size() < 2){
           return false;
       }

       // swapping data of first two elements
       T item = front.getLink().getInfo(); // getting data of second element from front
       front.getLink().setInfo(front.getInfo()); // setting front's data to second element from front
       front.setInfo(item); // setting data to front

       return true;
   }

   public boolean swapEnds(){
       // if we have lesser than 2 items in queue, return false
       if(size() < 2){
           return false;
       }

       LLNode<T> temp = front;

       // moving temp until temp next is not rear
       while(temp.getLink() != rear)
           temp = temp.getLink();

       // now temp pointing to second last element
       // swapping data of second last element and rear
       T item = temp.getInfo();
       temp.setInfo(rear.getInfo());
       rear.setInfo(item);

       return true;
   }
}


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


public class LLNode<T>
{
protected LLNode<T> link;
protected T info;

public LLNode(T info)
{
    this.info = info;
    link = null;
}

public void setInfo(T info){ this.info = info;}
public T getInfo(){ return info; }
public void setLink(LLNode<T> link){this.link = link;}
public LLNode<T> getLink(){ return link;}
}

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


public interface QueueInterface<T>
{
void enqueue(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the rear of this queue.

T dequeue() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.

boolean isFull();
// Returns true if this queue is full; otherwise, returns false.

boolean isEmpty();
// Returns true if this queue is empty; otherwise, returns false.

int size();
// Returns the number of elements in this queue.
}

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


public class QueueUnderflowException extends RuntimeException
{
public QueueUnderflowException()
{
    super();
}

public QueueUnderflowException(String message)
{
    super(message);
}
}
-------------------------

public class LinkedQueueTest
{
public static void main(String args[])
{
LinkedQueue<Integer> s=new LinkedQueue<>();

System.out.println(s.toString());
System.out.println(s.size());
}
}

Explanation / Answer

//Tested on Eclipse

/*****************LinkedQueue.java*********************/

public class LinkedQueue<T> implements QueueInterface<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
protected int numElements = 0; // number of elements in this queue
public LinkedQueue()
{
front = null;
rear = null;
}
public void enqueue(T element)
// Adds element to the rear of this queue.
{
LLNode<T> newNode = new LLNode<T>(element);
if (rear == null)
front = newNode;
else
rear.setLink(newNode);
rear = newNode;
numElements++;
}   
public T dequeue()
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
T element;
element = front.getInfo();
front = front.getLink();
if (front == null)
rear = null;
numElements--;
return element;
}
}
public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
return (front == null);
}
public boolean isFull()
// Returns false - a linked queue is never full.
{
return false;
}
public int size()
// Returns the number of elements in this queue.
{
return numElements;
}
@Override
public String toString(){
LLNode<T> temp=front;
String result = "";
while(temp != null){
result = result + temp.getInfo()+" ";
temp = temp.getLink();
}
return result;
}
public void remove(int count) throws QueueUnderflowException{
// if we have less number of items in queue, throw exception
if(size() < count){
throw new QueueUnderflowException();
}
// remove count number of elements from front
for(int i=1; i<=count; i++)
front = front.getLink();
}
public boolean swapStart(){
// if we have lesser than 2 items in queue, return false
if(size() < 2){
return false;
}
// swapping data of first two elements
T item = front.getLink().getInfo(); // getting data of second element from front
front.getLink().setInfo(front.getInfo()); // setting front's data to second element from front
front.setInfo(item); // setting data to front
return true;
}
public boolean swapEnds(){
// if we have lesser than 2 items in queue, return false
if(size() < 2){
return false;
}
LLNode<T> temp = front;
// moving temp until temp next is not rear
while(temp.getLink() != rear)
temp = temp.getLink();
// now temp pointing to second last element
// swapping data of second last element and rear
T item = temp.getInfo();
temp.setInfo(rear.getInfo());
rear.setInfo(item);
return true;
}
}

/*******************************LLNode.java********************/

public class LLNode<T>
{
protected LLNode<T> link;
protected T info;

public LLNode(T info)
{
this.info = info;
link = null;
}

public void setInfo(T info){ this.info = info;}
public T getInfo(){ return info; }
public void setLink(LLNode<T> link){this.link = link;}
public LLNode<T> getLink(){ return link;}
}

/**********************QueueInterface.java*************************/

public interface QueueInterface<T>
{
void enqueue(T element) throws QueueOverflowException;
// Throws QueueOverflowException if this queue is full;
// otherwise, adds element to the rear of this queue.
T dequeue() throws QueueUnderflowException;
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
boolean isFull();
// Returns true if this queue is full; otherwise, returns false.
boolean isEmpty();
// Returns true if this queue is empty; otherwise, returns false.

int size();
// Returns the number of elements in this queue.
}

/**************************QueueOverflowException .java*************************/

public class QueueOverflowException extends RuntimeException
{
public QueueOverflowException()
{
super();
}
public QueueOverflowException(String message)
{
super(message);
}
}

/***********************************************QueueUnderflowException.java******************/

public class QueueUnderflowException extends RuntimeException
{
public QueueUnderflowException()
{
super();
}
public QueueUnderflowException(String message)
{
super(message);
}
}

/************************************LinkedQueueTest.java*********************/

public class LinkedQueueTest
{
public static void main(String args[])
{
LinkedQueue<Integer> s=new LinkedQueue<>();
s.enqueue(10);
s.enqueue(20);
s.enqueue(30);
s.enqueue(40);
s.enqueue(50);
System.out.println(s.toString());
System.out.println(s.size());
}
}

/************************output*********************/

10 20 30 40 50
5

Thanks a lot