Task 1: The Measured List

Look at the Measured interface in the list package first. Here it is, without its comments:

public interface Measured {
  void reset();
  int accesses();
  int mutations();
}

A Measured object counts the operations performed on the elements it stores. An access is one read of an element stored in the object. A mutation is one write of an element into the object. accesses and mutations report the two counts, and reset sets both back to zero.

You will write MeasuredArrayList, a class that implements both List and Measured. It stores its elements in an array, the same way ArrayList does. Start from the code of ArrayList, and add the counting.

What counts

Apply the two definitions above to every operation of the List interface. Working out what each operation counts is part of this task.

Two cases do not follow from the definitions. They are choices, so I made them for you:

  • Growing the backing array does not count. It copies every element, so each copy is technically both an access and a mutation. But no element changes its position in the list: the element at each index before the copy is still at that index after it.
  • When remove clears the last slot to null, that is not a mutation. The slot is past the end of the list, so it holds no element of the list.

Testing MeasuredArrayList

You must write unit tests for MeasuredArrayList. Your focus should be on the “Measured” aspect of it (i.e., reset, accesses, mutations). As you test these operations, naturally, you will need to call List interface methods (i.e., add, get, set, remove, indexOf, contains, size) to trigger the various possible outcomes.

MeasuredArrayListTest.java is where you need to add unit tests. The starter provides skeleton code, with two tests. However, you need to add many more tests in there. Notice that MeasuredArrayListTest extends ListTest, so all the List tests also run against your MeasuredArrayList. They make sure the counting does not break the list.

I suggest you write the tests before you implement MeasuredArrayList.