COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Queues My Queue Program
QueueInterface.java
MyArrayListQueue.java
MyArrayQueue.java
MyQueueProjectClient.java
EmptyQueueException.java
FullQueueException.java
Repeating Key Program



MyQueue Program


Description

This program builds two generic queue data structures: ArrayList based and array based.

Associated Concepts:

Queue

Algorithm:

QueueInterface Java interface

It defines a Java interface QueueInterface which includes five abstract methods.

enQueue(), deQueue(), peek(), size(), and isEmpty().

MyArrayListQueue class

It implements QueueInterface.

Three private data members:

A generic ArrayList object to store objects.

Two integer variables called front and rear to indicate the front and rear of the queue.

Constructor:

Initialize the data members:

- Create the generic ArrayList object.

- Set the variable front and rear to 0.

size(), isEmpty(), enQueue(), peek(), deQueue(), and toString() methods.

size() method

Return the number of objects in the queue.

isEmpty() method

Return true if the queue is empty; false otherwise.

void enQueue(E object) method

1. Add the object at the rear index of the queue.

2. Increment the index rear by one.

E peep() method

1. Check for the empty queue.

2. Retrieve and return the front object.

E deQueue() method

1. Check for the empty queue.

2. Decrement the index rear by 1.

3. Remove and return the front object.

String toString() method

Print the queue.

MyArrayQueue class

It implements QueueInterface.

Three private data members:

A generic array to store 16 objects.

Two integer variables called front and rear to indicate the front and rear of the queue.

Constructor:

Initialize the data members:

- Create the generic array.

- Set the variable front and rear to 0.

size(), isEmpty(), enQueue(), peek(), deQueue(), and toString() methods.

size() method

Return the number of objects in the queue.

isEmpty() method

Return true if the queue is empty; false otherwise.

void enQueue(E object) method

1. Check for the full queue.

2. Add the object at the rear index of the queue.

3. Increment the index rear by one.

E peep() method

1. Check for the empty queue.

2. Retrieve and return the front object.

E deQueue() method

1. Check for the empty queue.

2. Decrement the index rear by 1.

3. Remove and return the front object.

String toString() method

1. Declare a string to store the result.

2. In a loop, retrieve each object in the queue and add it to the result.

3. Return the result after the loop is ended.

Client program

Create a MyArrayListQueue or MyArrayQueue object.

Call the methods defined in the MyArrayListQueue or MyArrayQueue class to test them.

EmptyQueueException class

Extend the RuntimeException class.

Invoke the superclass' constructor in the constructor.

FullQueueException class

Extend the RuntimeException class.

Invoke the superclass' constructor in the constructor.

Source Code

QueueInterface class



MyArrayListQueue class



MyArrayQueue class



Client program



EmptyQueueException class



FullQueueException class



Sample Run



Video