Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to do this please help ASAP don't use Any Java library class (e.g., ArrayList, Arrays) /* * Input Parameters: * - `seq`: an array

How to do this please help ASAP

don't use Any Java library class (e.g., ArrayList, Arrays)

/* * 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. */ public static int[] task3(int[] seq, int[] indices) { 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. int count = 0, index = 0; for (int i = 0; i < indices.length; i++) { if (indices[i] >= 0 && indices[i] < seq.length) { ++count; } } result = new int[count]; for (int i = 0; i < indices.length; i++) { if (indices[i] >= 0 && indices[i] < seq.length) { result[index++] = seq[indices[i]]; } }

// Do not modify this return statement. return result; }

the unit tests:

/* * Tests related to Task 3 */ @Test public void test_task3_01() { int[] seq = {3, 7, 4, 5}; int[] indices = {2, 1, 3}; int[] result = Utilities.task3(seq, indices); int[] expected = {4, 7, 5}; /* elements at indices 2, 1, 3 of `seq` */ assertArrayEquals(expected, result); } @Test public void test_task3_02() { int[] seq = {3, 7, 4, 5}; int[] indices = {2, -1, 0, 1, -2, 5, 3, 1, 0, 4}; /* invalid indices: -1, -2, 5, 4 */ int[] result = Utilities.task3(seq, indices); int[] expected = {4, 3, 7, 5, 7, 3}; assertArrayEquals(expected, result); } @Test public void test_task3_03() { int[] seq = {}; int[] indices = {2, -1, 0, 1, -2, 5, 3, 1, 0, 4}; int[] result = Utilities.task3(seq, indices); int[] expected = {}; assertArrayEquals(expected, result); } @Test public void test_task3_04() { int[] seq = {3, 7, 4, 5}; int[] indices = {-1, -2, 5, 4}; /* invalid indices: -1, -2, 5, 4 */ int[] result = Utilities.task3(seq, indices); int[] expected = {}; assertArrayEquals(expected, result); }

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

Step: 3

blur-text-image

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

LO5 Illustrate the steps in developing a base pay system.

Answered: 1 week ago

Question

LO3 Outline strategic compensation decisions.

Answered: 1 week ago