Write a public static method called averageOfTwo that takes an ArrayList as a pa
ID: 3674319 • Letter: W
Question
Write a public static method called averageOfTwo that takes an ArrayList as a parameter, and returns another ArrayList. The returned ArrayList should have a size that is 1/2 the size of the original. The elements of the returned ArrayList should be the average of 2 elements from the original ArrayList. For example, if the values {1, 2, 3, 4, 5, 9} are passed in, averageOfThree should return the list {1.5, 3.5, 7.0}. You may ignore the case where the original ArrayList has an odd number of elements.Explanation / Answer
/* method to find average of consecutive elements in aaray and store in another array and return it */
public static ArrayList<Double> averageTwo( ArrayList<Double> numbers, int length) {
ArrayList<Double> average = new ArrayList<Double>();
int i,j=0;
for ( i = 0; i < length/2 ; i=i+2 ) {
average.get(j) = (numbers.get(i)+numbers.get(i+1))/2;
j++;
}
return (number);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.