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

(Intro to Java help?) Write a static method named stretch that accepts an array

ID: 3699325 • Letter: #

Question

(Intro to Java help?)

Write a static method named stretch that accepts an array of integers as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. For example, if a variable named list refers to an array storing the values {18, 7, 4, 24, 11}, the call of stretch(list) should return a new array containing {9, 9, 4, 3, 2, 2, 12, 12, 6, 5}. (The number 18 is stretched into the pair 9, 9, the number 7 is stretched into 4, 3, the number 4 is stretched into 2, 2, the number 24 is stretched into 12, 12 and the number 11 is stretched into 6, 5.)

Test your code with the following class:

import java.util.*;

public class TestStretch {

    public static void main(String[] args) {

        int[] list = {18, 7, 4, 14, 11};

        int[] list2 = stretch(list);

        System.out.println(Arrays.toString(list)); // [18, 7, 4, 24, 11]

        System.out.println(Arrays.toString(list2)); // [9, 9, 4, 3, 2, 2, 7, 7, 6, 5]

    }

    // your code goes here

}

Explanation / Answer

First of all your test case is not correct it should be [18, 7, 4, 14, 11] but you have written [18, 7, 4, 24, 11].

Have a look at your desired function:?

public static int[] stretch(int[] arr) {

//Check if it has some values or not

if(arr.length<=0)

return null;

//Get a new array of the double of the size

int new_arr[] = new int[arr.length*2];

int j =0;

for(int i = 0;i<arr.length;i++) {

//If number is even

if(arr[i]%2==0) {

new_arr[j++] = arr[i]/2;

new_arr[j++] = arr[i]/2;

}

//If number is odd

else {

new_arr[j++] = (arr[i]/2)+1;

new_arr[j++] = arr[i]/2;

}

}

return new_arr;

}

Code is well commented and easy to understand. Here I am attaching complete code with output:

import java.util.*;

public class TestStretch {

public static void main(String[] args) {

int[] list = {18, 7, 4, 14, 11};

int[] list2 = stretch(list);

System.out.println(Arrays.toString(list)); // [18, 7, 4, 24, 11]

System.out.println(Arrays.toString(list2)); // [9, 9, 4, 3, 2, 2, 7, 7, 6, 5]

}

public static int[] stretch(int[] arr) {

//Check if it has some values or not

if(arr.length<=0)

return null;

//Get a new array of the double of the size

int new_arr[] = new int[arr.length*2];

int j =0;

for(int i = 0;i<arr.length;i++) {

//If number is even

if(arr[i]%2==0) {

new_arr[j++] = arr[i]/2;

new_arr[j++] = arr[i]/2;

}

//If number is odd

else {

new_arr[j++] = (arr[i]/2)+1;

new_arr[j++] = arr[i]/2;

}

}

return new_arr;

}

}

Output:

[18, 7, 4, 14, 11]
[9, 9, 4, 3, 2, 2, 7, 7, 6, 5]