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

  • Write a Client class with a main method that tests the data structures as follows: For...

    Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...

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

  • Write a generic array list class. Task Description Your goal for this lab is to write...

    Write a generic array list class. Task Description Your goal for this lab is to write a generic ArrayList class similar to the one in the given lecture notes on array lists. The ArrayList Class The public constructors and methods required for the ArrayList class are listed here. The type E is the generic type of an element of the list. ArrayList() Construct an empty ArrayList object. int size() Return the size (number of items) in this ArrayList. boolean isEmpty()...

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

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

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

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