Question
Everything works except one thing. Default capacity is 3. When I run the program and enqueue three elements, the list function will not work because
Everything works except one thing. Default capacity is 3. When I run the program and enqueue three elements, the list function will not work because rear=0 (Found that when debugging. I think this is happening under expandCapacity). Please help (don't just increase default capacity to a larger number).
CircularArrayQueue.java
package jsjf;
import jsjf.exceptions.*;
/** * CircularArrayQueue represents an array implementation of a queue in * which the indexes for the front and rear of the queue circle back to 0 * when they reach the end of the array. * * @author Java Foundations * @version 4.0 */ public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 3; private int front, rear, count; private T[] queue;
/** * Creates an empty queue using the specified capacity. * @param initialCapacity the initial size of the circular array queue */ public CircularArrayQueue(int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); }
/** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { this(DEFAULT_CAPACITY); }
/** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. * @param element the element to add to the rear of the queue */ public void enqueue(T element) { if (size() == queue.length) expandCapacity();
queue[rear] = element; rear = (rear + 1) % queue.length;
count++; }
/** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length * 2]);
for (int scan = 0; scan
front = 0; rear = count; queue = larger; }
/** * Removes the element at the front of this queue and returns a * reference to it. * @return the element removed from the front of the queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) { System.out.println("Queue is empty. Please enqueue at least 1 element first."); } else { T result = queue[front];
queue[front] = null;
front = (front+1) % queue.length;
count--;
return result; } return null; }
/** * Returns a reference to the element at the front of this queue. * The element is not removed from the queue. * @return the first element in the queue * @throws EmptyCollectionException if the queue is empty */ public T first() throws EmptyCollectionException { if (isEmpty()) { System.out.println("Queue is empty. Please enqueue at least 1 element first."); } else { return queue[front]; } return null; }
/** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { return count
/** * Returns the number of elements currently in this queue. * @return the size of the queue */ public int size() { return count; } public int length() { return queue.length; }
/** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { String result = "";
for(int i = front; i
public void list() { if (isEmpty()) { System.out.println("Nothing because the queue is empty. Please enqueue at least 1 element first."); } else { System.out.println(this.toString()); } } }
QueueADT.java
package jsjf;
public interface QueueADT
public T dequeue();
public T first();
public boolean isEmpty();
public int size(); public int length();
public String toString(); public void list(); }
EmptyCollectionException.java
package jsjf.exceptions;
public class EmptyCollectionException extends RuntimeException { public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } }
CQDriver.java
package jsjf;
import java.util.Scanner;
public class CQDriver { public static void main(String[] args) { Scanner input=new Scanner(System.in); CircularArrayQueue
System.out.print("Enter your choice: "); menu=Integer.parseInt(input.next()); switch(menu) { case 1: System.out.print("Enter element: "); String element=input.next(); queue.enqueue(element); break; case 2: System.out.println("Popped element is: "+queue.dequeue()); break; case 3: System.out.println("Top element is: "+queue.first()); break; case 4: System.out.println("Stack elements are: "); queue.list(); break; case 5: System.out.println("Current queue holds "+queue.size()); System.out.println("Current queue size is "+queue.length()); break; } } while(menu!=6); //iterates until 6 (Exit) is entered input.close(); } }
Example
Queue menu selection 1. Enqueue 2.Dequeue 3. First |4.List 15.Size 8.Exit Enter your choice: 1 Enter element: a Queue menu selection 1.Enqueue 12.De queue |3.First|4.List 15.Size 16.Exit Enter your choice: 1 Enter element: b Queue menu selection 1.Enqueue 12.De queue 13. First|4.List 15.Size 18.Exit Enter your choice: 1 Enter element: C Queue menu selection 1.Enqueue 12.De queue 19. l'inst 4.List |5.3ize 16.Ex it Enter your choice: 4 Stack elements are: Queue menu selection 1. Enqueue 2.Dequeue 3. First |4.List 15.Size 8.Exit Enter your choice: 1 Enter element: d Queue menu selection 1.Enqueue 12.De queue |3.First|4.List 15.Size 16.Exit Enter your choice: 4 Stack elements are: abcd Queue menu selection 1.Enqueue 12.De queue 13. First|4.List 15.Size 18.Exit Enter your choice: 5 BUILD SUCCESSFUL (total time: 53 seconds)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started