Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

develop a method findClosestElement that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element

develop a method findClosestElement that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element in list that is closest to num and is greater than or equal to num. If all elements are smaller than num then the method should return -1.

For example, given list storing values [20, 30, 25, 89, 14],

findClosestElement(list, 26) should return 1 (index of element 30)

findClosestElement(list, 50) should return 3 (index of element 89)

findClosestElement(list, 90) should return -1 (no elements meet the conditions)

// Returns the index of the element in list that is closest to num and is >= num. // Returns -1 if no elements meet the conditions. public static int findClosestElement(ArrayList list, int num) { /* ADD your code here */ }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

This method iterates through the elements in the ArrayList calculates the distance between each elem... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Java Programs A Back To Basics Approach

Authors: Stuart Reges, Marty Stepp

5th Edition

013547194X, 978-0135471944

More Books

Students also viewed these Programming questions

Question

Describe four common misunderstandings of Gestalt psychology.

Answered: 1 week ago