Answer:
The method in Java is as follows:
public static void collapse(int [] myarr) {
int [] arr = new int[myarr.length/2];
int k = 0;
for (int i = 0;i<myarr.length;i+=2){
arr[k] = myarr[i]+myarr[i+1];
k++;
}
for (int i = 0;i<myarr.length/2;i++){
System.out.print(arr[i]+" ");
}
}
Explanation:
This defines the collapse method
public static void collapse(int [] myarr) {
This declares a new array
int [] arr = new int[myarr.length/2];
This declares and initializes k to 0
int k = 0;
This iterates through the array passed as argument
for (int i = 0;i<myarr.length;i+=2){
This calculates the elements of the new array
arr[k] = myarr[i]+myarr[i+1];
k++;
}
The following iteration returns and prints the elements of the new array
for (int i = 0;i<myarr.length/2;i++){
System.out.print(arr[i]+" ");
}
See attachment for complete program which includes the main