Need help Generic Methods Use of generic parameters gives us \"type erasure,\" t
ID: 3708870 • Letter: N
Question
Need help
Generic Methods
Use of generic parameters gives us "type erasure," that is, the symbol 'E', 'T', 'K', 'V' (or whichever 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.
(1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list.
(2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand = new Random(340L); ), and it should do 30 of these swaps (this number was chosen arbitrarily by us for testing purposes).
(3) Write the following method that returns the largest element in an ArrayList:
(4) Implement the following generic method for linear search.
(5) Implement the following method that returns the maximum element in an array:
(6) Implement a generic method that returns the maximum element in a two-dimensional array.
(7) Write the following main() method that tests the above methods following this guide:
Explanation / Answer
CODING:
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.