Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#5 (use QUEUE ADT - 20 pts) (Chapter 4) // author files: ArrayBoundedQueue, QueueInterface, QueueOverflowException, QueueUnderflowException // INPUT: Take from the sorted list you created

#5 (use QUEUE ADT - 20 pts) (Chapter 4) // author files: ArrayBoundedQueue, QueueInterface, QueueOverflowException, QueueUnderflowException // INPUT: Take from the sorted list you created in #4 (so it will be alphabetical) and present it to the user; // ask user to identify the order in which they want to visit the travel destinations. // Save the order of each travel destination by enqueueing the NAME(not index) of each travel destination // into a QUEUE in the given order (of course, you MUST use the author's QUEUE code). // OUTPUT: PRINT the contents of the queue as you dequeue each element // so that what gets printed out are the travel destinations in the order the user wants to visit them. // ------------------------------------------------------------------------------------------------------

#4

Enter Location #1: London Enter Location #2: Paris Enter Location #3: Rome Enter Location #4: New York Enter Location #5: California Enter Location #6: Barcelona Content of the list Barcelona California London New York Paris Rome

//--------------------------------------------------------------------------- // ArrayBoundedQueue.java by Dale/Joyce/Weems Chapter 4 // // Implements QueueInterface with an array to hold the queue elements. // // Two constructors are provided: one that creates a queue of a default // capacity and one that allows the calling program to specify the capacity. //--------------------------------------------------------------------------- //package ch04.queues; publicclass ArrayBoundedQueueimplements QueueInterface { protectedfinalint DEFCAP = 100;// default capacity protected T[] elements; // array that holds queue elements protectedint numElements = 0; // number of elements in this queue protectedint front = 0; // index of front of queue protectedint rear; // index of rear of queue public ArrayBoundedQueue() { elements = (T[])new Object[DEFCAP]; rear = DEFCAP - 1; } public ArrayBoundedQueue(int maxSize) { elements = (T[])new Object[maxSize]; rear = maxSize - 1; } publicvoid enqueue(T element) // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. { if (isFull()) thrownew QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % elements.length; elements[rear] = element; numElements = numElements + 1; } } public T dequeue() // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. { if (isEmpty()) thrownew QueueUnderflowException("Dequeue attempted on empty queue."); else { T toReturn = elements[front]; elements[front] =null; front = (front + 1) % elements.length; numElements = numElements - 1; return toReturn; } } publicboolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { return (numElements == 0); } publicboolean isFull() // Returns true if this queue is full; otherwise, returns false. { return (numElements == elements.length); } publicint size() // Returns the number of elements in this queue. { return numElements; } }

//---------------------------------------------------------------------------- // QueueInterface.java by Dale/Joyce/Weems Chapter 4 // // Interface for a class that implements a queue of T. // A queue is a "first in, first out" structure. //---------------------------------------------------------------------------- //package ch04.queues; publicinterface QueueInterface { void enqueue(T element)throws QueueOverflowException; // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. T dequeue()throws QueueUnderflowException; // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. boolean isFull(); // Returns true if this queue is full; otherwise, returns false. boolean isEmpty(); // Returns true if this queue is empty; otherwise, returns false. int size(); // Returns the number of elements in this queue. } //package ch04.queues; publicclass QueueOverflowExceptionextends RuntimeException { public QueueOverflowException() { super(); } public QueueOverflowException(String message) { super(message); } }

//package ch04.queues; publicclass QueueUnderflowExceptionextends RuntimeException { public QueueUnderflowException() { super(); } public QueueUnderflowException(String message) { super(message); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions