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

This quiz asks you to complete the two methods described below. Each item to be

ID: 3770261 • Letter: T

Question

This quiz asks you to complete the two methods described below. Each item to be filled in is marked by ?n?. Question n asks you to fill in ?n?. The generic maxElement method returns the maximum element in a collection. If the collection has no elements, then this method should return null. The generic declaration needs to ensure that the compareTo method can be used. public static ?2? maxElement(Collection collection) { T max = null; for (?4? element: ?5?) { if (?6? == null || ?7?.compareTo(?8?) set = new TreeSet(); while (?14? (); for (int i: ?17?) { result.? 18?(list.? 19?(?20?)); } return result; }

Explanation / Answer

/**The method maxElement that takes a Collection class
   * and finds the maximum element in collection and returens
   * max value. The method maxElement extends Comparable interface
   * on T element
   * */
   public static <T extends Comparable<T>>
   T maxElement(Collection <? extends T> collection)
           {
       //Assum max element is starting element
       T max=collection.iterator().next();
       //Comparing th max with collection
       for( T element : collection)
       {
           if(element.compareTo(max)>0)
           {
               max=element;
           }
       }
       //return max value
       return max;
           }

---------------------------------------------------------------------------------------
//Test java program for maxElement

//Generic.java
import java.util.Collection;
import java.util.LinkedList;
public class Generic
{
   public static void main(String[] args)
   {
       // create link list object
       LinkedList<Integer> list = new LinkedList<Integer>();

       // populate the list
       list.add(18);
       list.add(42);
       list.add(45);
       list.add(15);
       list.add(32);
       System.out.println("LinkedList elements");
      
       for (Integer element : list) {
           System.out.println(element);
       }
      
       System.out.println("Maximum Element : ");
       //Calling generic function maxElement with list as argument
       System.out.println(maxElement(list));
      
   }

   /**The method maxElement that takes a Collection class
   * and finds the maximum element in collection and returens
   * max value. The method maxElement extends Comparable interface
   * on T element
   * */
   public static <T extends Comparable<T>>
   T maxElement(Collection <? extends T> collection)
           {
       //Assum max element is starting element
       T max=collection.iterator().next();
       //Comparing th max with collection
       for( T element : collection)
       {
           if(element.compareTo(max)>0)
           {
               max=element;
           }
       }
       //return max value
       return max;
           }
}
-----------------------------------------
Sample output:
LinkedList elements
18
42
45
15
32
Maximum Element :
45

--------------------------------------------------------------------------------------
/**
   * The method randomSubset that takes the List of type integer
   * values and takes a n value as input arguments.
   * The method that returns the random set of vlaues
   * */
   public static <T> List <? extends Number>
   randomSubset(List<Integer> list, int n)
           {
       //Create an instance of Random class
       Random random=new Random();
       //Create a Set object
       Set<Integer> set= new TreeSet<Integer>();
      
       //Add random values to set
       while(set.size()!=n)
       {
           set.add(random.nextInt(list.size()));
          
       }
      
       //Create a list of type Integer
       List<Integer>result=new ArrayList<Integer>();
      
       //Add list elements to the result from randomely
       //generated set
       for( int i : set)
       {
           result.add((Integer) list.get(i));
       }
       //return result
       return result;
   }

-------------------------------------------------------------------------------------------
//Sample test program for randomSubset

//Generic2.java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class Generic2
{
   public static void main(String[] args)
   {
       // create link list object
       List<Integer> list = new ArrayList<Integer>();

       // populate the list
       list.add(-18);
       list.add(40);
       list.add(-45);
       list.add(12);
       list.add(25);
       list.add(22);
      
       System.out.println("Random set : ");
       System.out.println(randomSubset(list,2));
      
      
      
   }

   /**
   * The method randomSubset that takes the List of type integer
   * values and takes a n value as input arguments.
   * The method that returns the random set of vlaues
   * */
   public static <T> List <? extends Number>
   randomSubset(List<Integer> list, int n)
           {
       //Create an instance of Random class
       Random random=new Random();
       //Create a Set object
       Set<Integer> set= new TreeSet<Integer>();
      
       //Add random values to set
       while(set.size()!=n)
       {
           set.add(random.nextInt(list.size()));
          
       }
      
       //Create a list of type Integer
       List<Integer>result=new ArrayList<Integer>();
      
       //Add list elements to the result from randomely
       //generated set
       for( int i : set)
       {
           result.add((Integer) list.get(i));
       }
       //return result
       return result;
   }
}

--------------------------------------------------------------------------------------------------------
Sample Output:
Random set :
[-45, 25]

Random set :
[-45, 22]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote