Question

In: Computer Science

QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...

QUEUEBOX:

Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueueBox:

public class QueueBox<E> {
private E[] elements = (E[])( new Object[5] );
private int front_idx = 0;
private int rear_idx = 0;
private int count = 0;
}

Hint: use the count variable to keep track of how many elements are in the queue (increment count when enqueing and decrement when dequeing). Makes it a lot easier to determine if the array is full and for the other functions as well.

Of course you may add or modify variables to this class BUT you MUST use a GENERIC array of initial size 5 and your queue MUST be a circular queue. Ensure your QueueBox can do the following:

boolean add(E e)
Adds e to the queue (enqueue). returns true. Throws NullPointerException if e is null.

E remove()
Removes element in the front of the queue (dequeue) and returns it. NoSuchElementException is thrown if this queue is empty

E element()
Retrieves, but does not remove, the head of the queue. Throws NoSuchElementException - if the queue is empty.

boolean isEmpty()
returns true if queue is empty.

int size()
returns number of elements held in the queue.

Create a driver class QueueBoxDriver that creates an instance of QueueBox<Integer> then fills it with 60,000 integers numbered from 0 to 59999 (in that order). Your QueueBox must be able to resize itself (double in size) when needed to accomodate. Everytime your queue auto-resizes it must display that it has done so (see output below). After you create your queue and populate it, dequeue the first 50000 elements.

Now dequeue the remaining 10000 elements and display them to the screen. Continue until the queue is empty. Your output should look as close as possible to this:

OUTPUT:
Array resized to 10
Array resized to 20
Array resized to 40
Array resized to 80
Array resized to 160
Array resized to 320
Array resized to 640
Array resized to 1280
Array resized to 2560
Array resized to 5120
Array resized to 10240
Array resized to 20480
Array resized to 40960
Array resized to 81920
50000
50001
50002
50003
50004
50005
50006
...

...

...
59997
59998
59999

Marking Criteria

Correct implementation of a queue with generic queue methods ... 5

Working implementation of a circular queue ... 10

Automatic resizing of the queue ... 3

Correct output ... 2

Solutions

Expert Solution

In above problem statement we are required to create and implemet a Dequeue, which is a double ended queue which has a front and a rear.

Whenever an element is suppose to be added to this queue it will be added at the end of the queue and whenever an element is poped it will be from the rear.

Lets first complete class QuueBox(refer below code snippet):



public class QueueBox<E> {
    private int QUEUE_SIZE = 5;
    private Object[] elements;
    private int front_idx, rear_idx, count;

    public QueueBox() {
        elements = new Object[QUEUE_SIZE];
        this.front_idx = 0;
        this.rear_idx = QUEUE_SIZE -1;
        this.count = 0;
    }

    //checks whether queue is empty or not
    public boolean isEmpty(){
        return count == 0;
    }

    //checks whether queue is full or not
    public boolean isFull(){
        return count == QUEUE_SIZE;
    }

    //adds an element to rear
    public void add(Object newItem) {
        if (!isFull()) {
            rear_idx = (rear_idx + 1) % QUEUE_SIZE;
            elements[rear_idx] = newItem;
            count++;
            return;
        }
        else {
            System.out.println("Queue is full. Doubling the size.");
            QUEUE_SIZE = (QUEUE_SIZE * 2); // double queue size not count 
            System.out.println("New max size is: " + QUEUE_SIZE);
            Object[] temp = new Object[QUEUE_SIZE]; // temp array
            System.arraycopy(elements, front_idx, temp, front_idx, elements.length - front_idx); // copy the elements from front_idx index to elements.length-front_idx
            if (front_idx != 0) {
                System.arraycopy(elements, 0, temp, elements.length, front_idx); // copy the elements in the range elements[0] to elements[rear_idx] into the new array
            }
            elements = temp; // set elements to temp array
            rear_idx = front_idx + count;
            elements[rear_idx] = newItem; // set new item 
            count++; // increment count
        }

    }

    //removes an element from front_idx
    public Object remove(){
        if (!isEmpty()){
            Object queuefront_idx = elements[front_idx];
            System.out.println("Element removed after DeQueuing: " + queuefront_idx.toString());
            front_idx = (front_idx+1) % QUEUE_SIZE;
            count--;
            return front_idx;
        }else
            System.out.println("Trying to dequeue from empty queue");
        return null;
    }

    //returns the head of the queue
    public Object element(){
        if (!isEmpty()) {
            return elements[front_idx];
        }
        else
            System.out.println("Trying to peek with empty queue");
        return null;
    }

    //returns size of the queue
    public int size(){
        return count;
    }

}

Now lets complete class QueueBoxDriver(refer below code snippet):



public class QueueBoxDriver {
    public static void main(String[] args) {
        QueueBox q = new QueueBox();
        createQueue(q);
        destroyQueue(q);

        while (!q.isEmpty()){
            q.remove();
        }
    }

    //adds 60,000 integers
    public static QueueBox createQueue(QueueBox q){
        for (int i=0; i<60000; i++){
            q.add(i);
        }
        return q;
    }

    public static QueueBox destroyQueue(QueueBox q){
        for (int i=0; i<50000; i++){
            q.remove();
        }
        return q;
    }
}

Related Solutions

QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...
QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueueBox: public class QueueBox<E> { private E[] elements = (E[])( new Object[5] ); private int front_idx = 0; private int rear_idx = 0; private int count = 0; } Hint: use the count variable to keep track of how many elements are in the queue (increment count when enqueing and decrement when dequeing). Makes it a lot easier to determine...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
Please enter a seed: 1 Please enter the size of the array: 1 Array size must...
Please enter a seed: 1 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 12 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements are less than half of the first element...
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
Using Selection Sort on an any array of size 7, what will be the minimum/maximum number...
Using Selection Sort on an any array of size 7, what will be the minimum/maximum number of comparisons and number of exchanges? 1 for i = 1 to A.length-1 2 for j = i+1 to A.length 3 if A[j] < A[i] 4 exchange A[i] with A[j]
A wine lover has decided to start a winery. The initial investment will be $5 million...
A wine lover has decided to start a winery. The initial investment will be $5 million on Day 1. The winery will require an additional $1 million dollars investment at very beginning Year 1. The vines will mature over five years. Beginning at the end of year 6, the winery is expected to produce net cash inflows of $2 million, 4 mil in year 7, 6 mil in year 8, 8 mil in year 9 and 10 mil in year...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT