Explore use of Comparable interface. Use Collections and/or Collection in the so
ID: 3851885 • Letter: E
Question
Explore use of Comparable interface. Use Collections and/or Collection in the solution Apply generics to methods using arrays. · Apply generics to methods using List ADTs. Background reading ZyBooks Chapters 11&12 Generic Methods Use of generic parameters gives us "type erasure," that is, the symbol E,'T. 'K, V (orwhichever may be used), is replaced at compile time with the data type indicated within diamond syntax. Generics can be applied to individual methods in addition to classes. In this lab, our methods, but not our class, will use generic parameters. Instructions for GenMethods.java Write a program GenMethods that has the following generic methods. (Also include a getldentificationString method again) (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static list)Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Sam
*/
public class GenMethods {
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list){
ArrayList<E> newList = new ArrayList<>();
Collections.copy(newList, list);
for (int i = 1; i < newList.size(); i++)
for (int j = 0; j < i; j++){
if (newList.get(i).equals(newList.get(j))){
newList.remove(j);
j--;
i--;
}
}
return newList;
}
public static <E> void suffle (ArrayList<E> list) {
Random rand = new Random(340L);
for (int i =0; i<30; i++) {
int a = rand.nextInt()%list.size();
int b = rand.nextInt()%list.size();
E tmp = list.remove(a);
list.add(a, list.remove(b));
list.add(b, tmp);
}
}
public static <E extends Comparable<E>> E max(ArrayList<E> list){
E max = list.get(0);
for (int i = 1; i<list.size(); i++)
if (list.get(i).compareTo(max) > 0)
max = list.get(i);
return max;
}
public static <E extends Comparable<E>> int linearSearch(E[] list, E key){
for (int i = 0; i<list.length; i++)
if (list[i].compareTo(key) == 0)
return i;
return -1;
}
public static <E extends Comparable<E>> E max(E[] list){
E max = list[0];
for (int i = 1; i<list.length; i++)
if (list[i].compareTo(max) > 0)
max = list[i];
return max;
}
public static <E extends Comparable<E>> E max(E[][] list){
E max = list[0][0];
for (int i = 0; i<list.length; i++)
for (int j = 0; j < list[i].length; j++)
if (list[i][j].compareTo(max) > 0)
max = list[i][j];
return max;
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter n:");
n = sc.nextInt();
Integer[] list = new Integer[n];
LinkedList<Integer> linked = new LinkedList<>();
System.out.println("Enter n numbers");
for (int i = 0; i<n; i++){
list[i] = sc.nextInt();
linked.add(list[i]);
}
System.out.println(Arrays.toString(list));
System.out.println(linked);
System.err.println("Enter key to search:");
int key = sc.nextInt();
int result = linearSearch(list, key);
System.out.println(key + " was found at position " + result);
result = max(list);
System.out.println("Max item is: " + result);
System.out.println("Enter first dimention:");
int m = sc.nextInt();
System.out.println("Enter second dimention:");
int p = sc.nextInt();
Integer[][] list2 = new Integer[m][n];
System.out.println("Enter elements of 2D items:");
for (int i = 0; i<m; i++)
for(int j =0; j<p; j++)
list2[i][j] = sc.nextInt();
for (int i = 0; i < list2.length; i++)
System.out.println(Arrays.toString(list2[i]));
result = max(list);
System.out.println("Max item is: " + result);
}
}
I hope you like the code :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.