public class SortedListIsAList extends ListReferenceBased
                               implements SortedListInterface{
  public SortedListIsAList() {
    // invokes default constructor of superclass
  }  // end default constructor

  public void sortedAdd(Comparable newItem) {
  // Adds an item to the list.
  // Precondition: None.
  // Postcondition: The item is added to the list in 
  // sorted order.
    int newPosition = locateIndex(newItem);
    add(newPosition, newItem);
  }  // end sortedAdd

  public void sortedRemove(Comparable anItem) 
                                 throws ListException {
  // Removes an item from the list.
  // Precondition: None.
  // Postcondition: The item is removed from the list
  // and the sorted order maintained.
    int position = locateIndex(anItem);
    if ((anItem.compareTo(get(position))==0)) {
      remove(position);
    }
    else {
      throw new ListException("Sorted remove failed");
    }  // end if
  }  // end sortedRemove

  public int locateIndex(Comparable anItem) {
  // Finds an item in the list.
  // Precondition: None.
  // Postcondition: If the item is in the list, its 
  // index position in the list is returned.  If the 
  // item is not in the list, the index of where it 
  // belongs in the list is returned.
    int index = 1;
    // Loop invariant: anItem belongs after all the 
    // elements up to the element referenced by index
    while ( (index <= size()) && 
            (anItem.compareTo(get(index)) > 0 ) ) {
      ++index;
    }  // end while
    return index;
  }  // end locateIndex

} // end SortedList
