// CSE 143, Winter 2010, Marty Stepp // A StutterIntList object stores an ordered list of integers using // an unfilled array. When elements are added to a StutterIntList, actually // several copies of the element are added, based on a "stretch factor" value // supplied as the list is constructed. // The point of this class is to demonstrate inheritance and basing one data // structure on another. public class StutterIntList extends ArrayIntList { // "stretch factor" - number of copies of each element that will be added private int stretch; // Constructs a new list of a default capacity with the given stretch factor. // Precondition: stretch >= 0 public StutterIntList(int stretch) { this(stretch, DEFAULT_CAPACITY); } // Constructs a new list of the given capacity with the given stretch factor. // Precondition: stretch >= 0 && capacity >= 0 public StutterIntList(int stretch, int capacity) { super(capacity); this.stretch = stretch; } // we don't need to write the one-parameter add method below, because the inherited // one from ArrayIntList will already call our new two-parameter add method, // add(size, value); , which will do the right thing (adding multiple copies). // public void add(int value) { // for (int i = 0; i < stretch; i++) { // super.add(value); // } // } // Adds 'stretch' copies of the given value at the given index. // Precondition: 0 <= index <= size() // Throws an IndexOutOfBoundsException if the index is out of range. public void add(int index, int value) { checkIndex(index, 0, size()); for (int i = 0; i < stretch; i++) { super.add(index, value); } } // Returns this list's stretch factor that was passed at construction. public int getStretch() { return stretch; } }