Question: Please help me with this ASAP public static int[] task2(int[] seq, int n) { int[] result = null; /* Your task is to implement this
Please help me with this ASAP
public static int[] task2(int[] seq, int n) { int[] result = null; /* Your task is to implement this method, * so that running TestUtilities.java will pass all JUnit tests. * * Recall from Week 1's tutorial videos: * 1. No System.out.println statements should appear here. * Instead, an explicit, final `return` statement is placed for you. * 2. No Scanner operations should appear here (e.g., input.nextInt()). * Instead, refer to the input parameters of this method. * 3. Do not re-assign any of the parameter/input variables. */ // Your implementation of this method starts here.
// Do not modify this return statement. return result; } /* * Input Parameters: * - `seq`: an array of integers * - `indices`: an array of integers (each of which may or may not be a valid index for `seq`) * * Assumptions: * - `seq` may be empty. * - `indices` may be empty. * - `indices` may contain duplicates. * * What to return? * - For each integer value in `indices`, if it is a valid index for input array `seq`, * then use it to index into `seq` and include the result in the output array. * * For example: Say `seq` is {23, 46, 69} and `indices` is {2, -1, 0, 3, 0, 2} * (where indices -1 and 3 are invalid, so only indices 2, 0, 0, 2 are used) * Then the method should return {69, 23, 23, 69} * * Note: Order of elements in the output array corresponds to the order in which * valid indices in the `indices` array are arranged. * * That is, the output array may be empty if the input array `seq` is empty, * or if the input array `indices` does not contain any valid indices. * * See the JUnit tests related to this method.
Must pass these tests
/* * Tests related to Task 2 */ @Test public void test_task2_01() { int[] seq = {1, 2, 3, 4, 5}; int[] expected = {1, 2, 3, 4, 5}; assertArrayEquals(expected, Utilities.task2(seq, 0)); } @Test public void test_task2_02() { int[] seq = {1, 2, 3, 4, 5}; int[] expected = {5, 1, 2, 3, 4}; assertArrayEquals(expected, Utilities.task2(seq, 1)); } @Test public void test_task2_03() { int[] seq = {1, 2, 3, 4, 5}; int[] expected = {2, 3, 4, 5, 1}; assertArrayEquals(expected, Utilities.task2(seq, 4)); } @Test public void test_task2_04() { int[] seq = {1, 2, 3, 4, 5}; int[] expected = {3, 4, 5, 1, 2}; assertArrayEquals(expected, Utilities.task2(seq, 8)); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
