Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java sortArrayList Code a method called sortArrayList, that takes an ArrayList of Integers as a parameter and returns another ArrayList of the same size. The
Java
sortArrayList
Code a method called sortArrayList, that takes an ArrayList of Integers as a parameter and returns another ArrayList of the same size. The method is to sort the elements in the ArrayList in descending order.
Assume the ArrayList always has more than two elements.
Example 1
ArrayList: {12, 80} return: {80, 12 }
Example 2
ArrayList: {12, 80, 109, -2} return: {109, 80, 12, -2 }
getPairWithClosestToN
Code a method called getPairWithClosestToN, that takes an array of integers and an integer, n as parameters and prints out the pair of elements in the array that have the summation closest to n. The pairs are formed by first and second elements, third and fourth elements, and so on. Assume that all elements are positive numbers.
Your code must print out a warning statement if the length of the array is smaller than 2, or is an odd number.
Example 1
array: {1, 8, 2, 4, 5, 2}, n:5
Print out: Pair that have sum closest to 5 is 2 and 4.
Example 2
array: {2}, n:5
Print out: Invalid input.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started