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

Design a class named Queue for storing integers. Like a stack, a queue holds ele

ID: 3939772 • Letter: D

Question

Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retrieved in a last-in first-out fashion. In a queue, the elements are retrieved in a first-in first-out fashion. The class contains:

An int[] data field named elements that stores the int values in the queue.

A data field named size that stores the number of elements in the queue.

A constructor that creates a Queue object with a default capacity of 8.

The method enqueue(int v) that adds v into the queue.

The method dequeue() that removes and returns the element from the queue.

The method empty() that returns true if the queue is empty.

The method getSize() that returns the size of the queue.

How to draw the UML diagram for the class.

Explanation / Answer

public class Test

{

    public static void main(String[] args) {

        Queue queue = new Queue();

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

            queue.enqueue(i);

        }

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

            System.out.printf("%2d ", queue.dequeue());

            if ((i + 1) % 5 == 0) System.out.printf(" ");

        }

    }

}

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