Object Queue ADT Implentation
CS2 Course Summary Slide 24

An Object Stack ADT is presented along with a circular implementation. This is another nice example of Vector processing. Considerable class time is spent motivating circular queues and discussing standard programming conventions.


ADT

/*
 * ObjectQueueADT.java
 */

package datatypes.queue;

public interface ObjectQueueADT {
   public boolean emptyp();
   public boolean fullp();
   public Object front();
   public void add(Object data) throws ObjectQueueOverflowException;
   public Object delete() throws ObjectQueueUnderflowException;
   public String toString();    
}

Exception Classes

/*
 * ObjectQueueOverflowException.java
 */

package datatypes.queue;

public class ObjectQueueOverflowException extends java.lang.Exception {
    
    public ObjectQueueOverflowException() {
    }
    
    public ObjectQueueOverflowException(String msg) {
        super(msg);
    }
}


/*
 * ObjectQueueUnderflowException.java
 */

package datatypes.queue;

public class ObjectQueueUnderflowException extends java.lang.Exception {
    
    public ObjectQueueUnderflowException() {
    }
    
    public ObjectQueueUnderflowException(String msg) {
        super(msg);
    }
}

Implementation

/*
 * ObjectQueue.java
 */

package datatypes.queue.sequential;

import datatypes.queue.*;

public class ObjectQueue implements ObjectQueueADT
{
    // instance variables
    private Object[] q;
    private int limit;
    private int front = 0;
    private int rear = 0;
    
    // constructor
    public ObjectQueue(int maxsize)
    {
        q = new Object[maxsize+1];
        limit = maxsize+1;
    }
    
    public boolean fullp()
    {
        return ( advance(rear) == front );
    }
    
    public boolean emptyp()
    {
        return ( front == rear );
    }

    public Object front()
    {
        return q[advance(front)];
    }

    private int advance(int x)
    {
        if ( x == limit-1 ) {
            return 0;
        } else {
            return x+1;
        }
    }
    
    public void add(Object data) throws ObjectQueueOverflowException
    {
        if ( fullp() ) {
            throw new ObjectQueueOverflowException();
        }
        rear = advance(rear);
        q[rear] = data;
    }
    
    public Object delete() throws ObjectQueueUnderflowException
    {
        Object result = null;
        if ( emptyp() ) {
            throw new ObjectQueueUnderflowException();
        } else {
            front = advance(front);
            result = q[front];
        }
        return result;
    }
    
    public int size()
    {
        int count = 0;
        int p = front;
        while ( p != rear ) {
            count++;
            p = advance(p);
        }
        return count;
    }
    
    public String toString()
    {
        String result = "";
        result = result + "FRONT of queue\n";   
        int p = front;
        while ( p != rear ) {
            p = advance(p);
            result = result + q[p] + "\n";
        }
        result = result + "REAR of queue";
        return result;
    }
    
}

Test Program and Demo

/*
 * ObjectQueueTest.java
 */

package testers;

import datatypes.queue.*;
import datatypes.queue.sequential.*;

class ObjectQueueTest
{
    static public void main(String[] a)
    {
        test_part1();
        test_part2();
        test_part3();
    }

    static private void test_part1()
    {
        System.out.println(">>> Testing queue constructor, add, delete, and toString");
        ObjectQueue  queue = new ObjectQueue(7);
        try {
            queue.add("A");
            queue.add("B");
            queue.add("C");
            queue.delete();
            queue.add("D");
            queue.add("E");
            queue.delete();
            queue.add("F");
            queue.add("G");
            queue.delete();
            queue.add("H");
        } catch ( ObjectQueueOverflowException e ) {
            System.out.println("Queue overflow occurred");
            display(queue);
        } catch ( ObjectQueueUnderflowException e ) {
            System.out.println("Queue overflow occurred");
            display(queue);
        }
        display(queue);
    }

    static private void test_part2()
    {
        System.out.println(">>> Testing overflow handling");
        ObjectQueue  queue = new ObjectQueue(4);
        try {
            queue.add("one");
            queue.add("two");
            queue.add("three");
            queue.add("four");
            queue.add("five");
        } catch ( ObjectQueueOverflowException e ) {
            System.out.println("Queue overflow occurred");
            display(queue);
        } 
        System.out.println(">>> Testing underflow handling");
        try {
            queue.delete();
            queue.delete();
            queue.delete();
            queue.delete();
            queue.delete();
            queue.delete();
        } catch ( ObjectQueueUnderflowException e ) {
            System.out.println("Queue underflow occurred");
            display(queue);
        }
    }

    static private void test_part3()
    {
        System.out.println(">>> Testing emptyp, fullp, and front");
        ObjectQueue  queue = new ObjectQueue(3);
        try {
            queue.add("red");
            queue.add("yellow");
            queue.add("blue");
            display(queue);
            if ( queue.emptyp() ) {
                System.out.println("queue is empty");
            } else if ( queue.fullp() ) {
                System.out.println("queue is full");
            }
            System.out.println("front = " + queue.front());
            queue.delete();
            queue.delete();
            queue.delete();
            display(queue);
            if ( queue.emptyp() ) {
                System.out.println("queue is empty");
            } else if ( queue.fullp() ) {
                System.out.println("queue is full");
            }
        } catch ( ObjectQueueOverflowException e ) {
            System.out.println("Queue overflow occurred");
            display(queue);
        } catch ( ObjectQueueUnderflowException e ) {
            System.out.println("Queue overflow occurred");
            display(queue);
        }
    }


    static private void display(ObjectQueue s)
    {
        System.out.println(s.toString());
    }
    


init:
deps-jar:
compile-single:
run-single:
>>> Testing queue constructor, add, delete, and toString
FRONT of queue
D
E
F
G
H
REAR of queue
>>> Testing overflow handling
Queue overflow occurred
FRONT of queue
one
two
three
four
REAR of queue
>>> Testing underflow handling
Queue underflow occurred
FRONT of queue
REAR of queue
>>> Testing emptyp, fullp, and front
FRONT of queue
red
yellow
blue
REAR of queue
queue is full
front = red
FRONT of queue
REAR of queue
queue is empty
BUILD SUCCESSFUL (total time: 1 second)