Question: The function A5Q7 should take in an ArrayList of Strings called words as a parameter, and return the middle value in the list. If the

The function A5Q7 should take in an ArrayList of Strings called words as a parameter, and return the middle value in the list. If the middle item is a even (list size 4) then return the rounded item (3). So for { A,B,C } return B; for { A,B,C, D } return C; Remember indexing is from 0.

THIS IS WHAT IVE DONE SO FAR IT IS NOT CORRECT :

The function A5Q7 should take in an ArrayList of Strings called words

public String A5Q7 ( ArrayList words) { int middle = words.size() / 2; //finding the middle index of the list if (words.size() % 2 == 0) { middle = middle - 1; } //handling case for even sized lists return words.get(middle); }

public static void main(String[] args) { ArrayList str = new ArrayList(); //adding values to list str.add("A"); str.add("B"); str.add("C"); //calling the getMiddleValue function and printing the output System.out.println(getMiddleValue(str));

str.add("D"); //adding one more element to test for even size list System.out.println(getMiddleValue(str)); }

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!