Question

****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

****WRITE A JAVA PROGRAM THAT :

Write a method named stretch that accepts an array of integers (that the user inputs) 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, (bigger number first)

-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 (again, the bigger number first)

Be sure to have a stretch() method

*All variable names are completely up to you, as long as they’re descriptive

*Use the correct data types

*Use comments to explain what you’re coding

*Put your name in a comment

******** PLEASE CODE SO THAT I BEGINNER CAN UNDERSTAND

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks!
===========================================================================

import java.util.Arrays;

public class TestProgram {


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

        // create an array that has size twice that of numbers
        int[] array = new int[numbers.length * 2];

        // create an int variable to track the index of the element in array
        int index = 0;

        // we need to iterate each element in the numbers
        for (int i = 0; i < numbers.length; i++) {
            // check the number is even
            if (numbers[i] % 2 == 0) {
                // half the current number and insert them in the two spots
                // incrementing the index by 1 each time
                array[index++] = numbers[i] / 2;
                array[index++] = numbers[i] / 2;
            } else {
                // for odd case
                int number = numbers[i] / 2; // this will the lower number
                array[index++] = number + 1;
                array[index++] = number;
            }

        }


        // return this array
        return array;
    }

    public static void main(String[] args) {

        int[] numbers = {18, 7, 4, 24, 11};
        System.out.println(Arrays.toString(numbers));
        System.out.println("After strech");
        int[] strechedNumbers = stretch(numbers);
        System.out.println(Arrays.toString(strechedNumbers));
    }
}

======================================================================

1: Proje public static int[] stretch(int[] numbers) { // create an array that has size twice that of numbers int[] array = ne

Add a comment
Know the answer?
Add Answer to:
****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • What I need: Write a program named Averages that includes a method named Average that accepts...

    What I need: Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. For example, if 7 and 4 were passed to the method, the ouput would be: 7 4 -- Average is 5.5 Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers. What I...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • Preferred in Java Write a method called print Array With Total that accepts a reference to...

    Preferred in Java Write a method called print Array With Total that accepts a reference to a two - dimensional array of integers and two primitive integers called number Of Rows and number OF Columns as parameters and displays the elements of two - dimensional array as a table with an extra column for the row total as the rightmost column and an extra row for the column totals as the bottom row. (Please do NOT call either definition of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT