Question: The methods 1. Write a method with the header public static void process(int[] vals) that takes a reference to an array of integers and modifies

The methods

1. Write a method with the header

public static void process(int[] vals) 

that takes a reference to an array of integers and modifies its internals as follows:

If an element is even, it should be doubled.

If an element is odd, it should be negated (i.e., multiplied by -1).

For example, if you add the following test code to your main method:

int[] a1 = {1, 2, 6, 5, -8, -10, -11}; ArrayMethods.process(a1); System.out.println(Arrays.toString(a1)); 

you should see the following output:

[-1, 4, 12, -5, -16, -20, 11] 

Special case: If the parameter is null, the method should throw an IllegalArgumentException.

2. Write a method with the header

public static String[] extractBU(String[] words) 

that takes a reference to an array of strings, and that creates and returns a new array that contains the strings from the original array whose first letter is either 'b', 'B', 'u' or'U'.

For example:

String[] a2 = {"Build", "up", "your", "best", "self"}; String[] a3 = ArrayMethods.extractBU(a2); System.out.println(Arrays.toString(a3)); 

should display:

[Build, up, best] 

Important:

You may not use any of the built-in methods for copying or extracting a portion of an array.

You must not modify the original array.

The new array (the one that you will return) should not have any extra unused elements. As a result, we recommend making an initial pass through the original array to determine how long the new array should be. Then you can create the new array and make a second pass through the original array to find and copy the appropriate strings.

Special case: If the parameter is null, the method should throw an IllegalArgumentException.

3. Write a method with the header

public static void moveToEnd(int[] vals, int n) 

that takes a reference to an array of integers vals and a non-negative integer n, and that modifies the internals of vals so its first n elements are moved to the end of the array and the remaining elements are shifted to the beginning.

Important: You may not use any of the built-in methods for copying or extracting a portion of an array.

For example:

int[] a4 = {1, 2, 3, 4, 5, 6, 7, 8}; ArrayMethods.moveToEnd(a4, 3); System.out.println(Arrays.toString(a4)); 

should display:

[4, 5, 6, 7, 8, 1, 2, 3] 

Special cases:

If the first parameter is null, if n is negative, or if the length of the array is less than n, the method should throw an IllegalArgumentException.

If n is 0 or the length of the array is equal to n, the method can simply return without making any changes.

Hint: We recommend creating one or more temporary arrays that you can use to store values from the original array.

4. Write a method with the header

public static boolean hasXOfAKind(int[] vals, int x) 

that takes a reference to an array of integers vals and an integer n, and that determines if there is at least one number that appears at least x times (i.e., x or more times) in vals, returning true if there is such a number and false otherwise.

For example, consider the following arrays:

int[] vals1 = {2, 8, 4, 8, 5, 6, 8}; int[] vals2 = {9, 6, 9, 7, 9, 7, 9, 9}; 

Given these arrays:

The method call hasXOfAKind(vals1, 3) should return true, because there is a number (8) that appears 3 times in vals1. Similarly, hasXOfAKind(vals1, 2) should return true, because 8 appears at least 2 times in vals1.

The method call hasXOfAKind(vals1, 4) should return false, because there is no number that appears 4 or more times in vals1.

The method call hasXOfAKind(vals2, 5) should return true, because there is a number (9) that appears 5 times in vals2. Similarly, hasXOfAKind(vals2, 4), hasXOfAKind(vals2, 3), and hasXOfAKind(vals2, 2) should all return true, buthasXOfAKind(vals2, 6) should return false.

Special cases:

If the first parameter is null or the second parameter is less than 1, the method should throw an IllegalArgumentException.

If the first parameter refers to an array of length 0, the method should return false.

If vals has at least one element and x is 1, the method should always return true.

Important: You must not modify the original array.

5. Write a method with the header

public static int[] combine(int[] vals1, int[] vals2) 

that takes two arrays of integers vals1 and vals2 as parameters, and that creates and returns a new array in which each element is the sum of the corresponding elements from vals1 and vals2.

For example:

int[] a5 = {1, 2, 3, 4}; int[] a6 = {5, 6, 7, 8}; int[] a7 = ArrayMethods.combine(a5, a6); System.out.println(Arrays.toString(a7)); 

should display:

[6, 8, 10, 12] 

If one of the input arrays is longer than the other, its extra elements should appear at the end of the new array. For example:

int[] a5 = {1, 2, 3, 4}; // same array as above int[] a8 = {5, 6, 7, 8, 9, 11}; int[] a9 = ArrayMethods.combine(a5, a8); System.out.println(Arrays.toString(a9)); 

should display:

[6, 8, 10, 12, 9, 11] 

Special case: If either parameter is null or has a length of 0, the method should throw an IllegalArgumentException.

Important: You must not modify the original arrays.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!