Question: JAVA Arrays 1. Write a method called explode that, for each element in the input array, the frequency of each element is doubled in a
JAVA Arrays 1. Write a method called explode that, for each element in the input array, the frequency of each element is doubled in a new primitive array that is subsequently returned. That is, an input array consisting of 2 3 4 5 results in a new array containing 2 2 3 3 4 4 5 5. 2. Write a method called implode that removes redundant elements from a given sorted array (and inserts zeros at the end of the array). That is, an input array consisting of 2 2 3 3 3 4 5 5 will result in 2 3 4 5 0 0 0 0. 3. Write a method called reverse that reverses the content of a given array (first goes to last, second goes to second-to-last etc.). 4. Write a method called unique that returns true or false if the input array contains only unique values (no duplicates). Do not assume the input array is sorted. 5. Write a method called merge that combines the contents of two sorted input arrays into one sorted array (that is subsequently returned from the method). That is, merging 2 4 7 10 and 1 3 7 11 results in an array containing 1 2 3 4 7 7 10 11. Your implementation must sort as you go: it is not allowed to add both arrays to a single array and then sort that array. 6. Write a method called consolidate that removes all non-positive integers from the given array so that the remaining integer values are in adjacent positions (and zeros fill all space at the end). That is, an input array consisting of 0 2 -1 0 5 7 0 3 10 results in a new array containing 2 5 7 3 10 0 0 0 0. 7. Write a method called search that finds the occurrence of a target in a given array.
SAMPLE CODE MUST MATCH Test 'explode' method aryIn has: 1 2 3 4 5 6 aryOut has: 1 1 2 2 3 3 4 4 5 5 6 6 Test 'implode' method Before processing aryOut has: 1 1 2 2 3 3 4 4 5 5 6 6 After processing aryOut has: 1 2 3 4 5 6 0 0 0 0 0 0 Test 'reverse' method Before processing aryIn has: 1 2 3 4 5 6 After processing aryOut has: 6 5 4 3 2 1 Test 'unique' method aryIn has: 6 5 4 3 2 1 Is it Unique? true aryOut has: 1 2 3 4 5 6 0 0 0 0 0 0 Is it Unique? false Test 'merge' method aryLeft has: 2 4 7 10 aryRight has: 1 3 7 11 After merge: 1 2 3 4 7 7 10 11 Test 'consolidate' method aryIn6 before 'consolidate' has: 0 2 -1 0 5 7 0 3 10 aryIn6 after 'consolidate' has: 2 5 7 3 10 0 0 0 0 Test 'search' method target '7' in aryIn6? (index or -1 if not found) : 2 target '20' in aryIn6? (index or -1 if not found) : -1
SKELETON CODE
public class Arrays {
public static String printArray(int[] ary) {
String result = "";
//To be completed return result;
}
public int[] explode(int[] ary) { int[] ex = new int[ary.length * 2];
//To be completed return ex; }
public void implode(int[] ary) {
//To be completed }
public void reverse(int[] ary) {
//To be completed }
public boolean unique(int[] ary) {
//To be completed return true;
}
public int[] merge(int[] left, int[] right) { int[] merged = new int[left.length + right.length];
//To be completed return merged;
}
public void consolidate(int[] ary) { //To be completed }
int search(int[] array, int target) { int index = 0; boolean found = false;
//To be completed return found ? index : -1; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
