CS 1723, A List of Comparable Items

Here are an Interface and two class files. At the end come three different mains that work with lists of ints, of doubles, and of Strings.
// List: a simple list of comparable items
public interface List { 

   // isEmpty: is the list empty? (initially true)
   public boolean isEmpty();

   // isFull: is the list full? (initially false)
   public boolean isFull();

   // add: add x to the list.  No check for duplicates
   public boolean add(Comparable x);

   // contains: is x in the list?
   public boolean contains(Comparable x);

   // size: return number of elements in the list
   public int size();

   // remove: remove the parameter
   public boolean remove(Comparable x);

   // clear: make the list empty
   public void clear();

   // removeMin: return the minimum and remove it
   public Comparable removeMin();

   // removeMax: return the minimum and remove it
   public Comparable removeMax();

} 

// CompList: a simple list of comparables public class CompList implements List { // fields private Comparable[] listArray; // the actual list private int currentSize; // current number of elements private final int INITIAL_SIZE = 6; // initial maximum size // CompList: constructor public CompList () { currentSize = 0; // not needed (already done) listArray = new Comparable[INITIAL_SIZE]; // allocate array } // isEmpty: is the list empty? (initially true) public boolean isEmpty() { return size() == 0; } // isFull: is the list full? (initially false) public boolean isFull() { return size() == listArray.length; } // add: add x to the list. No check for duplicates public boolean add(Comparable x) { if (currentSize >= listArray.length) doubleArray(); listArray[currentSize++] = x; return true; } // contains: is x in the list? public boolean contains(Comparable x) { for (int i = 0; i < currentSize; i++) if (listArray[i].compareTo(x) == 0) return true; return false; } // size: return number of elements in the list public int size() { return currentSize; } // remove: remove the parameter public boolean remove(Comparable x) { for (int i = 0; i < currentSize; i++) if (listArray[i].compareTo(x) == 0) { for (int j = i; j < currentSize - 1; j++) listArray[j] = listArray[j+1]; currentSize--; return true; } return false; } // clear: make the list empty public void clear() { currentSize = 0; } // removeMin: return the minimum and remove it public Comparable removeMin() { if (size() == 0) return null; Comparable min = listArray[0]; for (int i = 1; i < currentSize; i++) if (listArray[i].compareTo(min) < 0) min = listArray[i]; remove(min); return min; } // removeMax: return the maximum and remove it public Comparable removeMax() { if (size() == 0) return null; Comparable max = listArray[0]; for (int i = 1; i < currentSize; i++) if (listArray[i].compareTo(max) > 0) max = listArray[i]; remove(max); return max; } // doubleArray: double the size of the array private void doubleArray() { Comparable[] tempArray = new Comparable[listArray.length * 2]; for(int k = 0; k < currentSize; k++) tempArray[k] = listArray[k]; listArray = tempArray; } // dump: print data for debugging public void dump() { System.out.println("Dump of List, Current size: " + size() + ", Maximum size: " + listArray.length + ", Empty? " + isEmpty() + ", Full? " + isFull()); for (int i = 0; i < currentSize; i++) System.out.print(" " + listArray[i]); System.out.println(" End of Dump\n"); } }
Here is the source for the I/O: GetData.java.

Finally, here are the three main functions, along with sample runs:

// IntListMain: make use of the CompList class for ints
public class IntListMain { 

   public static void main(String[] args) {
      CompList list1 = new CompList();
      CompList list2 = new CompList();
      GetData getData = new GetData();
      int num;
      Integer numWrap;
      while(true) {
         num = getData.getNextInt();
         numWrap = new Integer(num);
         if (num == 0) break;
         list1.add(numWrap);
         list2.add(numWrap);
      }
      list1.dump();
      while (!list1.isEmpty())
         System.out.print(list1.removeMin() + " ");
      System.out.println();
      while (!list2.isEmpty())
         System.out.print(list2.removeMax() + " ");
      System.out.println();
   }
}
% java IntListMain
34 26 89 74 15 58 42 66 0
Dump of List, Current size: 8, Maximum size: 12, Empty? false, Full? false
 34 26 89 74 15 58 42 66  End of Dump

15 26 34 42 58 66 74 89 
89 74 66 58 42 34 26 15 

// DoubleListMain: make use of the CompList class for Doubles public class DoubleListMain { public static void main(String[] args) { CompList list1 = new CompList(); CompList list2 = new CompList(); GetData getData = new GetData(); double num; Double numWrap; while(true) { num = getData.getNextDouble(); numWrap = new Double(num); if (num == 0) break; list1.add(numWrap); list2.add(numWrap); } list1.dump(); while (!list1.isEmpty()) System.out.print(list1.removeMin() + " "); System.out.println(); while (!list2.isEmpty()) System.out.print(list2.removeMax() + " "); System.out.println(); } } % java DoubleListMain 34 26 89 74 15 58 42 66 0 Dump of List, Current size: 8, Maximum size: 12, Empty? false, Full? false 34.0 26.0 89.0 74.0 15.0 58.0 42.0 66.0 End of Dump 15.0 26.0 34.0 42.0 58.0 66.0 74.0 89.0 89.0 74.0 66.0 58.0 42.0 34.0 26.0 15.0
// StringListMain: make use of the CompList class for Strings public class StringListMain { public static void main(String[] args) { CompList list1 = new CompList(); CompList list2 = new CompList(); GetData getData = new GetData(); String item; while(true) { item = getData.getNextString(); if (item.equals("quit")) break; list1.add(item); list2.add(item); } list1.dump(); while (!list1.isEmpty()) System.out.print(list1.removeMin() + " "); System.out.println(); while (!list2.isEmpty()) System.out.print(list2.removeMax() + " "); System.out.println(); } } % java StringListMain ralph joe jim jeb jil abe fred frank quit Dump of List, Current size: 8, Maximum size: 12, Empty? false, Full? false ralph joe jim jeb jil abe fred frank End of Dump abe frank fred jeb jil jim joe ralph ralph joe jim jil jeb fred frank abe