Question: Read the comments in the BagsInterface.java source file. You will be tasked with implementing the resize method (how do you wish to resize the Bags

Read the comments in the BagsInterface.java source file. You will be tasked with implementing the resize method (how do you wish to resize the Bags array? I leave that up to you; you could double the size of the array, resize array one element at a time per call, etc) and also the sort method (I highly recommend using the sort code from our Big O Notation analysis assignment).

Sort function code:

private static void helper(Bags a[], int k, int n) { while ( k*2 + 1 < n ) { int child = 2*k + 1; if ((child + 1 < n) && (a[child] < a[child+1])) child++; if (a[k] < a[child]) { swap( a[child], a[k] ); k = child; } else return; } } public static void sort() { int N = this.num_items; for (int i = N/2; i >= 0; i--) { helper( bag, i, this.num_items); } while (N-1 > 0) { T temp = bag[N-1]; bag[N-1] = bag[0]; bag[0] = temp; helper(bag, 0, N-1); N--; } } 

public final class Bag implements BagInterface { private T[] bag_items; private int numberOfEntries; public Bag() { bag_items = (T[])new Object[10]; // default 10 item bag this.numberOfEntries=0; } public Bag(int startSize) { bag_items = (T[])new Object[startSize]; this.numberOfEntries=0; } // return the current size of the bag, or the number of elements in the bag public int getSize(){ return this.numberOfEntries; } // returns true if bag is empty or false if bag is not empty public boolean isEmpty() { return this.numberOfEntries == 0; } // add item to bag, returns true if successful, false if not public boolean add(T item) { if ( this.numberOfEntries >= bag_items.length ) return false; // cannot add more items bag_items[numberOfEntries++] = item; return true; } // returns true if item is removed from bag public boolean remove(T item) { for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!