Storing integer values

exercise No. 152

A container of fixed capacity holding integer values

Q:

You may feel tired of bothering about fixed array sizes. This exercise set is about automating the process of enlarging arrays to accommodate new values.

Implement a class BoundedIntegerStore being able to hold a fixed number of integer values. Internally these values will be stored in an array. The design has been chosen to be extended in later exercises.

A skeleton project archive is available at:

Like in the previous exercise Junit tests are being prepared but masked by a @Disabled annotation.

This skeleton project contains:

  • A class Driver which allows you to execute some output generating tests.

  • A Junit test IntStoreTest which allows you to test your ongoing implementation of BoundedIntegerStore. This class contains several // TODO comments indicating positions to be completed.

  • Like in the previous exercise the Junit tests are being masked by a @Disabled annotation. After successful implementation its removal allows for test execution.

A:

exercise No. 153

Allow for variable capacity holding integer values

Q:

In A container of fixed capacity holding integer values once a store has been created its capacity is fixed. We'd like to extend our class to support dynamic growth while adding new values. To achieve this goal follow the following steps:

  1. Copy the completed project to a new one e.g. named UnboundedContainer. This way you still have the bounded implementation available.

  2. Alter the both artifact and project name in your pom.xml file. Rename your class BoundedIntegerStore to IntegerStore indicating the now missing restriction and thus the possibility of dynamic growth.

  3. Read section Changing an Array Size in chapter 6.

  4. You have to modify the void addValue(int value) method's implementation: If the array's size gets exceeded (e.g. adding the fifth value while still having array size of 4) provide the following actions:

    • Create a second array offering twice the current capacity. This technique is referred to as amortized doubling.

    • Copy all existing values from the current array to the new array

    • Assign the new array to your int[] values variable. You thereby implicitly discard the old array which become subject to be garbage collected.

    • copy the latest argument to the array as usual.

    Basically you have to compare the number of already inserted elements and the current capacity prior to inserting any new value.

  5. Add a default constructor providing an initial default capacity of 4.

Modify and add Junit tests accordingly to reflect the new behaviour. In particular you should test for correct capacity reallocation handling when adding larger numbers of values being added.

A: