I would like to know how it working. Here is the question.. isnt it put the method Iterable positions();?
Please help me to figure it out.

Suppose we want to extend the PositionalList ADT with a method, indexOf(p), that returns the current index of the element stored at position p. Write this method using only other methods of the PositionalList interface (not details of our LinkedPositionalList implementation). Write the necessary code to test the method. Hint: Count the steps while traversing the list until encountering position p

public interface PositionalList<E> extends Iterable<E> {


int size();

boolean isEmpty();

Position<E> first();

Position<E> last();

Position<E> before(Position<E> p) throws IllegalArgumentException;

Position<E> after(Position<E> p) throws IllegalArgumentException;

Position<E> addFirst(E e);

Position<E> addLast(E e);

Position<E> addBefore(Position<E> p, E e)
throws IllegalArgumentException;

Position<E> addAfter(Position<E> p, E e)
throws IllegalArgumentException;

E set(Position<E> p, E e) throws IllegalArgumentException;

E remove(Position<E> p) throws IllegalArgumentException;

Iterator<E> iterator();

Iterable<Position<E>> positions();

}

I'm new around here, so I don't know how well your question fits the "show some effort" rule on assignments.

I don't see any harm in helping you with the syntax (regulars...help me out if this is wrong). You'll need a name for the derived Interface (or "ADT") and that will look something like:

public interface IndexiblePositionalList<E> extends PositionalList<E> {
    default int indexOf(E item) {
        int index;
        // insert code to search the list, using
        // only methods from PositionalList<E> and Position<E>.
        return index; // and return the resulting index
    }
}

The language in the assignment seems to be a little bit off. I don't see a way to express the answer in terms of PositionalList<E> alone. You need to use some method from Position<E> to compare the value you're searching for to the value stored at the list position.

When you want to test your code, you can use any methods you like to get test data into a LinkedPositionalList object, and then:

    LinkedPositionalList<String> theList = new LinkedPositionalList<>();
    // add strings to theList
    IndexiblePositionalList<String> indexTest = theList;
    // add code to test indexTest.indexOf() calls
commented: Good formatting. +1 +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.