The class ResizableArrayBag has an array that can grow in size as objects are added to the bag. Revise the class so that its array also can shrink in size as objects are removed from the bag. Accomplishing this task will require two new private methods, as follows:

• The first new method checks whether we should reduce the size of the array:
private boolean isTooBig()

This method returns true if the number of entries in the bag is less than half the size of the array and the size
of the array is greater than 20.
The second new method creates a new array that is three quarters the size of the current array and then
copies the objects in the bag to the new array:
private void reduceArray()
Implement each of these two methods, and then use them in the definitions of the two remove methods.

```
private boolean isTooBig()
{
return (numberOfEntries < bag.length / 2) && (bag.length > 20);
} // end isTooBig
private void reduceArray()
{
T[] oldBag = bag;
int oldSize = oldBag.length;
// Save reference to array
// Save old max size of array
@SuppressWarnings("unchecked")
T[] tempBag = (T[])new Object[3 * oldSize / 4]; // Reduce size of array; unchecked cast
bag = tempBag;
// Copy entries from old array to new, smaller array
for (int index = 0; index < numberOfEntries; index++)
bag[index] = oldBag[index];
} // end reduceArray
public T remove()
{
T result = removeEntry(numberOfEntries - 1);
if (isTooBig())
reduceArray();
return result;
} // end remove
public boolean remove(T anEntry)
{
int index = getIndexOf(anEntry);
T result = removeEntry(index);
if (isTooBig())
reduceArray();
return anEntry.equals(result);
} // end remove
```

Computer Science & Information Technology

You might also like to view...

Cork is an example of a texture fill

Indicate whether the statement is true or false

Computer Science & Information Technology

To provide easy (one-click) access to a commonly used command, you might customize this feature

A) Status bar B) Mini toolbar C) File tab D) Quick Access Toolbar

Computer Science & Information Technology