CS 201 - Lab 5


In this class you will modify the InfiniteArray class so that, like its name suggests, it acts like an array that can store an infinite number of elements.

The InfiniteArray class supports the following methods

public InfiniteArray() - the constructor initializes a new InfiniteArray object.  The new InfiniteArray object will not contain any elements.

public void add(Object element) - adds a new element onto the end of the InfiniteArray.

public Object get(int index) - gets back the element at the given index.  The first element added is in index 0, the second in index 1, etc.

public int size() - returns the number of elements that have been added to the InfiniteArray.

Getting Started

Import lab5.zip.  (File->Import...->General->Existing Projects into Workspace->Archive File).  You should see a project called lab5 in the Package Explorer.

Part I

First, add some additional unit tests to the InfiniteArrayTest class to prove that the InfiniteArray cannot actually store an unlimited number of elements.

Part II

Next, fix the InfiniteArray class so that it can store an unlimited number of elements.

Note: do NOT just increase the size of the INITIAL_CAPACITY constant.

Hint: when the capacity of the storage array is exceeded, you should create and initialize a new, larger array that contains all of the elements of the original array, but has extra room to add more elements.  Then, make the InfiniteArray use the new array instead of the original one.

Verify that all of the unit tests in InfiniteArrayTest pass.  Can you find a way to verify that the InfiniteArray can hold a large number of objects: perhaps 100,000 objects?