Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:\Users\USER1\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1

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

import support.LLNode;

public class LinkedQueue implements QueueInterface { protected LLNode front; // reference to the front of this queue protected LLNode 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 newNode = new LLNode(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 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 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 { protected LLNode 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 link){this.link = link;} public LLNode getLink(){ return link;} }

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

public interface QueueInterface { 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 s=new LinkedQueue<>();

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

If you were Adidas, how would you compete with Nike?

Answered: 1 week ago

Question

5. Our efficiency focus eliminates free time for fresh thinking.

Answered: 1 week ago