****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
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](http://img.homeworklib.com/questions/50306d60-eee5-11eb-b884-d9897627c503.png?x-oss-process=image/resize,w_560)
****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...
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 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,...
Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...
Write in Java (10 pts) Write a public method named “Even_Sum” that accepts a one-dimensional array of integers and returns the sum of the even elements in the array (10 pts) Write a public method named “Reverse” that receives a string prints its contents in reverse to the standard output using a stack. Assume the stack has been created and is empty.
Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10}; int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]
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...
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...
JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...
Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...
Write a static, void method named replaceAll that accepts an array of int's named numbers as well as an int named num. The method must replace all occurrences of num . that are found in even-numbered index portions with the value of 3. That is, if the value num is stored in numbers [4] then it must be replaced with the value of 3 since 4 is an even number. But if num is found in numbers[5], it must not...