Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a pro
ID: 3794469 • Letter: F
Question
Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a program that prints out the lists alter the following methods are called. Method takes an of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: [4. 7, 9, 2. 7, 7. 5. 3, 5. 1, 7, S. 6, 7] and you make this call it should store the following values after the call: [1. 4. 7, 9. 2.7. 7. 5. 3. 5. 7. S. 6. 7], You may assume that the list stores at least one value. After the call print the list. Method accepts an of integers and two integer values min and max as parameters and removes all elements whose values are ill the range min through max (inclusive) from the list. For example, if a variable called list stores the values: [1. 4, 7, 9. 2, 7. 7, 5, 3, 5, 7, S, 6, 7] The call of should remove all values between 5 and 7, therefore it should change the list to store [1, 4, 9, 2. 3, 8]. You may assume that the list is not null. After the call print the list. Method intersect accepts two sorted array lists of integers as parameters and returns a new list that contains only the elements that are found in both lists. For example, if lists named list land list2 initially store: [1.4, 8.9, 11, 15, 17, 28, 41, 59] [4. 7, 11, 17, 19, 20, 23, 28, 37, 59, 81] Then the call of returns the list: [4, 11, 17, 28, 59] Print the list that is returned by this method.Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ListUtil {
public List<Integer> minToFront(List<Integer> list) {
List<Integer> copy = new ArrayList<Integer>(list);
int min = Collections.min(copy);
copy.remove(min);
copy.add(0, min);
return copy;
}
public List<Integer> filterRange(List<Integer> list, int min, int max) {
List<Integer> copy = new ArrayList<Integer>(list);
Iterator<Integer> itr = copy.iterator();
while(itr.hasNext()) {
int value = itr.next();
if(value == min || value == max || (value > min && value < max))
itr.remove();
}
return copy;
}
public List<Integer> intersect(List<Integer> list1, List<Integer> list2) {
List<Integer> copy1 = new ArrayList<Integer>(list1);
List<Integer> copy2 = new ArrayList<Integer>(list2);
List<Integer> intersection = new ArrayList<Integer>();
Collections.sort(copy1);
Collections.sort(copy2);
Iterator<Integer> itr1, itr2;
if(copy1.size() > copy2.size()) {
itr1 = copy1.iterator();
itr2 = copy2.iterator();
} else {
itr1 = copy2.iterator();
itr2 = copy1.iterator();
}
int i = itr1.next();
int j = itr2.next();
while(itr1.hasNext()) {
if(i == j) {
if(!intersection.contains(i)) {
intersection.add(i);
i = itr1.next();
j = itr2.next();
}
}
else if(i < j)
i = itr1.next();
else
j = itr2.next();
}
intersection.add(i);
return intersection;
}
public static void main(String[] args) {
ListUtil listUtil = new ListUtil();
List<Integer> list = Arrays.asList(new Integer[] {4, 7, 8, 9, 1});
List<Integer> list1 = Arrays.asList(new Integer[] {4,7,1,3,5});
System.out.println("After minimum to Front:" + listUtil.minToFront(list));
System.out.println("After filtered via range:" + listUtil.filterRange(list, 5, 7));
System.out.println("After intersection:" + listUtil.intersect(list, list1));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.