Get Solutions for: Write a trimToSize method for the ResizableArrayBagclass. This method should accomplish the same fun

(https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#trimToSize–). The method header is:
public void trimToSize()
The class is given asbelow:
import java.util.Arrays;
/**
   A class that implements a bag of objects by usingan array.
The bag is never full.
   @author Frank M. Carrano, Timothy M. Henry
   @version 5.0
*/
public final class ResizableArrayBag implementsBagInterface
{
private T[] bag; // Cannot be final due to doubling
private int numberOfEntries;
   private boolean integrityOK = false;
private static final int DEFAULT_CAPACITY = 25; // Initialcapacity of bag
private static final int MAX_CAPACITY = 10000;
/** Creates an empty bag whose initial capacity is 25. */
public ResizableArrayBag()
{
this(DEFAULT_CAPACITY);
} // end default constructor
/** Creates an empty bag having a given initial capacity.
@param initialCapacity The integer capacity desired. */
public ResizableArrayBag(int initialCapacity)
{
checkCapacity(initialCapacity);
  
// The cast is safe because the new array contains nullentries
@SuppressWarnings(“unchecked”)
T[] tempBag = (T[])new Object[initialCapacity]; // Uncheckedcast
bag = tempBag;
numberOfEntries = 0;
integrityOK = true;
} // end constructor
/** Creates a bag containing given entries.
@param contents An array of objects. */
   public ResizableArrayBag(T[] contents)
   {
checkCapacity(contents.length);
bag = Arrays.copyOf(contents, contents.length);
numberOfEntries = contents.length;
integrityOK = true;
   } // end constructor
/** Adds a new entry to this bag.
   @param newEntry The object to be added as a newentry.
   @return True. */
public boolean add(T newEntry)
{
checkintegrity();
if (isArrayFull())
{
   doubleCapacity();
} // end if
  
bag[numberOfEntries] = newEntry;
numberOfEntries++;
  
return true;
} // end add
/** Retrieves all entries that are in this bag.
   @return A newly allocated array of all the entriesin this bag. */
public T[] toArray()
{
checkintegrity();
  
// The cast is safe because the new array contains nullentries.
@SuppressWarnings(“unchecked”)
T[] result = (T[])new Object[numberOfEntries]; // Uncheckedcast
for (int index = 0; index -1; // or >= 0
} // end contains
/** Removes all entries from this bag. */
public void clear()
{
while (!isEmpty())
   remove();
} // end clear
/** Removes one unspecified entry from this bag, ifpossible.
   @return Either the removed entry, if theremoval
   was successful, or null. */
public T remove()
{
checkintegrity();
T result = removeEntry(numberOfEntries – 1);
return result;
} // end remove
/** Removes one occurrence of a given entry from this bag.
   @param anEntry The entry to be removed.
   @return True if the removal was successful, orfalse if not. */
public boolean remove(T anEntry)
{
checkintegrity();
int index = getIndexOf(anEntry);
T result = removeEntry(index);
return anEntry.equals(result);
} // end remove
// Locates a given entry within the array bag.
// Returns the index of the entry, if located,
// or -1 otherwise.
   // Precondition: checkintegrity has beencalled.
private int getIndexOf(T anEntry)
{
int where = -1;
boolean found = false;
int index = 0;
  
while (!found && (index -1, anEntry is in the array bag, andit
// equals bag[where]; otherwise, anEntry is not in thearray.
  
return where;
} // end getIndexOf
   // Removes and returns the entry at a given indexwithin the array.
   // If no such entry exists, returns null.
   // Precondition: 0