Starting code for Linked List class

OurList interface that OurLinkedList implements:



/**
OurList interface is modeled after the List interface in the 
Java Collections Framework. The prefix "Our" is to avoid name 
clashes with the standard Java interfaces and classes.

Author: Elena Machkasova
Purpose: to be used in CSci 2101 UMM course
**/

public interface OurList<E>  {
    /**
     * @return true if this list contains no elements, 
     * false otherwise
     */
	public boolean isEmpty();
	
    /**
     * Returns the number of elements in this list. 
     * @return the number of elements in this list
     */
    public int size();
    
    /**
     * Inserts the specified element at the specified index in this list 
     * @param index - the index where to insert the element
     * @param item - the item to be inserted
     * @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
     */
    public void add(int index, E item) throws ListIndexOutOfBoundsException;
    
    /**
     * Returns the element at the specified position in this list.
     * @param index - the index of the element to be returned
     * @return - the element at the specified position in this list.
     * @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
     */
    public E get(int index) throws ListIndexOutOfBoundsException;
    
    /**
     * Removes the element at the specified position in this list. 
     * Shifts any subsequent elements to the left (subtracts one from their indices). 
     * @param index - the index of the element to removed.
     * @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
     */ 
    public void remove(int index) throws ListIndexOutOfBoundsException;
    
    /**
     * Removes all of the elements from this list (optional operation). 
     * This list will be empty after this call returns.
     * @return
     */
    public void clear();
	
}


ListIndexOutOfBoundsException


public class ListIndexOutOfBoundsException extends Exception {

    /**
       Constructor that sets the message
     **/
	public ListIndexOutOfBoundsException(String message) {
		// passing the message to the constructor of the superclass
		super(message);
	}
}

Testing code for OurLinkedList (we don't have the class itself yet!)


/**
The class tests methods of a linked list OurLinkedList via its 
interface OurList
**/

public class TestOurLinkedList {
	public static void main(String [] args) throws ListIndexOutOfBoundsException {
		OurList<String> strings = new OurLinkedList<String>();
		
		// the list should be empty initially
		System.out.println("A newly-created list:");
		// expected: true
		System.out.println("List isEmpty is " + strings.isEmpty());
		//expected: The size of the list is 0
		System.out.println("The size of the list is " + strings.size());
		
		strings.add(0,"hello");
		
		// the list after adding an element
		System.out.println("A list with one element:");
		// expected: false
		System.out.println("List isEmpty is "+ strings.isEmpty());
		//expected: The size of the list is 1
		System.out.println("The size of the list is " + strings.size());
		
		strings.add(1,"bye");
		strings.add(2,"greetings!");
		
		// the list after adding 3 elements
		System.out.println("A list with three elements:");
		// expected: false
		System.out.println("List isEmpty is "+ strings.isEmpty());
		//expected: The size of the list is 3
		System.out.println("The size of the list is " + strings.size());
		
		// testing add and get methods
		System.out.println("After adding three strings the list is");
		for(int i = 0; i < strings.size(); ++i) {
			String s = strings.get(i);
			System.out.println(s);
		}
		
		
		// test exception
		try {
			strings.get(55);
		} catch (ListIndexOutOfBoundsException e) {
			System.out.println(e);
		}

		//much more testing is needed!
	}
}



CSci 2101 course web site.