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

write the method subArray (int[] arr, int start, int end), which has an array an

ID: 3632648 • Letter: W

Question

write the method subArray (int[] arr, int start, int end), which has an array and two intergers as a parameters, and returns a new array conssiting of all of the elements between positions begin and end of its parameter array arr (including the one at position begin, but NOT the one at postion end) For example, subArray(new int [] {1, 3, 5, 7, 9, 11}, 2, 5) should return a new array with the elements 5, 7, and 9. the parameter array should not be modified.
Note: if the values begin and end are valid positions in the array, but there are no elements between them, the method should just return null, rather than an array. if either begin and end are not valid positions in the array then null should also be returned.

write the method void reverse(int [] arr), which should reverse all of the elements of its parameter array arr. note that the method is modifying elements of its parameter array, and not returning any value. for example if its parapemet array contained the values (in order) 2, 4, 6, 8 then after this method is called it should have the values 8,6,4, and 2.

Please write j unit test for each method also.

Explanation / Answer

public static int[] subArray(int[] arr, int start, int end)
{

if(start >=end)


return null;

if(start > arr.length() || end > arr.length())

return null;

int[] output = new int[end-start];

for(int i = start; i < end; i++)
output[i-start] = arr[i];

return output;
}