Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

image text in transcribed

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

//TODO: 5 "to do" items below

public class BucketList implements 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) { list = new ArrayList>(); for (int i=0; i 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 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(); } }

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

/* Don't change this class */

public interface BucketListADT {

void add(Entry item);

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

public int getNumBuckets();

ArrayList> 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 */ public class Entry { 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 b = new BucketList(0,100,6); b.add(new Entry(94 , "Jim")); System.out.println(b); b.add(new Entry(93 , "Val")); System.out.println(b); b.add(new Entry(68 , "C-Student")); System.out.println(b); b.add(new Entry(72 , "B-Student")); System.out.println(b); b.add(new Entry(84 , "A-Student")); System.out.println(b); b.add(new Entry(98 , "A-plus-Student")); System.out.println(b);

// add N more random students int N = 10; ArrayList> allStudents = new ArrayList>(); for (int i=1; i e = new Entry((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 implements SortedBucketADT{

private ArrayList> 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>(); }

/** * Adds entry t to bucket in proper sorter order */ @Override public void add(Entry 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() > 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{

void add(Entry t);

ArrayList> 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 b; ArrayList> everyone; Entry 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(0,100,6); b.addAll(everyone); ArrayList> list4 = new ArrayList(); list4.add(C); list4.add(B); list4.add(A); ArrayList> 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(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> list0 = new ArrayList(); ArrayList> list1 = new ArrayList(); ArrayList> list2 = new ArrayList(); ArrayList> list3 = new ArrayList(); ArrayList> 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(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(-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(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(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(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

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago