Question: Please can you do it in java Requirements: * - It is strictly forbidden for you to use: * + Any Java library class (e.g.,
Please can you do it in java
Requirements: * - It is strictly forbidden for you to use: * + Any Java library class (e.g., ArrayList, Arrays) * (That is, there must not be an import statement in the beginning of this class.)
/* * Input Parameters: * - `seq`: an array of integers that stores the following encoding: * Element at each odd index (e.g., at index 1, at index 3, and so on) specifies * how many times the element at the previous even index (e.g., at index 0, at index 2, and so on) * should repeat. * For example, input sequence {2, 4, 1, 3} encodes that * value 2 (at even index 0) should repeat 4 times (as specified at odd index 1), * and similarly, value 1 should repeat 3 times. * * Assumptions: * - Input array `seq` is not empty and contains at least one element. * * What to return? * - Return an array that decodes the input array. * e.g., Say `seq` is {2, 4, 1, 3}. * Then the method should return an array {2, 2, 2, 2, 1, 1, 1}. * * See the JUnit tests related to this method. */ public static int[] task3(int[] seq) { 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. */
/* * Tests related to Task 3 */ @Test public void test_task3_1() { int[] seq = {3, 1, 1, 3, 2, 4}; int[] expected = {3, 1, 1, 1, 2, 2, 2, 2}; assertArrayEquals(expected, Utilities.task3(seq)); }
@Test public void test_task3_2() { int[] seq = {3, 0, 1, 3, 2, 4}; int[] expected = {1, 1, 1, 2, 2, 2, 2}; assertArrayEquals(expected, Utilities.task3(seq)); } @Test public void test_task3_3() { int[] seq = {3, 0, 1, 0, 2, 0}; int[] expected = {}; assertArrayEquals(expected, Utilities.task3(seq)); }
should pass all these tests
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
