Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need help with Visual Studio Coding. In VSCode when you open the project folder you should see: 1. BucketSortMain.java allows you to test

Hi, I need help with Visual Studio Coding.

In VSCode when you open the project folder you should see:

image text in transcribed

1. BucketSortMain.java allows you to test your code by choosing different input data.

2. All of the 7 TODOs are found in the IntegerBucketSorter.java file. Each TODO asks you to implement a method. Explanations of what the methods should do are in the comments for each method. We recommend that you implement the TODOs in order from 1 to 7.

3. The Sorter.java is an interface that IntegerBucketSorter implements.

4. The JUnit tests are in the ProjectTests.java file.

After all the ToDos are completed and you run BucketSortMain.java, the sorted output with the sample input data is:

image text in transcribed

BucketSortMain.java:

package app;

public class BucketSortMain {

public static void main(String[] args) throws Exception{

Sorter s = new IntegerBucketSorter ();

System.out.println("input:");

int[] testArr = {100, 23, 92, 498, 12, 29, 48, 354, 1, 57, 33};

System.out.println(printArr(testArr));

// correct output: 1, 12, 23, 29, 33, 48, 57, 92, 100, 354, 498

int[] resultArr = s.sort(testArr);

System.out.println("output:");

System.out.println(printArr(resultArr));

}

public static String printArr(int[] arr){

StringBuilder sb = new StringBuilder();

for(int i=0;i

sb.append(arr[i]);

if (i

sb.append(", ");

}

return sb.toString();

}

}

IntegerBucketSorter.java: (7 TODOs can be found here)

package app;

/**

* This class implements the bucket sort algorithm for an array of integers.

* It uses a two dimensional array to carry out the algorithm.

* The maximum integer length, MAX_DIGIT_LIMIT, is 5, so only integers with 5 digits or less

* can be processed. Also at this time, only integers >=0 can be processed.

* The SENTINEL value -1 is used to indicate an empty cell in the buckets array.

*/

public class IntegerBucketSorter implements Sorter {

// 2-D int array- the "buckets".

int[][] buckets;

// indicates an "empty" cell- no data.

private static final int SENTINEL = -1;

// maximum number of digits an integer can have to be processed.

private static final int MAX_DIGIT_LIMIT = 5;

/**

* Use the bucket sort algorithm to return an array of the integers in the

* input array in sorted order. The input array and the returned array may be the same array.

* @param dataArray an array of integers to be sorted.

* @return an array of integers sorted in ascending order.

* @throws an Exception if any integer in the array is > MAX_DIGIT_LIMIT.

*/

public int[] sort(int[] dataArray) throws Exception {

//TODO7: Implement this method.

return dataArray;

}

/**

* Distribution phase:

* Iterate through the input array and distribute the data into the buckets array

* by sorting on the value of each integer at the current place. The integer's value

* at the current place is the index of the row into which it is distributed.

* The next available cell in the row has to be found so data that already exists in that row

* is not overwritten.

* @param dataArray the integers to be sorted.

* @param curPlace the current "place" used to determine the bucket where an integer is written to.

*/

public void distribute(int[] dataArray, int curPlace){

//TODO5: Implement this method.

}

/**

* Collection phase:

* Iterate through the buckets array starting at row 0.

* Collect all integers stored in that row into the data array,

* in the order they appear in that row.

* Do that for each row until done.

* @param dataArray the integers to be sorted.

*/

public void collect(int[] dataArray) {

//TODO6: Implement this method

}

/**

* Finds the integer with the maximum number of digits, or "places"

* in the array. This is used to determine how many iterations of the

* bucket sort algorithm are required.

* Assume the length of the array >=0.

* This method calls findIntLength.

* @param array the input array of integers to be sorted.

* @throws Exception if findIntLength throws an Exception.

* @return the largest number of digits found in the integers in the array, return 0 if array is empty.

*/

public int findMaxIntLength(int[] array) throws Exception{

int max = -1;

//TODO4: Implement this method

return max;

}

/**

* Returns the number of digits or "places" in the argument, num.

* If num was 0, the return would be 1.

* If num was 5, the return would be 1.

* If num was 15, the return would be 2.

* If num was 500, the return would be 3.

* etc. This method should handle an integer with up to MAX_DIGIT_LIMIT places.

* Assume num >=0.

* @param num the integer whose number of digits we want to determine.

* @return the number of digits of num.

* @throws Exception if the number of digits of num is > MAX_DIGIT_LIMIT.

*/

public int findIntLength(int num) throws Exception {

int len = -1;

//TODO3: Implement this method

return len;

}

/**

* Returns the digit at the specific "place" in the argument, num.

* If the argument does not have a digit at the specified place, 0 is returned.

* If place was 1 and num was 5, the return would be 5.

* If place was 2 and num was 5, the return would be 0.

* If place was 1 and num was 39, the return would be 9.

* If place was 2 and num was 167, the return would be 6.

* Assume num >=0.

* @param place the specific digit required.

* @param num the integer we want to extract the digit from.

* @return the digit at the specified place.

*/

public int getPlaceValue(int place, int num){

int digit = -1;

//TODO2: Implement this method

return digit;

}

/**

* This method overwrites all cells in the "buckets" array

* with the SENTINEL value. This resets the array to be ready

* for another round of the bucket sort algorithm. It should

* also be called as soon as the buckets array is created so that

* it is properly initialized.

*/

public void resetBucketValues(){

//TODO1: Implement this method

}

}

Sorter.java:

package app;

/**

* Generic sorting interface. At this time, only arrays of integers is supported.

*/

public interface Sorter {

public int[] sort(int[] unsorted) throws Exception;

}

EXPLORER PROJ3-BUCKET-SORT-STARTER > lib V src app BucketSortMain.java IntegerBucket Sorter.java Sorter.java v test ProjectTests.java input: 100, 23, 92, 498, 12, 29, 48, 354, 1, 57, 33 output: 1, 12, 23, 29, 33, 48, 57, 92, 100, 354, 498 EXPLORER PROJ3-BUCKET-SORT-STARTER > lib V src app BucketSortMain.java IntegerBucket Sorter.java Sorter.java v test ProjectTests.java input: 100, 23, 92, 498, 12, 29, 48, 354, 1, 57, 33 output: 1, 12, 23, 29, 33, 48, 57, 92, 100, 354, 498

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_2

Step: 3

blur-text-image_3

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

Genomes Browsers And Databases Data Mining Tools For Integrated Genomic Databases

Authors: Peter Schattner

1st Edition

0521711320, 978-0521711326

More Books

Students also viewed these Databases questions

Question

What are the major differences between public and private markets?

Answered: 1 week ago