Difference between revisions of "Comparator Assignment"
Jump to navigation
Jump to search
m (→mostPoints) |
|||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | =Group Assignment= | ||
+ | This is a group assignment. | ||
=Background= | =Background= | ||
[https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html Comparator<T>] | [https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html Comparator<T>] | ||
Line 8: | Line 10: | ||
=Code To Implement= | =Code To Implement= | ||
==CompareUtils== | ==CompareUtils== | ||
+ | {{CodeToImplement|CompareUtils|min|compare.group}} | ||
+ | |||
===min=== | ===min=== | ||
<nowiki>public static <E> Optional<E> min(E[] array, Comparator<E> comparator)</nowiki> | <nowiki>public static <E> Optional<E> min(E[] array, Comparator<E> comparator)</nowiki> | ||
+ | |||
+ | '''Tip: ''' To create an Optional<E>, do the following: <code>Optional.of(E value)</code>. | ||
==CompareApps== | ==CompareApps== | ||
+ | {{CodeToImplement|CompareApps|closestTo<br/>youngest<br/>mostPoints|compare.group}} | ||
+ | |||
===closestTo=== | ===closestTo=== | ||
+ | {{Warning | THIS SHOULD BE A ONE LINE SOLUTION! You should be calling CompareUtils.min()}} | ||
+ | |||
<nowiki>public static Optional<Integer> closestTo(Integer[] values, int target)</nowiki> | <nowiki>public static Optional<Integer> closestTo(Integer[] values, int target)</nowiki> | ||
+ | |||
+ | Return a call to CompareUtils.min() which contains an index within an Optional. Use a lambda to define a comparator as the second parameter to min(). What computation should this comparator return? | ||
+ | |||
===youngest=== | ===youngest=== | ||
<nowiki>public static Optional<HockeyPlayer> youngest(HockeyPlayer[] players)</nowiki> | <nowiki>public static Optional<HockeyPlayer> youngest(HockeyPlayer[] players)</nowiki> | ||
===mostPoints=== | ===mostPoints=== | ||
<nowiki>public static Optional<HockeyPlayer> mostPoints(HockeyPlayer[] players)</nowiki> | <nowiki>public static Optional<HockeyPlayer> mostPoints(HockeyPlayer[] players)</nowiki> | ||
+ | How can you make it so a call to min() ends up returning the max instead? 🤔 | ||
+ | |||
+ | =Testing Your Solution= | ||
+ | {{TestSuite|_MinCompareTestSuite|compare.group}} |
Latest revision as of 20:36, 7 February 2023
Contents
Group Assignment
This is a group assignment.
Background
Code To Implement
CompareUtils
class: | CompareUtils.java | |
methods: | min | |
package: | compare.group | |
source folder: | student/src/main/java |
min
public static <E> Optional<E> min(E[] array, Comparator<E> comparator)
Tip: To create an Optional<E>, do the following: Optional.of(E value)
.
CompareApps
class: | CompareApps.java | |
methods: | closestTo youngest mostPoints |
|
package: | compare.group | |
source folder: | student/src/main/java |
closestTo
Warning: THIS SHOULD BE A ONE LINE SOLUTION! You should be calling CompareUtils.min() |
public static Optional<Integer> closestTo(Integer[] values, int target)
Return a call to CompareUtils.min() which contains an index within an Optional. Use a lambda to define a comparator as the second parameter to min(). What computation should this comparator return?
youngest
public static Optional<HockeyPlayer> youngest(HockeyPlayer[] players)
mostPoints
public static Optional<HockeyPlayer> mostPoints(HockeyPlayer[] players)
How can you make it so a call to min() ends up returning the max instead? 🤔
Testing Your Solution
class: | _MinCompareTestSuite.java | |
package: | compare.group | |
source folder: | testing/src/test/java |