Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer the \"TODO\" parts for each class to make the program works, there

ID: 3591156 • Letter: P

Question

Please answer the "TODO" parts for each class to make the program works, there are total 7 classes and have 4 classes need to add some codes. Please answer the "TODO" parts as many as you can, thank you very much. Please add JUnit 4 when you test the codes below.

import java.util.ArrayList;
import java.util.Collection;

//TODO: 5 "to do" items below

public class BucketList<V> implements BucketListADT<V> {

   private ArrayList<SortedBucket<V>> 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) {
       list = new ArrayList<SortedBucket<V>>();
       for (int i=0; i<n; i++){
           //TODO: instantiate a SortedBucket of <V> type
          
           //TODO: add it to our BucketList instance variable "list"
       }
       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<V> 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<Entry<V>> c) {
       // Don't touch this.
       // Implement above method .add() for this to work.
      
       for (Entry<V> e : c)
           this.add(e);
   }
  
   /**
   * @return Returns a single ArrayList of the whole
   * sorted order of all buckets put together
   *
   */
   @Override
   public ArrayList<Entry<V>> getSortedOrder() {
       ArrayList<Entry<V>> output = new ArrayList<Entry<V>>();

       //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<Entry<V>> 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<V> s: this.list) {
           output.append(s.toString());
       }
       output.append("]");
       return output.toString();
   }
}

import java.util.ArrayList;
import java.util.Collection;

/* Don't change this class */

public interface BucketListADT<V> {

   void add(Entry<V> item);

   void addAll(Collection<Entry<V>> c);
  
   ArrayList<Entry<V>> getBucket(int i);

   public int getNumBuckets();

   ArrayList<Entry<V>> getSortedOrder();

}

/* There is no need to modify this class. You may modify the class
if you know what you are doing, and want to add functionality
to it, for instance, adding mutators */

/**
* A (key,value) pair class. Key is int type.
*
*
*
* @param <V>
*/
public class Entry <V> {
   private int key;
   private V val;
  
   public Entry(int k, V v) {
       key = k;
       val = v;
   }

   public int getKey() {
       return this.key;
   }

   public V getValue() {
       return this.val;
   }

   public String toString() {
       return this.getValue()+"="+this.getKey();
   }
}

import java.util.ArrayList;

public class MainClass {

   public static void main(String[] args) {

       // No modifications are needed in this main method. It is
       // here just to illustrate a typical usage of this BucketList
       // object and the resulting BucketSort method

       // This example makes a list of 6 buckets.
       // Key values are grades, so range is: min=0 max=100
       // It adds the 6 objects from the test file, plus
       // several randomly-generated students.
      
       // This file, as given, will have no errors or warnings
       // if the BucketList and SortedBucket classes are fully
       // implemented
      
       // Feel free to tinker with this file as you are developing.

       BucketList<String> b = new BucketList<String>(0,100,6);
       b.add(new Entry<String>(94 , "Jim"));
       System.out.println(b);
       b.add(new Entry<String>(93 , "Val"));
       System.out.println(b);
       b.add(new Entry<String>(68 , "C-Student"));
       System.out.println(b);
       b.add(new Entry<String>(72 , "B-Student"));
       System.out.println(b);
       b.add(new Entry<String>(84 , "A-Student"));
       System.out.println(b);
       b.add(new Entry<String>(98 , "A-plus-Student"));
       System.out.println(b);

       // add N more random students
       int N = 10;
       ArrayList<Entry<String>> allStudents = new ArrayList<Entry<String>>();
       for (int i=1; i<=N; i++) {
           Entry<String> e = new Entry<String>((int)(45+Math.random()*50),"stu"+i);
           allStudents.add(e);
       }

       // add the whole class to the buckets
       b.addAll(allStudents);
      
       // display all buckets and their contents
       System.out.println(b);
      
       // display the final sorted order
       System.out.println(b.getSortedOrder());
   }

}

import java.util.ArrayList;

//TODO: 1 "to do" item below

public class SortedBucket<V> implements SortedBucketADT<V>{

   private ArrayList<Entry<V>> b;

   /**
   * Constructor. Instantiates a bucket, which will be a sorted
   * collection of Entries (with each Entry being a key,value pair)
   */
   public SortedBucket() {
       // Don't touch.
       b = new ArrayList<Entry<V>>();
   }

   /**
   * Adds entry t to bucket in proper sorter order
   */
   @Override
   public void add(Entry<V> t) {
       // TODO: Implement this method
      
       /* HINT: This is the main sorting process. As we insert into a
       * bucket the Entry t should be placed in the correct position
       * in the bucket. Lowest keys should be at the 'head' of the list.
       * That is, index 0 is the smallest key value. The bucket b is
       * an ArrayList, and you should iterate through the contents of
       * the bucket until you find where
       *            t.getKey() < e.getKey()
       * and then insert at that position. Note that ArrayList has
       * an .add(i,t) method which inserts object t into position i
       * and automatically shifts everything to the right to
       * make room for t at that position.
       *
       * You will likely need a special case for when you need to insert
       * into b when b is empty, and another case for when you need
       * to insert at the end of b
       *
       */
      
      
       b.add(t); // this inserts without considering the order
       return;
   }
  
  
   /**
   * @return a sorted ArrayList of Entries in this bucket
   */
   @Override
   public ArrayList<Entry<V>> getBucketContents() {
       // Don't touch.
       return b;
   }

   @Override
   public String toString() {
       // Don't touch.
       return b.toString();
   }
}

import java.util.ArrayList;

/* Don't change this class */

public interface SortedBucketADT<V>{

   void add(Entry<V> t);

   ArrayList<Entry<V>> getBucketContents();


}

// This file will pass all tests without errors when BucketList
// and SortedBucket are implemented

// TODO: there are 3 "to do" items below

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Before;

public class Test {

   /* Declaration of the test objects */
  
   BucketList<String> b;
   ArrayList<Entry<String>> everyone;
   Entry<String> jim, val, C, B, A, Aplus;
  
   /* Instantiation of the objects */
  
   @Before
   public void initialize() {
       everyone = new ArrayList<>();
       jim = new Entry<>(94 , "Jim");
       val = new Entry<>(93 , "Val");
       C = new Entry<>(68 , "C-Student");
       B = new Entry<>(72 , "B-Student");
       A = new Entry<>(84 , "A-Student");
       Aplus = new Entry<>(98 , "A-plus-Student");

       everyone.add(jim);
       everyone.add(val);
       everyone.add(C);
       everyone.add(B);
       everyone.add(A);
       everyone.add(Aplus);

   }

   /* Various tests follow */
  
   @org.junit.Test
   public void test1() {
       // BucketList constructor takes (min,max,numBuckets)
       b = new BucketList<String>(0,100,6);
       b.addAll(everyone);
       ArrayList<Entry<String>> list4 = new ArrayList<>();
       list4.add(C);
       list4.add(B);
       list4.add(A);
       ArrayList<Entry<String>> list5 = new ArrayList<>();
       list5.add(val);
       list5.add(jim);
       list5.add(Aplus);
       //System.out.println(list4);
       //System.out.println(list5);
       //System.out.println(b);
      
       // .equals in ArrayLists compares corresponding items
       assertTrue(b.getBucket(4).equals(list4)
                   && b.getBucket(5).equals(list5)
                   && b.getBucket(0).isEmpty()
                   && b.getBucket(1).isEmpty()
                   && b.getBucket(2).isEmpty()
                   && b.getBucket(3).isEmpty());
   }

   @org.junit.Test
   public void test2() {
       // BucketList constructor takes (min,max,numBuckets)
       b = new BucketList<String>(0,100,5);
       b.addAll(everyone);

       //TODO:
      
       // Figure out (on paper?) where these 6 Entry objects should
       // be in the case of 5 buckets. Then build a test
       // like in test1() to check each of the 5 buckets
       // for the correct contents (and correct order of contents)
       // (Note that list4 and list5 in test1 had their elements
       // added so that the resulting list is in sorted order so
       // that testing the corresponding bucket is easier.

       // Use whichever lists you need (see test1() )
       /*
       ArrayList<Entry<String>> list0 = new ArrayList<>();
       ArrayList<Entry<String>> list1 = new ArrayList<>();
       ArrayList<Entry<String>> list2 = new ArrayList<>();
       ArrayList<Entry<String>> list3 = new ArrayList<>();
       ArrayList<Entry<String>> list4 = new ArrayList<>();

       list4.add(Aplus); // ... add rest of Entry objects
       */
      
       assertTrue(b.getBucket(0).isEmpty() // complete this test
                   // &&... (add
               );
   }

   @org.junit.Test
   public void test3() {
       // BucketList constructor takes (min,max,numBuckets)
       b = new BucketList<String>(0,100,4);
       b.addAll(everyone);
      
       //TODO:
       // Write this test similar to test2(). But this time these
       // six Entry Objects are added into a BucketList with 4 buckets
       assertTrue(true); // change this assert
   }
  
   @org.junit.Test
   public void emptyTest() {
       // BucketList constructor takes (min,max,numBuckets)
       // Testing if we can construct in the negative range, and if
       // it will still perform "sort" without crashing if all buckets
       // are empty
       b = new BucketList<String>(-50,50,6);
       assertTrue(b.getNumBuckets()==6 && b.getSortedOrder().size()==0);
   }

   @org.junit.Test
   public void perfectStudentTest() {
       // test where an Entry named "perfect" with a score of 100 will
       // be placed in a BucketList which stores grades from 0 to 100 and
       // having 10 buckets.
      
       b = new BucketList<String>(0,100,10);
       b.add(new Entry<>(100,"perfect"));
       assertTrue(b.getBucket(9).size()==1);
   }

   @org.junit.Test
   public void outOfBoundsTest1() {
       // tests if we can add an Entry with key value larger than max
      
       b = new BucketList<String>(0,100,10);
       b.add(new Entry<>(110,"superPerfect"));
       assertTrue(b.getBucket(9).size()==1);
   }

   @org.junit.Test
   public void outOfBoundsTest2() {
       // tests if we can add an Entry with key value less than min
      
       b = new BucketList<String>(0,100,10);
       b.add(new Entry<>(-110,"notSoPerfect"));
       assertTrue(b.getBucket(0).size()==1);
   }

   @org.junit.Test
   public void additionalTest() {
       //TODO: Write an additional test that
       //is different from the above
   }

}

src (default package) BucketList.java DBucketListADT.java D Entry.java MainClass .java D SortedBucketjava DSortedBucketADT.java DTest.java JRE System Library [JavaSE-1.8] JUnit 4

Explanation / Answer

BucketList.java

import java.util.ArrayList;

import java.util.Collection;

//TODO: 5 "to do" items below

public class BucketList<V> implements BucketListADT<V> {

private ArrayList<SortedBucket<V>> 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) {

list = new ArrayList<SortedBucket<V>>();

for (int i=0; i<n; i++){

//TODO: instantiate a SortedBucket of <V> type

SortedBucket<V> sortedBucket = new SortedBucket<>();

//TODO: add it to our BucketList instance variable "list"

list.add(sortedBucket);

}

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<V> 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.

if(indexToInsert>=n) {

list.get(n-1).add(item);

}else if(indexToInsert<0){

list.get(0).add(item);;

}else {

(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<Entry<V>> c) {

// Don't touch this.

// Implement above method .add() for this to work.

  

for (Entry<V> e : c)

this.add(e);

}

  

/**

* @return Returns a single ArrayList of the whole

* sorted order of all buckets put together

*

*/

@Override

public ArrayList<Entry<V>> getSortedOrder() {

ArrayList<Entry<V>> output = new ArrayList<Entry<V>>();

//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

for(SortedBucket<V> bucket:list) {

output.addAll(bucket.getBucketContents());

}

return output;

}

  

/**

* Returns an ArrayList of the bucket contents of bucket i

* @param i

* @return

*/

@Override

public ArrayList<Entry<V>> getBucket(int i) {

//TODO: return the (i)th bucket as an arrayList.

//Hint: our SortedBucket class has .getBucketContents()

return list.get(i).getBucketContents();

}

  

/**

* 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<V> s: this.list) {

output.append(s.toString());

}

output.append("]");

return output.toString();

}

}

SortedBucket.java:

import java.util.ArrayList;

//TODO: 1 "to do" item below

public class SortedBucket<V> implements SortedBucketADT<V> {

private ArrayList<Entry<V>> b;

/**

* Constructor. Instantiates a bucket, which will be a sorted collection of

* Entries (with each Entry being a key,value pair)

*/

public SortedBucket() {

// Don't touch.

b = new ArrayList<Entry<V>>();

}

/**

* Adds entry t to bucket in proper sorter order

*/

@Override

public void add(Entry<V> t) {

// TODO: Implement this method

/*

* HINT: This is the main sorting process. As we insert into a bucket the Entry

* t should be placed in the correct position in the bucket. Lowest keys should

* be at the 'head' of the list. That is, index 0 is the smallest key value. The

* bucket b is an ArrayList, and you should iterate through the contents of the

* bucket until you find where t.getKey() < e.getKey() and then insert at that

* position. Note that ArrayList has an .add(i,t) method which inserts object t

* into position i and automatically shifts everything to the right to make room

* for t at that position.

*

* You will likely need a special case for when you need to insert into b when b

* is empty, and another case for when you need to insert at the end of b

*

*/

int index = 0;

for(Entry<V> entry:b) {

if(entry.getKey()>t.getKey()) {

break;

}

index++;

}

if(index==b.size()) {

b.add(t);

}else {

b.add(index, t);

}

return;

}

/**

* @return a sorted ArrayList of Entries in this bucket

*/

@Override

public ArrayList<Entry<V>> getBucketContents() {

// Don't touch.

return b;

}

@Override

public String toString() {

// Don't touch.

return b.toString();

}

}