Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions