20162316刘诚昊 用数组实现循环队列

20162316刘诚昊 2017-2018-2 《Java程序设计》用数组实现循环队列

实验要求

1 参考程序15.6给出方法deque,first,isEmpty,size,toString的定义,完成CireclularArrayQueue类并用Junit进行单元测试(正常,异常,边界情况)
2 提交测试代码运行截图,要全屏,包含自己的学号信息
3课下把代码推送到代码托管平台

过程

1.完成书上的代码:

import Week_5.EmptyCollectionException;

public class CircularArrayQueue<T> implements QueueADT<T>
{
    private final int DEFAULT_CAPACITY = 10;
    private int front, rear, count;
    private T[] queue;

    /*
     * Creates an empty queue using the default capacity.
     */
    public CircularArrayQueue()
    {
        front = rear = count = 0;
        queue = (T[]) (new Object[DEFAULT_CAPACITY]);
    }

    /**

     * Adds the specified element to the rear of this queue, expanding

     * the capacity of the queue array if necessary.

     */
    public void enqueue (T element)
    {
        if (size() == queue.length)
            expandCapacity();

        queue[rear] = element;
        rear = (rear+1) % queue.length;

        count++;
    }

    /**
     * Removes the element at the front of this queue and returns a
     * reference to it. Throws an EmptyCollectionException if the
     * queue is empty.
     */
    public T dequeue() throws EmptyCollectionException
    {
        if (isEmpty())
            throw new EmptyCollectionException ("queue");

        T result = queue[front];
        queue[front] = null;
        front = (front+1) % queue.length;

        count--;

        return result;
    }

    /*
     * Returns a reference to the element at the front of this queue.
     * The element is not removed from the queue.  Throws an
     * EmptyCollectionException if the queue is empty.
     */
    public T first() throws EmptyCollectionException
    {
        if (isEmpty())
            throw new EmptyCollectionException ("queue");

        return queue[front];
    }


    public boolean isEmpty()
    {
        return (count == 0);
    }


    public int size()
    {
        return count;
    }


    public String toString()
    {
        String result = "";


        for(int num = 0; num<count; num++){
            result = result + queue[(front+num)%queue.length] + " ";
        }


        return result;
    }

    /**

     * Creates a new array to store the contents of this queue with

     * twice the capacity of the old one.

     */
    public void expandCapacity()
    {
        T[] larger = (T[])(new Object[queue.length *2]);

        for(int scan=0; scan < count; scan++)
        {
            larger[scan] = queue[front];
            front=(front+1) % queue.length;
        }

        front = 0;
        rear = count;
        queue = larger;
    }
}

2.测试:

public class CircularArrayQueueTest extends TestCase {
    CircularArrayQueue CAQ = new CircularArrayQueue();
    public void testDequeue() throws Exception {
        CAQ.enqueue(01);
        CAQ.enqueue(02);
        assertEquals(01,CAQ.dequeue());
        assertEquals(02,CAQ.dequeue());
    }

    public void testFirst() throws Exception {
        CAQ.enqueue(01);
        CAQ.enqueue(02);
        assertEquals(01,CAQ.first());
    }

    public void testIsEmpty() throws Exception {
        assertEquals(true,CAQ.isEmpty());
        CAQ.enqueue(01);
        assertEquals(false,CAQ.isEmpty());
    }

    public void testSize() throws Exception {
        CAQ.enqueue(01);
        CAQ.enqueue(02);
        CAQ.enqueue(03);
        CAQ.enqueue(04);
        assertEquals(4,CAQ.size());
    }

    public void testToString() throws Exception {
        CAQ.enqueue(01);
        CAQ.enqueue(02);
        CAQ.enqueue(03);
        CAQ.enqueue(04);
        assertEquals(1+ " " + 2+" " +3+" " +4 + " ",CAQ.toString()
        );
    }

}

posted @ 2017-10-22 15:15  20162316刘诚昊  阅读(205)  评论(0编辑  收藏  举报