This document provides code to swap the first and second halves of an integer array. It includes a method that takes an integer array as a parameter, calculates the midpoint and length of each half, and swaps the values in the first half with the values in the second half using a temporary variable. It also includes a check method that calls the swap method and returns the modified array to validate the solution works as intended.
Your task is to complete a method that swaps the first and second half.docx
1. Your task is to complete a method that swaps the first and second half of an array of integers. For
example, if the array contains the values
then after calling the method, it should contain the values
If the array contains an odd number of elements, leave the middle element in place. For example,
becomes
Complete the following code:
Solution
public class Swapper { /** This method swaps the first and second half of the given array.
@param values an array */ public void swapFirstAndSecondHalf(int[] values) { final int len =
values.length / 2; final int offset = values.length - len; for (int i = 0; i < len; i++) { int temp =
values[i]; values[i] = values[offset + i]; values[offset + i] = temp; } // This method is used to
check your work public int[] check(int[] values) { swapFirstAndSecondHalf(values); return
values; } }