This quiz asks you to complete the two methods described below. Each item to be
ID: 3770596 • 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 <T extends ?1?> ?2? maxElement(Collection<?3?> collection) {
T max = null;
for (?4? element : ?5?) {
if (?6? == null || ?7?.compareTo(?8?) < 0) {
?9? = ?10?;
}
}
return max;
}
The generic randomSubset method returns a random subset of the given list. The subset should have n elements, where n is given. You should assume that the size of the list is at least n. The method is supposed to work by first selecting n random indexes with no duplicates, then it creates a list from the elements at those indexes.
public static <T> List<?11?> randomSubset(List<?12?> list, int n) {
// create a set with n randomly chosen indexes into the list
Random random = new Random();
Set<?13?> set = new TreeSet<?13?>();
while (?14? < n) {
set.add(random.nextInt(?15?));
}
// add n elements to the result
List<?16?> result = new ArrayList<?16?>();
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 returns
* max value. The method “maxElement()”extends Comparable interface
* on “T”element
* */
public static <T extends Comparable<T>>
T maxElement(Collection <? extends T> collection)
{
//Assume maximum element is starting element
T max=collection.iterator().next();
//Comparing the maximum element with collection
for( T element : collection)
{
if(element.compareTo(max)>0)
{
max=element;
}
}
//return max value
return max;
}
---------------------------------------------------------------------------------------
//Write a java program to test the method “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 values
* */
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]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.