Difference between revisions of "Comparator Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
Line 19: Line 19:
  
 
===closestTo===
 
===closestTo===
{{Warning | Attempt to do this by utilizing CompareUtils.min(). The purpose of this is to get practice using comparables. }}
+
{{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>

Revision as of 19:53, 7 February 2023

Group Assignment

This is a group assignment.

Background

Comparator<T>

Optional<T>

HockeyPlayer

Code To Implement

CompareUtils

class: CompareUtils.java Java.png
methods: min
package: compare.group
source folder: student/src/main/java

min

public static <E> Optional<E> min(E[] array, Comparator<E> comparator)

CompareApps

class: CompareApps.java Java.png
methods: closestTo
youngest
mostPoints
package: compare.group
source folder: student/src/main/java

closestTo

Attention niels epting.svg 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)

Testing Your Solution

class: _MinCompareTestSuite.java Junit.png
package: compare.group
source folder: testing/src/test/java