X1201: Test indexOf of a List

Use this interface definition to solve this exercise.

x
 
1
public interface ListADT<E> {
2
   // returns number of elements stored
3
   public int size();
4
5
   // removes all elements
6
   public void clear();
7
8
   // return true if list is empty, false otherwise
9
   public boolean isEmpty();
10
11
   // adds e at the end of the list
12
   public boolean add(E e);
13
14
   // returns true if o is in the list, false otherwise
15
   public boolean contains(Object o);
16
17
   // returns the object at index, returns null if
18
   // index is invalid
19
   public E get(int index);
20
21
   // returns the index of o if it exists, -1 otherwise
22
   public int indexOf(Object o);
23
}

Write a test method to test the indexOf() of List<String>. The test infrastructure has an variable named instance of type List<String>. When your test is run, this object has just been allocated. Write at least 3 asserts to test the indexOf() of the instance object. Add some objects, check the indexOf() the objects added as well as some objects not in the list.

Your Answer:

xxxxxxxxxx
8
 
1
void testIndexOf()
2
{
3
    // first simple test
4
    assertNotNull(instance);
5
    // write three more tests below to test indexOf()
6
    
7
}
8

Feedback

Your feedback will appear here when you check your answer.