Question: PART : Completing FloatArrays.java --------------------------------- Task 1. Complete the sequentialSearch(float[] a, float x) method so that it can find the location of the specified value
PART : Completing FloatArrays.java --------------------------------- Task 1. Complete the "sequentialSearch(float[] a, float x)" method so that it can find the location of the specified value "x" in the specified array "a" using the sequential search algorithm (this method needs to return -1 if "x" is not contained in "a"). For a certain time limit that you choose (e.g., 5 minutes), try to implement the "sequentialSearch(float[] a, float x)" method based only on your understanding of the algorithm. Please understand that, during each pass, sequential search examines only one element. If that element is equal to "x", then it needs to return the location of that element in the array. If all of the passes fail to find "x" (i.e., "x" is not in array "a"), then the method needs to return -1. Once you finish the method (or after the time limit), compare your implementation with the Java code in the slides. You can use the code from the slides (but some slight changes may be needed). Your code also needs to pass the unit test named "test2()" in "UnitTests.java". Task 2. (Use any sort of this 3 : Bubble Sort,Selection sort, quick sort)You will need to sort the arrays before you can binary search them. Task 3. Complete the "binarySearchRecursive(float[] a, int lower, int upper, float x)" method so that it can find the location of the specified value "x" in the specified array "a" in the portion bounded by "lower" and "upper" using the RECURSIVE binary search algorithm (this method needs to return -1 if "x" is not contained in "a"). For a certain time limit that you choose (e.g., 15 minutes), try to implement the "binarySearchRecursive(float[] a, int lower, int upper, float x)" method based only on your understanding of the algorithm. Please understand that, given a portion of the array "a[lower..upper]", binary search compares the element in the middle (i.e., "a[middle]" where "middle = (lower+upper)/2") with "x". If "a[middle]" is equal to "x", then the method returns "middle" (i.e., the location of "a[middle]" which is equal to "x"). If "a[middle]" is smaller than "x" (i.e., "x" is greater than "a[middle]"), then the method must apply itself to "a[middle+1..upper]" and then return the result. If "a[middle]" is greater than "x" (i.e., "x" is smaller than "a[middle]"), then the method must apply itself to "a[lower..middle-1]" and then return the result. Once you finish the method, compare your implementation with the Java code in the slides. Your code also needs to pass the unit test named "test3()" in "UnitTests.java". Task 4 . Complete the "binarySearchIterative(float[] a, float x)" method so that it can find the location of the specified value "x" in the specified array "a" using the ITERATIVE binary search algorithm (this method needs to return -1 if "x" is not contained in "a"). For a certain time limit that you choose (e.g., 15 minutes), try to implement the "binarySearchIterative(float[] a, float x)" method based only on your understanding of the algorithm. Please understand that this method needs to maintain two variables "lower" and "upper" in order to focus on a portion of the array "a[lower..upper]". This method needs to compare the element in the middle (i.e., "a[middle]" where "middle = (lower+upper)/2") with "x". If "a[middle]" is equal to "x", then the method returns "middle" (i.e., the location of "a[middle]" which is equal to "x"). If "a[middle]" is smaller than "x" (i.e., "x" is greater than "a[middle]"), then the method needs to set "lower" to "middle+1" to focus only on "a[middle+1..upper]". If "a[middle]" is greater than "x" (i.e., "x" is smaller than "a[middle]"), then the method needs to set "upper" to "middle-1" to focus only on "a[lower..middle-1]". Once you finish the method (or after the time limit), compare your implementation with the Java code in the slides. Your code also needs to pass the unit test named "test4()" in "UnitTests.java".
............................
FloatArrays.java :
import java.util.Arrays;
public class FloatArrays { /** * Finds the location of the specified value (the index at which the specified value is stored) in the specified * array by examining each element until the value is found. * * @param a * an {@code float} array * @param x * an {@code float} value * @return the location of the specified value (the index at which the specified value is stored) in the specified * array; {@code -1} if the specified value is not in the array */ public static int sequentialSearch(float[] a, float x) { // TODO: add some code here return -1; } /** * Finds the location of the specified value (the index at which the specified value is stored) in the specified * sorted array using the recursive binary search algorithm. * * @param a * an {@code float} array that is known to be sorted * @param lower * an index representing the lower bound of the binary search * @param upper * an index representing the upper bound of the binary search * @param x * an {@code float} value * @return the location of the specified value (the index at which the specified value is stored) in the specified * array; {@code -1} if the specified value is not in the array */ public static int binarySearchRecursive(float[] a, float lower, float upper, float x) { // TODO: add some code here return -1; } /** * Finds the location of the specified value (the index at which the specified value is stored) in the specified * sorted array using the iterative binary search algorithm. * * @param a * an {@code float} array that is known to be sorted * @param x * an {@code float} value * @return the location of the specified value (the index at which the specified value is stored) in the specified * array; {@code -1} if the specified value is not in the array */ public static int binarySearchIterative(float[] a, float x) { // TODO: add some code here return -1; } /** * The main method of the {@code IntArrays} class. * * @param args * the program arguments */ public static void main(String[] args) { float[] a = { 5.1f, 3.2f, 1.7f, 2.6f, 4.4f }; float[] b = { 1.4f, 3.5f, 5.2f, 7.2f, 9.2f, 11.2f, 13.8f }; System.out.println(sequentialSearch(a, 2.6f)); System.out.println(sequentialSearch(a, 7.2f)); System.out.println(sequentialSearch(b, 2.3f)); System.out.println(sequentialSearch(b, 7.2f)); System.out.println(); System.out.println(binarySearchRecursive(a, 0, a.length - 1, 2.6f)); System.out.println(binarySearchRecursive(a, 0, a.length - 1, 7.2f)); System.out.println(binarySearchRecursive(b, 0, b.length - 1, 2.3f)); System.out.println(binarySearchRecursive(b, 0, b.length - 1, 7.2f)); System.out.println(); System.out.println(binarySearchIterative(a, 2.6f)); System.out.println(binarySearchIterative(a, 7.2f)); System.out.println(binarySearchIterative(b, 2.6f)); System.out.println(binarySearchIterative(b, 7.2f)); System.out.println(); } } ........................
UnitTests.java
package csi213.lab06;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import org.junit.Test;
public class UnitTests {
static float[] a = { 5.5f, 3.3f, 1.1f, 2.2f, 4.4f };
static float[] b = { 7.7f, 6.6f, 5.5f, 1.1f, 2.2f, 3.3f, 4.4f };
static float[] c = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f };
static float[] d = { 1.1f, 3.3f, 5.5f, 7.7f, 9.9f, 11.11f, 13.3f };
/**
* Tests the Task 2 implementation.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test2() throws Exception {
assertEquals(0, FloatArrays.sequentialSearch(c, 1.1f));
assertEquals(-1, FloatArrays.sequentialSearch(c, 7.7f));
assertEquals(-1, FloatArrays.sequentialSearch(d, 2.2f));
assertEquals(3, FloatArrays.sequentialSearch(d, 7.7f));
}
/**
* Tests the Task 3 implementation.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test3() throws Exception {
assertNotEquals(-1, FloatArrays.binarySearchRecursive(c, 0, c.length - 1, 1.1f));
assertEquals(-1, FloatArrays.binarySearchRecursive(c, 0, c.length - 1, 7.7f));
assertEquals(-1, FloatArrays.binarySearchRecursive(d, 0, d.length - 1, 2.2f));
assertNotEquals(-1, FloatArrays.binarySearchRecursive(d, 0, d.length - 1, 3.3f));
}
/**
* Tests the Task 4 implementation.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test4() throws Exception {
assertNotEquals(-1, FloatArrays.binarySearchIterative(c, 1.1f));
assertEquals(-1, FloatArrays.binarySearchIterative(c, 7.7f));
assertEquals(-1, FloatArrays.binarySearchIterative(d, 2.2f));
assertNotEquals(-1, FloatArrays.binarySearchIterative(d, 3.3f));
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
