Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE HELP WITH THE TO DO SECTION!! package c222Lab4; import java.util.ArrayList; import java.util.Collection; // TODO: 5 to do items below public class BucketList implements BucketListADT
PLEASE HELP WITH THE TO DO SECTION!!
package c222Lab4; import java.util.ArrayList; import java.util.Collection; //TODO: 5 "to do" items below public class BucketListimplements BucketListADT { private ArrayList > list; private int min; private int max; /** * Constructor. Builds a list of buckets which * supports Entry (key,value pairs) * with expected key values from min to max * * @param min * @param max * @param n */ public BucketList(int min, int max, int n) { for (int i=0; i //TODO: instantiate a SortedBucket of type SortedBucketsb = new SortedBucket (); //TODO: add it to our BucketList instance variable "list" list.add(i, sb); } this.min = min; this.max = max; } /** * Adds the given Entry into the appropriate bucket in * this list of buckets. * @param item */ @Override public void add(Entry item) { int n = list.size(); // DO NOT CHANGE THIS FORMULA. This ensures items will be // added to the correct bucket. int indexToInsert = n*(item.getKey()-min)/(max-min+1); // TODO: check if the calculated indexToInsert is // outside of the allowed range. If it is too large, just // insert into the last bucket. If it is too small (negative) // then insert item into bucket 0. (list.get(indexToInsert)).add(item); } /** * Adds all entries in the Collection c to this List of Buckets * @param c is a Collection of Entries */ @Override public void addAll(Collection > c) { // Don't touch this. // Implement above method .add() for this to work. for (Entry e : c) this.add(e); } /** * @return Returns a single ArrayList of the whole * sorted order of all buckets put together * */ @Override public ArrayList > getSortedOrder() { ArrayList > output = new ArrayList >(); //TODO: add the contents of each bucket into the output ArrayList //Hint: our bucket's .getBucketContents() will get the contents //of that bucket in sorted order if they are stored in sorted order return output; } /** * Returns an ArrayList of the bucket contents of bucket i * @param i * @return */ @Override public ArrayList > getBucket(int i) { //TODO: return the (i)th bucket as an arrayList. //Hint: our SortedBucket class has .getBucketContents() return null; // temporary line } /** * Returns the number of buckets in this list of buckets * @return int */ @Override public int getNumBuckets(){ // Leave this alone. return list.size(); } /* * Shows contents of all buckets, each bucket in [ ] brackets. * Will show empty buckets as well. */ public String toString() { // Leave this alone StringBuilder output = new StringBuilder("["); for (SortedBucket s: this.list) { output.append(s.toString()); } output.append("]"); return output.toString(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started