Starting from:

$30

COMP2210-Assignment 2 Collection Selector Solved

This assignment focuses on implementing the methods of a class much like java.util.Collections. The Selector.java file defines a class with static methods that implement polymorphic algorithms that operate on and/or return Collections. Each method of Selector is very clearly specified, is independent of the other methods in the class, and is designed to provide relatively simple functionality. So, this is a great context for practicing what we’ve been discussing in lecture – generalized programming and test-based verification.

The Selector class
You must correctly implement all the method bodies of the provided Selector class. Your implementation must adhere exactly to the API of the Selector class, as described in the provided source code comments and as described below.

public class Selector { public static <T T min(Collection<T c, Comparator<T comp) public static <T T max(Collection<T c, Comparator<T comp) public static <T T kmin(Collection<T c, int k, Comparator<T comp) public static <T T kmax(Collection<T c, int k, Comparator<T comp) public static <T Collection<T range(Collection<T c, T low, T high,

Comparator<T comp)

public static <T T ceiling(Collection<T c, T key, Comparator<T comp) public static <T T floor(Collection<T c, T key, Comparator<T comp)

}

The sections that follow provide details of each method’s behavior as well as specific examples.

The min method.

This method selects the minimum value from a given collection, as defined by a given comparator. If either the collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty, the method throws a NoSuchElementException. The collection must not be changed by this method.

Examples:

T
c
comp
<Tmin(c, comp)
Integer
[2, 8, 7, 3, 4]
ascending order
2
Integer
[5, 9, 1, 7, 3]
descending order
9
Integer
[8, 7, 6, 5, 4]
ascending order
4
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
ascending by String field
(A,5)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
ascending by Integer field
(E,1)
1

The max method.

This method selects the maximum value from a given collection, as defined by a given comparator. If either the collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty, the method throws a NoSuchElementException. The collection must not be changed by this method.

Examples:

T
c
comp
<Tmax(c, comp)
Integer
[2, 8, 7, 3, 4]
ascending order
8
Integer
[5, 9, 1, 7, 3]
descending order
1
Integer
[8, 7, 6, 5, 4]
ascending order
8
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
ascending by String field
(E,1)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
ascending by Integer field
(A,5)
The kmin method.

This method selects the kth minimum value from a given collection, as defined by a given comparator. A value is the kth minimum if there are exactly k−1 values less than it in the collection. If either the collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty or if there is no kth minimum value, this method throws a NoSuchElementException. Note that there is no kth minimum value if k is less than 1, k is greater than the number of elements in the collection, or if k is greater than the number of distinct values in the collection. The collection must not be changed by this method.

Examples:

T
c
k
comp
<Tkmin(c, k, comp)
Integer
[2, 8, 7, 3, 4]
1
ascending order
2
Integer
[5, 9, 1, 7, 3]
2
descending order
7
Integer
[8, 7, 6, 5, 4]
3
ascending order
6
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
4
ascending by String field
(D,2)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
2
ascending by Integer field
(D,2)
The kmax method.

This method selects the kth maximum value from a given collection, as defined by a given comparator. A value is the kth maximum if there are exactly k −1 values greater than it in the collection. If either the collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty or if there is no kth minimum value, this method throws a NoSuchElementException. Note that there is no kth minimum value if k is less than 1, k is greater than the number of elements in the collection, or if k is greater than the number of distinct values in the collection. The collection must not be changed by this method.

Examples:

T
c
k
comp
<Tkmax(c, k, comp)
Integer
[2, 8, 7, 3, 4]
1
ascending order
8
Integer
[5, 9, 1, 7, 3]
2
descending order
3
Integer
[8, 7, 6, 5, 4]
3
ascending order
6
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
4
ascending by String field
(B,4)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
2
ascending by Integer field
(B,4)
The range method.

This method returns a collection of all values i from a given collection such that low ≤ i ≤ high, as defined by the given comparator, including duplicate values. (Note that low and high do not have to be actual values in the given collection.) The returned collection contains only values in the range [low..high], and no others. If there are no qualifying values, including the case where c is empty, this method throws a NoSuchElementException. This method throws an IllegalArgumentException if either the collection or comparator is null. The collection is not changed by this method.

Examples:

T
c
low
high
comp
<Trange(c, low, high, comp)
Integer
[2, 8, 7, 3, 4]
1
5
ascending order
[2,3,4]
Integer
[5, 9, 1, 7, 3]
3
5
ascending order
[5,3]
Integer
[5, 9, 1, 7, 3]
5
3
descending order
[5,3]
Integer
[8, 7, 6, 5, 4]
4
8
ascending order
[8, 7, 6, 5, 4]
(String, Integer)
[(A,5), (B,4), (C,3)]
(B,3)
(C,5)
asc. by String field
[(B,4),(C,3)]
(String, Integer)
[(A,5), (B,4), (C,3)]
(F,4)
(G,7)
asc. by Integer field
[(A,5),(B,4)]
The ceiling method.

This method returns the smallest value i in a given collection such that i ≥ key, as defined by the given comparator. (Note that key does not have to be an actual value in the given collection.) If the given collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty or if there is no qualifying value i, this method throws a NoSuchElementException.

Examples:

T
c
key
comp
<Tceiling(c, key, comp)
Integer
[2, 8, 7, 3, 4]
1
ascending order
2
Integer
[5, 9, 1, 7, 3]
7
descending order
7
Integer
[8, 7, 6, 5, 4]
0
ascending order
4
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
(B,9)
ascending by String field
(B,4)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
(F,0)
ascending by Integer field
(E,1)
The floor method.

This method returns the largest value i in a given collection such that i ≤ key, as defined by the given comparator. (Note that key does not have to be an actual value in the given collection.) If the given collection or comparator is null, this method throws an IllegalArgumentException. If the collection is empty or if there is no qualifying value i, this method throws a NoSuchElementException.

Examples:

T
c
key
comp
<Tfloor(c, key, comp)
Integer
[2, 8, 7, 3, 4]
6
ascending order
4
Integer
[5, 9, 1, 7, 3]
1
descending order
1
Integer
[8, 7, 6, 5, 4]
9
ascending order
8
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
(F,0)
ascending by String field
(E,1)
(String, Integer)
[(A,5), (B,4), (C,3), (D,2), (E, 1)]
(B,9)
ascending by Integer field
(A,5)

More products