/* T07n01.java -- An implementation of the CS227ListInterface using arrays * for list representation. * * This file contains: * o The CS227ListInterface definition, with 8 operations. (First * appeared in T06n01.java.) * o The CS227ArrayList class which implements CS227ListInterface. * o The T07n01 class, with a main method containing sample calls * to test constructors and the append() method. Other methods also * should be tested thoroughly, of course, but this shows how * to do it. * * Things to note: * o I opted for the "no dynamic expansion" approach, so that isFull() * would have some meaning in at least one of our examples. * o No documentation for the methods is included! Admit it; you miss it * when you don't see it. :-) * o Main's testing is pretty good for the constructor and for append(), * but doesn't exist for the others...and it should. * o When you compile this, you'll notice a warning from Java related to * the CS227ArrayList constructor. See the explanation below. */ interface CS227ListInterface { public int append (ElementType item); public int prepend (ElementType item); public int insert (int location, ElementType item); public ElementType delete (int location); public boolean isEmpty (); public boolean isFull (); public int size (); public int capacity (); public String toString (); } class CS227ArrayList implements CS227ListInterface { public static final int CAPACITY = 10; public static final int ARRAY_IS_FULL = -1; private ElementType[] list; private int occupancy; /* CS227ArrayList is a generic (a.k.a. parameterized) type that * needs an array to hold members of the list. This array * should be of the type of the parameter to the class. * The Java compiler can't accept a statement that creates * an array of references to an unspecified type, meaning * that the straightforward * list = new ElementType[CAPACITY]; * statement can't be used. Instead, we fall back on the 'old' * technique of creating an array of Object references and * cast the result to be of the unspecified type. Java will * accept this, but because it's still an unknown type, it will * warn us: * Note: T07n01.java uses unchecked or unsafe operations. * Note: Recompile with -Xlint:unchecked for details. * If you do as it suggests, you'll see a message like this: * T07n01.java:75: warning: [unchecked] unchecked cast * found : java.lang.Object[] * required: ElementType[] * list = (ElementType[]) new Object[CAPACITY]; * The compiler is telling you (indirectly) that it can't * verify that your cast makes sense because the type * ElementType is unspecified. But, it will complete the * compilation anyway, on the assumption that you know what * you're doing with that cast. */ public CS227ArrayList() { occupancy = 0; list = (ElementType[]) new Object[CAPACITY]; } // returns # of items in the list; first location is 0. public int append (ElementType item) { if (!this.isFull()) { list[occupancy++] = item; return occupancy; } else { return ARRAY_IS_FULL; } } // returns # of items in the list; first location is 0. public int prepend (ElementType item) { if (!this.isFull()) { for (int i = occupancy; i > 0; i--) { list[i] = list[i-1]; } list[0] = item; return ++occupancy; } else { return ARRAY_IS_FULL; } } // returns # of items in the list; first location is 0. public int insert (int location, ElementType item) throws IndexOutOfBoundsException { if (location < 0 || location >= CAPACITY) { // not a valid index throw new IndexOutOfBoundsException(); } else if (this.isFull()) { // no room at the inn return ARRAY_IS_FULL; } else if (location > occupancy) { // can't leave gap in list return this.append(item); } else { for (int i = occupancy; i > location; i--) { list[i] = list[i-1]; } list[location] = item; return ++occupancy; } } // returns item removed from the list, or null if nothing removed. // location starts with 0. public ElementType delete (int location) throws IndexOutOfBoundsException { if (location < 0 || location >= CAPACITY) { // not a valid index throw new IndexOutOfBoundsException(); } else if (location >= occupancy) { // nothing there! return null; } else { ElementType temp = list[location]; for (int i = location; i < occupancy-1; i++) { list[i] = list[i+1]; } occupancy--; return temp; } } public boolean isEmpty () { return (occupancy == 0); // short and sweet! No need for if stmt... // if (occupancy == 0) { return true; } else { return false; } } public boolean isFull () { return (occupancy == CAPACITY); } public int size () { return occupancy; } public int capacity () { return CAPACITY; } public String toString () { String temp = ""; if (this.isEmpty()) return "()"; for (int i=0; i aList = new CS227ArrayList(); System.out.println("\nVERIFYING CONSTRUCTOR..."); if (aList.capacity() == CS227ArrayList.CAPACITY) System.out.println("\tCapacity is correctly set."); else System.out.println("\t>>> ERROR: Capacity is not correctly set."); if (aList.size() == 0) System.out.println("\tSize is correctly set."); else System.out.println("\t>>> ERROR: Size is not correctly set."); if (aList.isEmpty()) System.out.println("\tList shows as empty."); else System.out.println("\t>>> ERROR: List should be empty."); if (!aList.isFull()) System.out.println("\tList shows as not full."); else System.out.println("\t>>> ERROR: List should be not full."); System.out.println("\nVERIFYING APPEND..."); int returnVal = aList.append("Hi"); System.out.println(" Appended first element..."); if ((aList.toString()).equals("(Hi)")) System.out.println("\tList content is correct."); else System.out.println("\t>>> ERROR: List content is not correct."); if (aList.size() == 1) System.out.println("\tSize is correctly set."); else System.out.println("\t>>> ERROR: Size is not correctly set."); if (returnVal == 1) System.out.println("\tReturn value is correct."); else System.out.println("\t>>> ERROR: Return value is not correct."); returnVal = aList.append("there"); System.out.println(" Appended second element..."); if ((aList.toString()).equals("(Hi)(there)")) System.out.println("\tList content is correct."); else System.out.println("\t>>> ERROR: Append to non-empty list: " + "List content is not correct."); if (returnVal == 2) System.out.println("\tReturn value is correct."); else System.out.println("\t>>> ERROR: Return value is not correct."); returnVal = aList.append("2"); returnVal = aList.append("3"); returnVal = aList.append("4"); returnVal = aList.append("5"); returnVal = aList.append("6"); returnVal = aList.append("7"); returnVal = aList.append("8"); returnVal = aList.append("9"); System.out.println(" Appended eight more elements..."); if ((aList.toString()).equals("(Hi)(there)(2)(3)(4)(5)(6)(7)(8)(9)")) System.out.println("\tList content is correct."); else System.out.println("\t>>> ERROR: Append fills list: " + "List content is not correct."); if (returnVal == 10) System.out.println("\tReturn value is correct."); else System.out.println("\t>>> ERROR: Return value is not correct."); returnVal = aList.append("10"); if (returnVal == CS227ArrayList.ARRAY_IS_FULL) System.out.println("\tAppend to full list return value " + "is correct."); else System.out.println("\t>>> ERROR: Append to full list return value " + "is not correct."); if ((aList.toString()).equals("(Hi)(there)(2)(3)(4)(5)(6)(7)(8)(9)")) System.out.println("\tList content is correct."); else System.out.println("\t>>> ERROR: Append fills list: " + "List content is not correct."); System.out.println("\n...AND THIS SORT OF THING SHOULD BE DONE FOR " + "THE OTHERS, TOO!"); } }