// Copyright (c) 1996, 1997, 1998 Bruno R. Preiss, P.Eng. // $Author: brpreiss $ // $Date: 1998/12/14 02:52:01 $ // $RCSfile: Demo2.java,v $ // $Revision: 3.4 $ package Opus5; /** * Demonstration program that tests the following classes: * StackAsArray, * StackAsLinkedList, * QueueAsArray, * QueueAsLinkedList, * DequeAsArray, and * DequeAsLinkedList. * @see StackAsArray * @see StackAsLinkedList * @see QueueAsArray * @see QueueAsLinkedList * @see DequeAsArray * @see DequeAsLinkedList * @author Bruno R. Preiss, P.Eng. * @version $Id: Demo2.java,v 3.4 1998/12/14 02:52:01 brpreiss Exp $ **/ public class Demo2 { //[ final String copyright = "@(#) Copyright (c) 1998 by Bruno R. Preiss, P.Eng."; //] /** * Performs various tests on the specified stack. * @param stack The stack to be tested. **/ private static void testStack (Stack stack) { for (int i = 0; i < 6; ++i) { if (stack.isFull ()) break; stack.push (new Association (new Int (i), new Int (i))); } Terminal.out.println (stack); while (!stack.isEmpty ()) { Object obj = stack. pop (); Terminal.out.println (obj); } } /** * Performs various tests on the specified queue. * @param queue The queue to be tested. **/ private static void testQueue (Queue queue) { for (int i = 0; i < 6; ++i) { if (queue.isFull ()) break; queue.enqueue (new Association (new Int (i), new Int (i))); } Terminal.out.println (queue); while (!queue.isEmpty ()) { Object obj = queue. dequeue (); Terminal.out.println (obj); } } /** * Performs various tests on the specified deque. * @param deque The deque to be tested. **/ private static void testDeque (Deque deque) { for (int i = 0; i < 6; ++i) { if (deque.isFull ()) break; deque.enqueueHead (new Association (new Int (i++), new Int (i++))); if (deque.isFull ()) break; deque.enqueueTail (new Association (new Int (i++), new Int (i++))); } Terminal.out.println (deque); while (!deque.isEmpty ()) { Object obj = deque. dequeueHead (); Terminal.out.println (obj); if (deque.isEmpty ()) break; obj = deque. dequeueTail (); Terminal.out.println (obj); } } /** * The (static) main program. * Invokes various test routines. * @param args The command-line arguments. Ignored. **/ public static void main (String[] args) { Terminal.out.println ("StackAsArray Test"); Stack stack1 = new StackAsArray (5); testStack (stack1); Terminal.out.println ("StackAsLinkedList Test"); Stack stack2 = new StackAsLinkedList (); testStack (stack2); Terminal.out.println ("QueueAsArray Test"); Queue queue1 = new QueueAsArray (5); testQueue (queue1); Terminal.out.println ("QueueAsLinkedList Test"); Queue queue2 = new QueueAsLinkedList (); testQueue (queue2); Terminal.out.println ("DequeAsArray Test"); Deque deque1 = new DequeAsArray (5); testQueue ((Queue) deque1); testDeque (deque1); Terminal.out.println ("DequeAsLinkedList Test"); Deque deque2 = new DequeAsLinkedList (); testQueue ((Queue) deque2); testDeque (deque2); } }