ArrayList is not abstract and does not override abstract method contains

I keep getting this error and I have no idea what to do to fix it.

ArrayList is not abstract and does not override abstract method contains(java.lang.Object) in ListADT

====================================================

Here is the code:

import java.util.Iterator;

publicclass ArrayList<T>implements ListADT<T>

{

protectedfinalint DEFAULT_CAPACITY = 100;

privatefinalint NOT_FOUND = -1;

protectedint rear;

protected T[] list;

//--

// Creates an empty list using the default capacity.

//--

public ArrayList()

{

rear = 0;

list = (T[])(new Object[DEFAULT_CAPACITY]);

}

//--

// Creates an empty list using the specified capacity.

//--

public ArrayList (int initialCapacity)

{

rear = 0;

list = (T[])(new Object[initialCapacity]);

}

//--

// Removes and returns the last element in the list.

//--

public T removeLast ()throws EmptyCollectionException

{

T result;

if (isEmpty())

thrownew EmptyCollectionException ("list");

rear--;

result = list[rear];

list[rear] =null;

return result;

}

//--

// Removes and returns the first element in the list.

//--

public T removeFirst()throws EmptyCollectionException

{

if (isEmpty())

thrownew EmptyCollectionException ("list");

T result = list[0];

rear--;

// shift the elements

for (int scan=0; scan < rear; scan++)

list[scan] = list[scan+1];

list[rear] =null;

return result;

}

//--

// Removes and returns the specified element.

//--

public T remove (T element)

{

T result;

int index = find (element);

if (index == NOT_FOUND)

thrownew ElementNotFoundException ("list");

result = list[index];

rear--;

// shift the appropriate elements

for (int scan=index; scan < rear; scan++)

list[scan] = list[scan+1];

list[rear] =null;

return result;

}

//--

// Returns a reference to the element at the front of the list.

// The element is not removed from the list. Throws an

// EmptyCollectionException if the list is empty.

//--

public T first()throws EmptyCollectionException

{

if (isEmpty())

thrownew EmptyCollectionException ("list");

return list[0];

}

//--

// Returns a reference to the element at the rear of the list.

// The element is not removed from the list. Throws an

// EmptyCollectionException if the list is empty.

//--

public T last()throws EmptyCollectionException

{

if (isEmpty())

thrownew EmptyCollectionException ("list");

return list[rear-1];

}

//--

// Returns true if this list contains the specified element.

//--

publicboolean contains (T target)

{

return (find(target) != NOT_FOUND);

}

//--

// Returns the array index of the specified element, or the

// constant NOT_FOUND if it is not found.

//--

privateint find (T target)

{

int scan = 0, result = NOT_FOUND;

boolean found =false;

if (! isEmpty())

while (! found && scan < rear)

if (target.equals(list[scan]))

found =true;

else

scan++;

if (found)

result = scan;

return result;

}

//--

// Returns true if this list is empty and false otherwise.

//--

publicboolean isEmpty()

{

return (rear == 0);

}

//--

// Returns the number of elements currently in this list.

//--

publicint size()

{

return rear;

}

//--

// Returns an iterator for the elements currently in this list.

//--

public Iterator<T> iterator()

{

returnnew ArrayIterator<T> (list, rear);

}

//--

// Returns a string representation of this list.

//--

public String toString()

{

String result ="";

for (int scan=0; scan < rear; scan++)

result = result + list[scan].toString() +"\n";

return result;

}

//--

// Creates a new array to store the contents of the list with

// twice the capacity of the old one.

//--

protectedvoid expandCapacity()

{

T[] larger = (T[])(new Object[list.length*2]);

for (int scan=0; scan < list.length; scan++)

larger[scan] = list[scan];

list = larger;

}

}

And this is the interface:

import java.util.Iterator;

publicinterface ListADT<T>

{

// Removes and returns the first element from this list

public Object removeFirst ();

// Removes and returns the last element from this list

public Object removeLast ();

// Removes and returns the specified element from this list

public Object remove (Object element);

// Returns a reference to the first element on this list

public Object first ();

// Returns a reference to the last element on this list

public Object last ();

// Returns true if this list contains the specified target element

publicboolean contains (Object target);

// Returns true if this list contains no elements

publicboolean isEmpty();

// Returns the number of elements in this list

publicint size();

// Returns an iterator for the elements in this list

public Iterator iterator();

// Returns a string representation of this list

public String toString();

}

Message was edited by:

hughveal

[12126 byte] By [hughveala] at [2007-11-27 8:56:19]
# 1
Please tell me this is some sort of homework assignment...
cotton.ma at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 2

It means exactly what it says -- ArrayList is not abstract (so, it's not allowed to have abstract methods) and it doesn't implement a method defined in the interface that it claims to implement.

The problem, I think, is that you've defined contains() in the interface to take an Object, and not the parameterized type. In ArrayList, you've defined it to take the parameterized type. So the ArrayList implementation does not really implement the method defined by the interface. But given how you're using parameterized types, the interface is probably in error. So fix that.

By the way, it's generally a bad idea to reuse the names of well-known classes in the standard API.

paulcwa at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 3
It is for a data structures class in Java.
hughveala at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 4
The instructor asked that we use generics, which is why Object as a type for the contains method in the interface should work. Should I change it from Object to T?
hughveala at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 5

Add a method called "contains(Object)" that fits the contract specified in ListADT.

I see you have a method called "contains()" already, but the argument isn't an Object.

Change:

public boolean contains (T target)

to:

public boolean contains(Object target) {

--Edit--

Never mind, change the interface like the other person said.

Message was edited by:

myncknm

myncknma at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 6
I changed the methods from Object to T in the interface and it compiles now. Thanks!Message was edited by: hughveal
hughveala at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 7
Did you recompile the interface?
paulcwa at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 8

> The instructor asked that we use generics, which is

> why Object as a type for the contains method in the

> interface should work.

No, just the opposite. Using Object is not the same as using a parameterized type.

> Should I change it from Object

> to T?

Yes.

paulcwa at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...
# 9
Yes, I did and I am getting that error in another part of the program, but I think I know how to handle it now.
hughveala at 2007-7-12 21:18:59 > top of Java-index,Java Essentials,Java Programming...