Question: I need help with this Java question. Please provide the final code and output if possible. Part 1: Using Iterators and ListIterators Write the following
I need help with this Java question. Please provide the final code and output if possible.
Part 1: Using Iterators and ListIterators
Write the following methods. Note that most of the methods specify a required running time. Keep in mind that the get(int index) method of a List can run in O(1) time or in O(n) time, depending on the lists implementation details. (If we have an ArrayList, O(1); if a LinkedList, O(n).) Do not make any assumptions about which kind of list is passed to the method.
1.) public static void doubleEvensAndTripleOdds(List list): Modifies the elements of the given list as follows. For each element, if the element is even, multiply it by 2; otherwise, multiply it by three. For example, if list is originally [2, 4, 3, 5], then after performing doubleEvensAndTripleOdds(list) the list should be [4, 8, 9, 15]. The running time of this method should be O(n), where n is list.size().
2.) public static void reverseMomentum(List list): Modifies the given list as follows. The last element remains the same; the second-to-last element becomes the sum of the last two elements; the third-to-last element becomes the sum of the last three elements; etc. That is, each element should be replaced by the sum of all the elements from the current element and on. For example, if list is originally [2, 4, 3, 1], then after performing reverseMomentum(list) the list should be [10, 8, 4, 1]. The running time of this method should be O(n), where n is list.size().
3.) public static List> associationList(List list1, List list2): Returns the association list of list1 and list2. For example, if list1 is [1, 2, 3] and list2 is [one, two, three], the method should return the list [(1, one), (2, two), (3, three)]. Assume that list1 and list2 have the same size. The running time of this method should be O(n), where n is list1.size() (and also list2.size()). Assume that the Pair class is defined as follows:
class Pair {
private U first;
private V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
public U getFirst() {
return first;
}
public V getSecond() {
return second;
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}
4.) public static void removeInRangeIf(List list, int start, int end, Predicate super E> predicate): Removes all elements in the specified list from index start (inclusive) to index end (exclusive) that satisfy the given predicate. For example, if originally list is a List representing [4, 5, 6, 7, 8, 9, 10] and we then say removeInRangeIf(list, 2, 6, integer -> integer % 2 == 0), now list should be [4, 5, 7, 9, 10]. Use a plain Iterator or a ListIterator.
5.) public static boolean containsAnyConsecutiveDuplicates(List list): Determines whether the list contains any consecutive duplicate elements. For example, the list ["cat", "dog", "dog", "table"] contains consecutive duplicate elements, as does the list [3, 1, 1, 7, 4, 4]. However, the list [3, 1, 6, 3, 6] does not contain any consecutive duplicate elements. The running time of this method should be O(n), where n is list.size().