Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a static method named stretch that accepts an array of integers as a parameter and returns a new array that is twice as large

image text in transcribed

Write a static method named stretch that accepts an array of integers as a parameter and returns a new array that is 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, 24, 11}; int[] list2 = stretch(list);

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

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

}

// your code goes here

}

Write a static method named stretch that accepts an array of integers as a parameter and returns a new array that is 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,24,11}; int[] list2 = stretch(list); System.out.println (Arrays.toString(list)); // the above prints [18,7,4,24,11] System.out.println (Arrays.tostring(list2)); // the above prints [9,9,4,3,2,2,12,12,6,5] \} // your code goes here \}

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

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions