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

Build and use your own minimal queue class using an array of type String of fixe

ID: 3806662 • Letter: B

Question

Build and use your own minimal queue class using an array of type String of fixed size. to hold the queue. The array should have the default size of 5. The array size should be setable via a constructor. The following methods must be implemented: enqueue – inserts a value at the rear of the queue dequeue – returns the value at the front of the queue, removing the value from the queue isEmpty – returns true if the queue is empty, false otherwise isFull - – returns true if the queue is full, false otherwise

Tip: In a queue of 2 or more elements, the index of the rear is “larger” than the index of the front – EXCEPT, the queue will need to “wrap around” the array. For example: If the array is 20 elements. the queue front is at index 15 and the queue rear is at index 19, the next enqueue will be at index 20, which doesn't exist. The index must “wrap around” the array, i. e. the next enqueue will be at index 0. The solution is to modulus by the array size whenever increasing an index, either rear of front. For example, to change the index of the rear to the next index,use the formula rear = (rear + 1) % arraySize;

Please notice that this must be done using an array not an arraylist or any other list, I think im close but am having trouble with a couple things, Thanks

Explanation / Answer

MinimalQueue.java :

__________________

public class MinimalQueue {

String arr[] = new String[5];

int front = -1;

int rear = -1;

public MinimalQueue(int size){

arr = new String[size];

}

public void enqueue(String element){

rear = (rear + 1) % arr.length;

arr[rear] = element;

if(front == -1)

front = 0;

}

public String dequeue(){

String element = arr[front];

arr[front] = null;

if(front == rear)

front = rear = -1;

else

front = (front+1)%arr.length;

return element;

}

public boolean isEmpty(){

if(front== -1){

return true;

}

else

return false;

}

public boolean isFull(){

if(rear == arr.length-1)

return true;

else

return false;

}

public void printQueue(){

for(int i=front;i<=rear;i++)

System.out.println(arr[i]);

}

}

MinimalQueueTesting.java :

________________________

public class MinimalQueueTesting {

public static void main(String[] args) {

MinimalQueue queue = new MinimalQueue(5);

System.out.println("Checking queue is empty or not:"+queue.isEmpty());

queue.enqueue("lakhman");

queue.enqueue("mahesh");

queue.enqueue("manoj");

System.out.println("Queue: ");

queue.printQueue();

System.out.println(queue.dequeue()+"is deleted from queue");

System.out.println(" Queue: ");

queue.enqueue("venky");

queue.printQueue();

System.out.println("Checking queue is full or not:"+queue.isFull());

System.out.println("Queue: ");

queue.enqueue("mohan");

queue.printQueue();

}

}

Sample Input and Output:

______________________

Checking queue is empty or not:true //queue is empty here

Queue:

lakhman

mahesh

manoj

lakhman is deleted from queue

Queue:

mahesh

manoj

venky

Checking queue is full or not:false //queue is not full

Queue:

mahesh

manoj

venky

mohan

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote