import list.*; /** * A simple test program for AList and its subclasses. * * @author Dung X. Nguyen */ public class ListClient { private AList pc0 = ListFactory.Singleton.makeEmptyList(); private AList pc1 = ListFactory.Singleton.makeNEList (new Integer (-1), pc0); private AList p1 = ListFactory.Singleton.makeNEList ("-5", pc0); // NOTE: "-5" is a String, not an Integer! private AList p2 = ListFactory.Singleton.makeNEList (new Integer (2), p1); private AList p3 = ListFactory.Singleton.makeNEList (new Integer (-4), p2); private AList p4 = ListFactory.Singleton.makeNEList (new Integer (3), pc1); public static void main (String[] args) { ListClient client = new ListClient (); // uses default constructor! client.testToString (); client.testMakeClone (); client.testNthElement (); client.testLastElement (); client.testFirstElements (); client.testReverse (); client.testAppend (); } private void testToString() { System.out.println ("\nTesting toString(): \n"); System.out.println ("EmptyList = " + pc0 ); System.out.println ("(-1) = " + pc1); System.out.println ("(3 -1) = " + p4); System.out.println ("(-5) = " + p1); System.out.println ("(2 -5) = " + p2); System.out.println ("(-4 2 -5) = " + p3); } private void testMakeClone() { System.out.println ("\nTesting makeClone(): \n"); System.out.println ("EmptyList.makeClone () = " + pc0.makeClone ()); System.out.println ("(-1).makeClone () = " + pc1.makeClone ()); System.out.println ("(3 -1).makeClone () = " + p4.makeClone ()); System.out.println ("(-4 2 -5).makeClone () = " + p3.makeClone ()); } private void testNthElement() { System.out.println ("\nTesting nthElement(): \n"); try { System.out.println ("EmptyList.nthElement (2) = " + pc0.nthElement (2)); } catch (Exception e) { System.out.println (e); } System.out.println ("(-1).nthElement (0) = " + pc1.nthElement (0)); System.out.println ("(3 -1).nthElement (1) = " + p4.nthElement (1)); System.out.println ("(-4 2 -5).nthElement (0) = " + p3.nthElement (0)); System.out.println ("(-4 2 -5).nthElement (1) = " + p3.nthElement (1)); System.out.println ("(-4 2 -5).nthElement (2) = " + p3.nthElement (2)); try { System.out.println ("(-4 2 -5).nthElement (3) = " + p3.nthElement (3)); } catch (Exception e) { System.out.println (e); } } private void testLastElement() { System.out.println ("\nTesting lastElement(): \n"); try { System.out.println ("EmptyList.lastElement () = " + pc0.lastElement ()); } catch (Exception e) { System.out.println (e); } System.out.println ("(-1).lastElement () = " + pc1.lastElement ()); System.out.println ("(3 -1).lastElement () = " + p4.lastElement ()); System.out.println ("(-4 2 -5).lastElement () = " + p3.lastElement ()); } private void testFirstElements() { System.out.println ("\nTesting firstElements(): \n"); System.out.println ("EmptyList.firstElements (2) = " + pc0.firstElements (2)); System.out.println ("(-1).firstElements (0) = " + pc1.firstElements (0)); System.out.println ("(3 -1).firstElements (1) = " + p4.firstElements (1)); System.out.println ("(-4 2 -5).firstElements (0) = " + p3.firstElements (0)); System.out.println ("(-4 2 -5).firstElements (1) = " + p3.firstElements (1)); System.out.println ("(-4 2 -5).firstElements (2) = " + p3.firstElements (2)); System.out.println ("(-4 2 -5).firstElements (3) = " + p3.firstElements (3)); System.out.println ("(-4 2 -5).firstElements (4) = " + p3.firstElements (4)); } private void testReverse() { System.out.println ("\nTesting reverse(): \n"); System.out.println ("EmptyList.reverse () = " + pc0.reverse ()); System.out.println ("(-1).reverse () = " + pc1.reverse ()); System.out.println ("(3 -1).reverse () = " + p4.reverse ()); System.out.println ("(-4 2 -5).reverse () = " + p3.reverse ()); } private void testAppend() { System.out.println ("\nTesting append(): \n"); System.out.println ("EmptyList.append (p4) = " + pc0.append (p4)); System.out.println ("(-1).append (pc0) = " + pc1.append (pc0)); System.out.println ("(3 -1).append (p3) = " + p4.append (p3)); System.out.println ("(-4 2 -5).append (p2) = " + p3.append (p2)); } }