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

Objective: The goal of this assignment is to practice Queue operations. Assignme

ID: 3704983 • Letter: O

Question

Objective: The goal of this assignment is to practice Queue operations.

Assignment: The following class is written for a standard array-based Queue, which we went over during the lecture session (or will go over soon). The Queue class from the lecture is copied below.

public class Queue{

private int QUEUE_SIZE = 50;

private Object[] items;

private int front, back, count;

public Queue() {

items = new Object[QUEUE_SIZE];

front = 0;

back = QUEUE_SIZE -1;

count =0;

}

public boolean isEmpty(){

return count ==0;

}

public boolean isFull(){

return count == QUEUE_SIZE;

}

public void enqueue(Object newItem){

if (!isFull()){

back = (back+1) % QUEUE_SIZE;

items[back] = newItem;

count++;

return;

}

else

System.out.println("Trying to enqueue into full queue");

}

public Object dequeue(){

if (!isEmpty()){

Object queueFront = items[front];

front = (front+1) % QUEUE_SIZE;

count--; return queueFront;

}

else

System.out.println("Trying to dequeue from empty queue");

return null;

}

public void dequeueAll(){

items = new Object[QUEUE_SIZE];

front = 0;

back = QUEUE_SIZE -1;

count =0;

}

public Object peek(){

if (!isEmpty()) {

return items[front];

}

else

System.out.println("Trying to peek with empty queue");

return null;

}

public int size(){

return count;

}

}// End of class Queue

Your tasks in this assignment are outlined below.

1. Change the QUEUE_SIZE=50 to QUEUE_SIZE=5 in the given code. This will make analyses of your code easier (especially for grading purpose).

2. Change the enqueue method of the Queue class in such a way that if the array is full then the array-size will become double. Obviously, the new item will be added in the expanded new array in that case. That is, enqueue will never fail due to the size-limitation of the array. For this assignment, enqueue only numbers (either int, or double). (In practice, you could enqueue any object.)

3. Write a different class named Runner.java from which you will create a queue object and demonstrate that your Queue class works. In Runner.java, in addition to the main method, write the following methods and demonstrate that these methods work too.

(a) public static void printQueue(Queue q): Print all the elements of a queue. q must have the same numbers in the same sequence after printing all the numbers.

(b) public static void findMaxInQueue(Queue q): Your queue has all numbers. Print the largest of all the number in q. The queue q must have the same numbers in the same sequence after printing the largest number.

(c) public static void reverseQueue(Queue q): Reverse the content of the queue. A sample terminal output of

Runner.java is provided below.

My queue is as follows:

10 20 30 40 50

I am going to dequeue one element.

My queue is as follows:

20 30 40 50

I am going to reverse my Queue.

My queue is as follows:

50 40 30 20

I am going to enqueue 60.

My queue is as follows:

50 40 30 20 60

I am going to enqueue 70.

Queue is full. Doubling the size.

New max. size is: 10

Entered the new item.

My queue is as follows:

50 40 30 20 60 70

I am going to reverse my Queue.

My queue is as follows:

70 60 20 30 40 50

The largest number in the queue is: 70

My queue is as follows:

70 60 20 30 40 50

Deliverables: Queue.java and Runner.java

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements.

// Queue.java

import java.util.Arrays;

public class Queue {

                private int QUEUE_SIZE = 5;

                private Object[] items;

                private int front, back, count;

                public Queue() {

                                items = new Object[QUEUE_SIZE];

                                front = 0;

                                back = QUEUE_SIZE - 1;

                                count = 0;

                }

                /**

                * defining a constructor to initialize a queue using another queue (copy constructor)

                * @param q -a Queue instance

                */

                public Queue(Queue q){

                                /**

                                * copying all the attributes to this object

                                */

                                items=q.items;

                                front=q.front;

                                back=q.back;

                                count=q.count;

                                QUEUE_SIZE=q.QUEUE_SIZE;

                }

                public boolean isEmpty() {

                                return count == 0;

                }

                public boolean isFull() {

                                return count == QUEUE_SIZE;

                }

                public void enqueue(Object newItem) {

                                if (!isFull()) {

                                                back = (back + 1) % QUEUE_SIZE;

                                                items[back] = newItem;

                                                count++;

                                                return;

                                }

                                else {

                                                System.out.println("Queue is full. Doubling the size.");

                                                /**

                                                * doubling the queue size

                                                */

                                                QUEUE_SIZE = QUEUE_SIZE * 2;

                                                /**

                                                * copying and expanding the old array to new array with double

                                                * capacity

                                                */

                                                items = Arrays.copyOf(items, QUEUE_SIZE);

                                                System.out.println("New max. size is: "+QUEUE_SIZE);

                                                /**

                                                * adding the element

                                                */

                                                enqueue(newItem);

                                }

                }

                public Object dequeue() {

                                if (!isEmpty()) {

                                                Object queueFront = items[front];

                                                front = (front + 1) % QUEUE_SIZE;

                                                count--;

                                                return queueFront;

                                }

                                else

                                                System.out.println("Trying to dequeue from empty queue");

                                return null;

                }

                public void dequeueAll() {

                                items = new Object[QUEUE_SIZE];

                                front = 0;

                                back = QUEUE_SIZE - 1;

                                count = 0;

                }

                public Object peek() {

                                if (!isEmpty()) {

                                                return items[front];

                                }

                                else

                                                System.out.println("Trying to peek with empty queue");

                                return null;

                }

                public int size() {

                                return count;

                }

}// End of class Queue

// Runner.java

public class Runner {

                public static void main(String[] args) {

                                /**

                                * creating a queue

                                */

                                Queue queue = new Queue();

                                /**

                                * adding some elements

                                */

                                queue.enqueue(10);

                                queue.enqueue(20);

                                queue.enqueue(30);

                                queue.enqueue(40);

                                queue.enqueue(50);

                                /**

                                * displaying the queue

                                */

                                printQueue(queue);

                                System.out.println("I am going to dequeue one element");

                                queue.dequeue();//dequeuing one element

                                printQueue(queue);

                                System.out.println("I am going to reverse the queue");

                                reverseQueue(queue);//reversing the queue

                                printQueue(queue);

                                System.out.println("I am going to enqueue 60");

                                queue.enqueue(60);

                                printQueue(queue);

                                System.out.println("I am going to enqueue 70");

                                queue.enqueue(70);

                                printQueue(queue);

                                findMaxInQueue(queue);//displaying the largest element

                }

                /**

                * method to print a queue

                */

                public static void printQueue(Queue q) {

                                System.out.println("My Queue is as follows:");

                                /**

                                * creating a copy of the queue, so that the original queue wont be

                                * affected

                                */

                                Queue tmp = new Queue(q);

                                while (!tmp.isEmpty()) {

                                                System.out.print(tmp.dequeue() + " ");

                                }

                                System.out.println();

                }

                /**

                * method to print the largest element of a queue

                */

                public static void findMaxInQueue(Queue q) {

                                /**

                                * creating a copy of the queue, so that the original queue wont be

                                * affected

                                */

                                Queue tmp = new Queue(q);

                                if (tmp.isEmpty()) {

                                                System.out.println("Queue doesn't have any elements");

                                } else {

                                                int max = (Integer) tmp.dequeue();

                                                while (!tmp.isEmpty()) {

                                                                int num = (Integer) tmp.dequeue();

                                                                if (num > max) {

                                                                                max = num;

                                                                }

                                                }

                                                System.out.println("The largest number in the queue is: " + max);

                                }

                }

                /**

                * method to reverse a queue

                */

                public static void reverseQueue(Queue q) {

                               

                                /**

                                * creating an array with queue's size to store the dequeued elements

                                */

                                int reversed[] = new int[q.size()];

                                for (int i = 0; i < reversed.length; i++) {

                                                /**

                                                * dequeuing and adding to the array

                                                */

                                                reversed[i] = (Integer) q.dequeue();

                                }

                                /**

                                * enqueueing the elements in reverse order

                                */

                                for (int i = reversed.length - 1; i >= 0; i--) {

                                                q.enqueue(reversed[i]);

                                }

                }

}

/*OUTPUT*/

My Queue is as follows:

10 20 30 40 50

I am going to dequeue one element

My Queue is as follows:

20 30 40 50

I am going to reverse the queue

My Queue is as follows:

50 40 30 20

I am going to enqueue 60

My Queue is as follows:

50 40 30 20 60

I am going to enqueue 70

Queue is full. Doubling the size.

New max. size is: 10

My Queue is as follows:

50 40 30 20 60 70

The largest number in the queue is: 70