Lab 4, CSC 2001
1 Introduction
Java Arrays are simple animals, reflecting their heritage; they represent a fixed number of values, indexed by a natural number. The operations available on an array are essentially confined to setting and getting a single element.
It is often useful to extend this functionality to include a richer set of operations; that’s what this lab is about.
As always, you must develop every method below by following the design recipe. This means (1) starting with any data definitions required by the function, (2) writing a purpose statement and the header of the function, (3) writing test cases (*before* proceeding onward), (4) writing the template (for functions on arrays, the template should include an accumulator, a loop that iterates over the elements of the array with an incomplete update of the accumulator, and finally a return), and only then (5) filling it in. If you write the function before writing test cases, prepare to struggle and waste time....
In the methods below, if you find that the implementation of one method could profitably employ another method you developed, then you should go ahead and do that! There’s no need for all of your methods to contain the same code.
Also, it probably goes without saying that you should not use Java’s ArrayList until the very last section’s question. Or a linked list.
2 Warmup
First, a few warmup exercises on Java arrays:
define three examples of Java integer arrays, of different lengths.
Following the design recipe, develop a static method that accepts an array of integers and returns true if all of its elements are less than 100.
3 Your Class
Okay, on to the main event.
Develop a class that represents a list of integers using a Java array. That is, your class should contain a Java array as a field. In order to support growing the length of this list, you will need to support backing-array doubling. This means that the class will contain an integer, representing a count of how many of the array elements are "live"; adding an element to the list will increase this count, and when the array is overfull, your code will have to create a fresh array of double the size, and copy the elements. Think about a good name for this class, that’s not too long and not too short.
Your class will have to contain an equals method, to allow test cases. This equals method should verify that numeric fields are the same, and that array fields contain the same elements.
Also, your class will have to contain the standard constructor, that accepts values for all fields, in order to allow tests.
Develop the equalElts method, that accepts another object of your class and returns true when they contain the same elements. Note that this is different from the equals method; the two objects may contain arrays of different lengths, and still represent the same sequence of integers.
Develop the empty method, that constructs a new object representing the empty list of integers.
Develop the length method, that returns the number of elements in the list represented by the object. (This is not the same as the length of the underlying array...)
Develop the get method, that accepts an index and returns the element at that index. If the index does not refer to a legal element number in the squence, you should throw NoSuchElementException.
Develop the set method, that accepts an index and a new value, and mutates the underlying array to contain the new value at the given index. This method cannot change the length of the list, the length method should return the same thing after calling set. Signal an exception for an invalid element reference.
Develop the insert method, that accepts and index and an element, and inserts the given item at the given index, moving later elements up by one. Signal an exception where appropriate, as before.
Develop the addToEnd method, that adds an element to the end of a list.
Develop the addToStart method, that adds an element to the beginning of a list.
Develop the remove method, that removes an element at a given index. After this operation, later elements should be moved up, so that the length of the list is now less by one. As before, signal an exception on an index that does not refer to an existing element.
4 Comparison to ArrayList
So... it turns out that actually, Java has a built-in class that’s very similar to yours. It’s called ArrayList. Do some research on Oracle’s Java documentation, and identify three differences between the built-in ArrayList and your own class. Write your answer in the form of a Java comment.