Complete the method named middleValue in the program below n

Complete the method named middleValue, in the program below, named ArrayOps.java, so that it accepts an array of integer values as a parameter, and returns the value in the middle of the array.

For instance, if an array contains five elements, then the third element would be the middle element to be returned. However, if the array contains six elements, both the third and fourth values could be considered to be the middle elements. In this case, only return the first middle value, namely, the third element of the array.

Complete the following code:

Solution

package genericitynew;

public class ArrayOps {


static <T> T midpoint (T[] A) {
assert A.length >= 1;
return A[A.length/2];
}
<T> int nonNullLength (T[] A) {
int count = 0;
for (T t : A) {
if (t != null) {
count++;
}
}
return count;
}

public static void main(String[] args) {
String result;
int count;
ArrayOps arrayWorker = new ArrayOps();

result = ArrayOps.midpoint(args);
result = ArrayOps.<String>midpoint(args);
count = arrayWorker.nonNullLength(args);
count = arrayWorker.<String>nonNullLength(args);

System.out.println(result + count);
}
}

Complete the method named middleValue, in the program below, named ArrayOps.java, so that it accepts an array of integer values as a parameter, and returns the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site