Question

QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueuE element() Retrieves, but does not remove, the head of the queue. Throws No Such ElementException - if the queue is empty. bOUTPUT: Array resized to 10 Array resized to 20 Array resized to 40 Array resized to 80 Array resized to 160 Array resized to

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi. I have answered this same question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// QueueBox.java

import java.util.NoSuchElementException;

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;

    // method to add an element to the rear end of the queue

    public boolean add(E value) {

        // resizing array if full

        if (count == elements.length) {

             resize(elements.length * 2);

        }

        // adding value to index rear_idx

        elements[rear_idx] = value;

        // updating rear_idx, wrapping around from the beginning if necessary

        rear_idx = (rear_idx + 1) % elements.length;

        // updating count

        count++;

        return true;

    }

    // private method to resize the array when full

    private void resize(int newCap) {

        // creating a new array

        E[] newArr = (E[]) new Object[newCap];

        int i = front_idx;

        int index = 0;

        // copying elements from old array to new

        while (index < count) {

             newArr[index] = elements[i];

             // moving to next index, wrapping around from 0 if go out of range

             i = (i + 1) % elements.length;

             index++;

        }

        // replacing old array with new, updating front and back indices

        elements = newArr;

        front_idx = 0;

        rear_idx = index;

        // displaying a message letting user know that array has been resized

        System.out.println("Array resized to " + newCap);

    }

   

    //removes and returns the top element

    public E remove() {

        if (isEmpty()) {

             // empty

             throw new NoSuchElementException();

        }

        // taking element at front

        E elem = elements[front_idx];

        // updating front index, wrapping around from the beginning if necessary

        front_idx = (front_idx + 1) % elements.length;

        //updating count and returning item

        count--;

        return elem; // returning removed element

    }

   

    //returns the element without removing

    public E element() {

        if (isEmpty()) {

             // empty

             throw new NoSuchElementException();

        }

        // element at front index

        return elements[front_idx];

    }

   

    // returns true if queue is empty

    public boolean isEmpty() {

        return count == 0;

    }

   

    //returns the current number of elements in queue

    public int size() {

        return count;

    }

}

// QueueBoxDriver.java

public class QueueBoxDriver {

    public static void main(String[] args) {

        // creating a QueueBox of integers

        QueueBox<Integer> que = new QueueBox<Integer>();

        // adding numbers between 0 and 59999

        for (int i = 0; i < 60000; i++) {

             que.add(i);

        }

        // removing first 50000 numbers

        for (int i = 0; i < 50000; i++) {

             que.remove();

        }

        // printing the rest. it should be values between 50000 and 59999

        while (!que.isEmpty()) {

            System.out.println(que.remove());

        }

    }

}

/*OUTPUT (partial)*/

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

50007

50008

50009

50010

50011

50012

50013

50014

50015

50016

50017

50018

50019

50020

50021

50022

50023

50024

50025

50026

50027

50028

50029

50030

59964

59965

59966

59967

59968

59969

59970

59971

59972

59973

59974

59975

59976

59977

59978

59979

59980

59981

59982

59983

59984

59985

59986

59987

59988

59989

59990

59991

59992

59993

59994

59995

59996

59997

59998

59999

Add a comment
Know the answer?
Add Answer to:
QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

  • Build and use your own minimal queue class using an array of type String of fixed...

    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...

  • I just need a java mehod that follows the Javadocs Implented using an arraylist. public class...

    I just need a java mehod that follows the Javadocs Implented using an arraylist. public class WorkAheadQueue<T> implements WorkAheadQueueADT<T> {     private LinearNode<T> front;     private LinearNode<T> back;     private int numNodes;     private ArrayList<LinearNode<T>> frontFive; Removes and returns the element that is at place x in the queue. Precondition: x must be less than 5, x must be less than size * Note: indexing from 0: 0-front element, I =-second element, etc. eparam x the passed in index of...

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT