Question: A) 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) 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.

B) 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, but hasXOfAKind(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.

C) 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!