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

In Ecplise Java create a queue class (for characters) must use an array or a lin

ID: 3909473 • Letter: I

Question

In Ecplise Java create a queue class (for characters) must use an array or a linked list.

(a) Build a test program that stores the alphabet using your queue.

(b) You can use enqueue() or dequeue() methods. In dequeue() method, you need to be able throw an exception where there is nothing to dequeue (e.g., throw new RuntimeException("Queue underflow");).

(c) Limit the size of the queue so that you can only store up to 10 characters. If you enter more than 10 characters, your test program should show an error message.

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


LinkedQueue.java

public class LinkedQueue<E> {
private static final int MAX_SIZE = 10;
private class Node {
private E element;
private Node link;
}

private Node head; // Reference to first node in list
private Node tail; // Reference to last node in list
private int size; // Number of elements in list

public LinkedQueue() {
head = null;
tail = null;
size = 0;
}



/**
* Adds the given element to the tail of the queue.
* @param element element to add
*/
public void add(E element) {
if(isFull())
throw new RuntimeException("Queue overflow");
Node n = new Node();
n.element = element;
n.link = null;
if (head == null) {
head = n;
} else {
tail.link = n;
}
tail = n;
size++;
}

/**
* Removed and returns the element at the head of the queue.
* @return element at head of queue
* @throws RuntimeException if queue is empty
*/

public E remove() {
if (head == null) {
throw new RuntimeException("Queue underflow");
} else {
E removed = head.element;
head = head.link;
if (head == null) {
tail = null;
}
size--;
return removed;
}
}

/**
* Returns the size (i.e. number of elements) of the queue.
* @return size of queue
*/
public int size() {
return size;
}


public boolean isFull(){
if(size == MAX_SIZE)
return true;
else
return false;
}

public boolean isEmpty(){
if(head == null)
return true;
else
return false;
}

public boolean contains(Object o)
{
Node curr = head;
while(curr != null)
{
if(curr.element.equals(o))
return true;
curr = curr.link;
}
return false;
}

public String toString()
{
String s = "[";
if(size > 0)
{
s += head.element;
Node curr = head.link;
while(curr != null)
{
s += ", " + curr.element;
curr = curr.link;
}
}
s += "]";
return s;
}
}


TestLinkedQueue.java
-------------

public class TestLinkedQueue {
public static void main(String[] args) {
LinkedQueue<Character> letters = new LinkedQueue<Character>();

char ch = 'A';

try{
System.out.println("Try to add 12 letters into the queue");
for(int i = 1; i < 12; i++, ch++)
{
System.out.println("Adding " + ch);
letters.add(ch);
}
}catch(Exception e)
{
System.out.println("*******Exception: "+ e.getMessage() + "********");
}


System.out.println("Queue size = " + letters.size());
System.out.println("Queue: " + letters);
System.out.println("Removing from front of queue");
while(!letters.isEmpty())
{
System.out.println("Removed " + letters.remove());
}
System.out.println("Queue size = " + letters.size());
System.out.println("Queue: " + letters);

}
}


output
----
Try to add 12 letters into the queue
Adding A
Adding B
Adding C
Adding D
Adding E
Adding F
Adding G
Adding H
Adding I
Adding J
Adding K
*******Exception: Queue overflow********
Queue size = 10
Queue: [A, B, C, D, E, F, G, H, I, J]
Removing from front of queue
Removed A
Removed B
Removed C
Removed D
Removed E
Removed F
Removed G
Removed H
Removed I
Removed J
Queue size = 0
Queue: []