Q:

(The Queue class) Section 10.6 gives a class for Stack. 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

0

(The Queue class) Section 10.6 gives a class for Stack. 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 default capacity 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.

Draw an UML diagram for the class. Implement the class with the initial array size set to 8. The array size will be doubled once the number of the elements exceeds the size. After an element is removed from the beginning of the array, you need to shift all elements in the array one position the left. Write a test program that adds 20 numbers from 1 to 20 into the queue and removes these numbers and displays them.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (The Queue class) Section 10.6 gives a class for Stack. 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 default capacity 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.                     *
*                                                                                *
* Draw an UML diagram for the class. Implement the class with the initial array  *
* size set to 8. The array size will be doubled once the number of the elements  *
* exceeds the size. After an element is removed from the beginning of the array, *
* you need to shift all elements in the array one position the left. Write a     *
* test program that adds 20 numbers from 1 to 20 into the queue and removes      *
* these numbers and displays them.                                               *
*********************************************************************************/
public class Exercise_10_10 {
	/** Main method */
	public static void main(String[] args) {
		// Create a Queue object
		Queue queue = new Queue();

		// Adds 20 numbers from 1 to 20 into the queue
		for (int i = 1; i <= 20; i++) {
			queue.enqueue(i);
		}

		// Removes these numbers and displays them
		while (!queue.empty()) {
			System.out.print(queue.dequeue() + " ");
		}
		System.out.println();
	}
}

Queue.java

/*******************
*      Queue       *
*------------------*
* -elements: int[] *
* -size: int       *
* Queue()          *
* enqueue(v: int)  *
* dequeue(): int   *
* empty(): boolean *
* getSize(): int   *
*******************/

// Implement Queue class
public class Queue {
	// Data fields
	private int[] elements;
	private int size;
	public static final int DEFAULT_CAPACITY = 8;

	/** Construct a Queue object with default capacity */
	Queue() {
		elements = new int[DEFAULT_CAPACITY];
	}

	/** Adds v into the queue */
	public void enqueue(int v) {
		if (size >= elements.length) {
			int[] temp = new int[elements.length * 2];
			System.arraycopy(elements, 0, temp, 0, elements.length);
			elements = temp;
		}
		elements[size++] = v;
	}

	/** Removes and returs the element from the queue */
	public int dequeue() {
		int v = elements[0];
		int[] temp = new int[elements.length];
		System.arraycopy(elements, 1, temp, 0, size);
		elements = temp;
		size--;
		return v;
	}

	/** Return true if queue is empty */
	public boolean empty() {
		return size == 0;
	}

	/** Return size */
	public int getSize() {
		return size;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now