Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method combineArray which has four parameters. The first two parameters

ID: 3723531 • Letter: W

Question

Write a method combineArray which has four parameters. The first two parameters are String arrays. The last two parameters are integers, which represent the positions where we will combine the String arrays. This method will return a String array. This method will create a new String array to hold Strings from the first and second String array. The new array must contain the Strings from the first String array, from the first position up to (and including) the index specified by the first integer parameter. As well, the new array must contain the 1Note that an “a” has been added to make the quote complete. Page 7 Strings from the second String array from the second integer parameter (but not including) until the end of the second String array. For example, let the first String array be { ‘‘Hello,’’, ‘‘how’’, ‘‘are’’, ‘‘you’’, ‘‘doing?’’} and the first integer parameter be 2. As well, let the second String array be { ‘‘I’’, ‘‘see’’, ‘‘two’’, ‘‘giant’’, ‘‘rhinos!!’’} and the second integer parameter be 1. Then, the returned String array must contain { ‘‘Hello,’’, ‘‘how’’, ‘‘are’’, ‘‘two’’, ‘‘giant’’, ‘‘rhinos!!’’}.

Explanation / Answer

CombineArrayTest.java

import java.util.Arrays;

public class CombineArrayTest {

public static void main(String[] args) {

String s1[]= { "Hello,", "how", "are", "you", "doing?"};

String s2[] = { "I", "see", "two", "giant", "rhinos!!"};

int n1 = 2;

int n2 = 1;

System.out.println(Arrays.toString(combineArray(s1, s2, n1, n2)));

}

public static String[] combineArray(String s1[], String s2[], int n1, int n2) {

String s[] = new String[2+s2.length-n2];

int k=0;

for(int i=0;i<=n1;i++) {

s[k++] = s1[i];

}

for(int i=n2+1;i<s2.length;i++) {

s[k++] = s2[i];

}

return s;

}

}

Output:

[Hello,, how, are, two, giant, rhinos!!]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote