Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA QUESTION Please provide a demo program(s) that implement and used the metho

ID: 3869767 • Letter: J

Question

JAVA QUESTION

Please provide a demo program(s) that implement and used the methods: A generic method that returns the minimum element in an array. public static> E min(E[ ] list) A generic method that returns the maximum element in a two-dimensional array. public static > E max(E[ ][ ] list) A generic method that returns a new ArrayList. The new list contains the non-duplicate elements from the original list. public static ArrayList removeDuplicates(ArrayList list) Note: You can have a demo for each method Example: (Maximum element in an array) Implement a generic method that returns the maximum element in an array. public static > E max(E[ ] list) Implementation: /* Maximum element in an array. Implement the following method that returns the maximum element in an array. public static > E max(E[ ] list) */ public class Demo_max { public static void main(String[] args) { Integer[] numbers = { 1, 2, -3, 7, 5, 4 , 11,12, 87, 23, -1, 0, 3}; for (int i=0;i> E max(E[] list) { E max = list[0]; for (int i = 1; i < list.length; i++) { if (max.compareTo(list[i]) < 0) { max = list[i]; } } return max; } }

Explanation / Answer

Go through the code first, then read the comments appropriate to the code.

Main method contains some example method calls for better understanding.

import java.util.*;

public class GenericMethods {

/* Generic method is a method that contains data any object type. Such methods can be used generically for Strings Integer etc.

* Comparable is an Interface that contains compareTo method.

* x.CompareTo(y) compares x and y. returns -1 if x<y, 0 if x==y and 1 if x>y

* <> is used to denote Generics. Here T is a placeholder for any data type to be supported by that method.

* For removing duplicates, All the arraylist elements are added to Hashset. HashSet supports no duplicate elements.

* So all duplicates are deleted when hashset objects are copied into arraylist again.

* Here the order of arraylist is disrupted but duplicates are removed.

* In main method : Integer is used instead of int because compareTo takes only objects where as int is a primitive data type.

*/

public static <T extends Comparable<T>> T maxObj(T[] arr){

T max = arr[0];

for(T element : arr){

if(element.compareTo(max) > 0){

max = element;

}

}

return max;

}

public static <T extends Comparable<T>> T minObj(T[] arr){

T max = arr[0];

for(T element : arr){

if(element.compareTo(max) < 0){

max = element;

}

}

return max;

}

public static <T extends Comparable<T>> ArrayList<T> removeDuplicates(ArrayList<T> arr){

Set<T> hs = new HashSet<T>();

hs.addAll(arr);

arr.clear();

arr.addAll(hs);

return arr;

}

public static void main(String[] args) {

// Examples :

Integer[] arr = {1,2,3,4,5,6,20,1,2};

String[] arr2 = {"uaA","aa","sdw","a"};

ArrayList<Integer> al = new ArrayList<Integer>();

al.add(20);

al.add(27);

al.add(35);

al.add(20);

System.out.println(maxObj(arr));

System.out.println(maxObj(arr2));

System.out.println(minObj(arr));

System.out.println(minObj(arr2));

System.out.println(removeDuplicates(al));

}

}