Please send questions to
st10@humboldt.edu .
/* Cs132AbstractList.java 1.2 (abstract class) */
/* by Sharon Tuttle */
/* last modified: 3-7-03 */
/* (added this opening comment!) */
public abstract class Cs132AbstractList implements Cs132List
{
// text thinks it is important that this be here...
public Cs132AbstractList()
{
// does nothing...?
}
// Purpose: returns true iff list has no elements
public boolean isEmpty()
{
return (this.size() == 0);
}
// NOTE that any concrete class of this must implement size() ---
// how to do THAT *is* implementation-dependent;
// Purpose: add given data newElement to beginning of list.
public void addFirst(Object newElement)
{
// add() CAN throw a ListIndexOutOfBoundsException ---
// BUT I know that cannot occur when adding a 1st element;
// so I'll write a "null" catch.
try
{
this.add(0, newElement);
}
catch (Exception ex) {}
}
// Purpose: add given data newElement to end of list.
public void addLast(Object newElement)
{
// again, need a try because add CAN throw a
// ListIndexOutOfBounds exception --- which it CANNOT
// do as called here. So, again, I have a null catch.
try
{
this.add( this.size(), newElement );
}
catch(Exception ex){}
}
// NOTE that our add() is implementation-dependent, however;
// Purpose: IF list is not empty, return FIRST element in list.
// If it IS empty, throw a ListEmptyException
public Object getFirst() throws ListEmptyException
{
Object gottenObj = null;
// complain if called for an empty list
if (this.size() == 0)
{
throw new ListEmptyException();
}
// if get here, it is safe to get the first element.
// get() can throw a ListIndexOutOfBoundsException, too ---
// but I know I am in-range, so I'll use an empty catch.
try
{
gottenObj = this.get(0);
}
catch (Exception ex) {}
return gottenObj;
}
// Purpose: IF the list is not empty, return the LAST element in list.
// If it IS empty, throw a ListEmptyException
public Object getLast() throws ListEmptyException
{
Object gottenObj = null;
// if list is empty, complain
if (this.size() == 0)
{
throw new ListEmptyException();
}
// if get here, I know I am calling get() appropriately ---
// null catch will suffice.
try
{
gottenObj = this.get( this.size() - 1 );
}
catch(Exception ex) {}
return gottenObj;
}
// NOTE that our get() is implementation-dependent, too
// NOTE that our remove() is implementation-dependent, too
// Purpose: empties the list
public void clear()
{
while (this.size() > 0)
{
// again, although remove() can throw a ListEmptyException
// or a ListIndexOutOfBoundsException, it cannot as called
// here. Thus, the empty catch.
try
{
Object ignoreMe = this.remove(0);
}
catch (Exception ex) {}
}
}
}