Question

I have this Java program I need Help plz package ads.set2.select; public class Selector { public...


I have this Java program
I need Help plz


package ads.set2.select;

public class Selector {

public static void main(String[] args) {
  int index = 3;
  int[] numbers = { 23, 4, 33, 11, 99,24 };
  int results = select(numbers, index);
  System.out.println("Element at index: " + index + " in sorted array is " + results);

  // error case
  select(numbers, 9);
}

/**
* Returns the number that would be at index {@code index} in a sorted version
* of the array.
*
* @param numbers array with pairwise distinct numbers.
* @param index   the index of the number we're looking for in a sorted version
*                of {@code numbers}.
* @return the number at the given index.
* @throws IllegalArgumentException if {@code index} is not valid for the array.
*/
public static int select(int[] numbers, int index) {
  sort(numbers, numbers.length);
  if (index > numbers.length) {
   throw new IllegalArgumentException("Index is not valid for the array");
  }
  return numbers[index];
}

/**
* A function to implement bubble sort
*
* @param arr the arr
* @param n   the n
*/
public static void sort(int[] arr, int n) {
  // Base case
  if (n == 1)
   return;

  // One pass of bubble sort. After
  // this pass, the largest element
  // is moved (or bubbled) to end.
  for (int i = 0; i < n - 1; i++)
   if (arr[i] > arr[i + 1]) {
    // swap arr[i], arr[i+1]
    int temp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = temp;
   }

  // Largest element is fixed,
  // recur for remaining array
  sort(arr, n - 1);
}

}

I have the Following Failures

There were 5 failures:
1) Test testEmptyArrayWithZeroIndex returned:
java.lang.Exception: Unexpected exception, expected but was
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
?at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StackOverflowError
?at ads.set2.select.Selector.sort(Selector.java:41)
?at ads.set2.select.Selector.sort(Selector.java:57)

2) Test testEmptyArrayWithPositiveIndex returned:
java.lang.Exception: Unexpected exception, expected but was
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
?at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StackOverflowError
?at ads.set2.select.Selector.sort(Selector.java:57)
?at ads.set2.select.Selector.sort(Selector.java:57)

3) Test testEmptyArrayWithNegativeIndex returned:
java.lang.Exception: Unexpected exception, expected but was
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
?at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StackOverflowError
?at ads.set2.select.Selector.sort(Selector.java:57)
?at ads.set2.select.Selector.sort(Selector.java:57)

4) Test testArrayWithNegativeIndex returned:
java.lang.Exception: Unexpected exception, expected but was
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
?at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
?at ads.set2.select.Selector.select(Selector.java:30)
?at SelectorTest.testArrayWithNegativeIndex(SelectorTest.java:38)
?at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
?at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
?at java.base/java.lang.reflect.Method.invoke(Method.java:566)
?at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
?at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
?at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
?at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
?... 4 more

5) Test testArrayWithLargerIndex returned:
java.lang.Exception: Unexpected exception, expected but was
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
?at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
?at ads.set2.select.Selector.select(Selector.java:30)
?at SelectorTest.testArrayWithLargerIndex(SelectorTest.java:43)
?at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
?at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
?at java.base/java.lang.reflect.Method.invoke(Method.java:566)
?at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
?at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
?at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
?at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
?at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
?... 4 more

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Selector {

    public static void main(String[] args) {
        int index = 3;
        int[] numbers = { 23, 4, 33, 11, 99,24 };
        int results = select(numbers, index);
        System.out.println("Element at index: " + index + " in sorted array is " + results);

        // error case
        select(numbers, 6);
    }

    /**
     * Returns the number that would be at index {@code index} in a sorted version
     * of the array.
     *
     * @param numbers array with pairwise distinct numbers.
     * @param index   the index of the number we're looking for in a sorted version
     *                of {@code numbers}.
     * @return the number at the given index.
     * @throws IllegalArgumentException if {@code index} is not valid for the array.
     */
    public static int select(int[] numbers, int index) {
        sort(numbers, numbers.length);
        if (index >= numbers.length || index < 0) {
            throw new IllegalArgumentException("Index is not valid for the array");
        }
        return numbers[index];
    }

    /**
     * A function to implement bubble sort
     *
     * @param arr the arr
     * @param n   the n
     */
    public static void sort(int[] arr, int n) {
        // Base case
        if (n <= 1)
            return;

        // One pass of bubble sort. After
        // this pass, the largest element
        // is moved (or bubbled) to end.
        for (int i = 0; i < n - 1; i++)
            if (arr[i] > arr[i + 1]) {
                // swap arr[i], arr[i+1]
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }

        // Largest element is fixed,
        // recur for remaining array
        sort(arr, n - 1);
    }

}


Failures:
1. testEmptyArrayWithZeroIndex: When Input is empty array with Zero Index
In your sort method 
public static void sort(int[] arr, int n) {
  // Base case
  if (n == 1)
   return;

There is need to change base condition to handle empty array case, As sort method is recursive and array is having size 0, so base case never occurs, Causing stackOverflow

We can change the base condition to 
if (n <= 1)
   return;
It will be fixed
2. testEmptyArrayWithPositiveIndex: Same as first one
3. testEmptyArrayWithNegativeIndex: Same as first one
4. testArrayWithNegativeIndex: Array with negative index
Change is required here 
public static int select(int[] numbers, int index) {
  sort(numbers, numbers.length);
  if (index > numbers.length) {
   throw new IllegalArgumentException("Index is not valid for the array");
  }

In index < 0 then also we need to throw IllegalArgumentException as its not valid index

5. testArrayWithLargerIndex: In Java array index starts with 0. So, if there are 6 elements in array the index for 6th element will be 5. So we need to update the condition to this
if (index >= numbers.length || index < 0) {
   throw new IllegalArgumentException("Index is not valid for the array");
  }

Add a comment
Know the answer?
Add Answer to:
I have this Java program I need Help plz package ads.set2.select; public class Selector { public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • java : here is a code that I have to implement. counting the occuence in an...

    java : here is a code that I have to implement. counting the occuence in an array /** * _Part 3: Implement this method._ * * Counts the items in the ordered array list that are equal to the item at * the specified index. Be sure to take advantage of the fact that the list * is sorted here. You should not have to run through the entire list to * make this count. * * @param index an...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT