lab23
subdirectory of your personal repo, the other submits a file report.txt
in the lab23
subdirectory of your personal repo.We start with this Pair
class.
import java.util.AbstractList; public class Pair<T> { public Pair() { first = null; second = null; } public Pair(T first, T second) { this.first = first; this.second = second; } public T get(int n) { return n == 0 ? first : n == 1 ? second : null; } public void set(int n, T t) { if (n == 0) first = t; else if (n == 1) second = t; } private T first; private T second; }
In order to avoid the tedious Employee
/Manager
examples, we will instead use the infinitely more exciting
import java.math.*; public class LabeledDecimal extends BigDecimal { public LabeledDecimal(String label, String digits) { super(digits); this.label = label; } public String getLabel() { return label; } public String toString() { return label + "=" + super.toString(); } private String label; }
For example,
new LabeledDecimal("pi", "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825");
copyFrom
in the Pair
class that sets this pair to another pair. Use wildcards so that the following call succeeds:
Pair<BigDecimal> p1 = new Pair<BigDecimal>(); LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.414"); Pair<LabeledDecimal> p2 = new Pair<LabeledDecimal>(ld1, ld2); p1.copyFrom(p2); System.out.println(p1);
copyTo
in the Pair
class that sets another pair to this pair. Use wildcards so that the following call succeeds:
LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.414"); Pair<LabeledDecimal> p1 = new Pair<LabeledDecimal>(ld1, ld2); Pair<BigDecimal> p2 = new Pair<BigDecimal>(); p1.copyTo(p2); System.out.println(p2);
min
method for the Pair
class
public T min(Comparator<T> comp) { if (comp(first, second) < 0) return first; else return second; }Try it out with this comparator:
public class ContrivedComparator implements Comparator<BigDecimal> { public int compare(BigDecimal a, BigDecimal b) { return a.precision() - b.precision(); } }Make a
Pair<BigDecimal> p1 = new Pair<BigDecimal>(new BigDecimal("3.14"), new BigDecimal("1.414"));
Can you compute p1.min(new ContrivedComparator())
? What is it? Why?
LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.414"); Comparator comp = new ContrivedComparator(); System.out.println(comp(ld1, ld2)); // not an error! Pair<LabeledDecimal> p2 = new Pair<LabeledDecimal>(ld1, ld2); System.out.println(p2.min(comp));What error do you get? Why? How can you use wildcards to fix the
min
method (without changing ContrivedComparator
, of course).min
that yields the smaller of the two pair values:
public T min() { if (first.compareTo(second) < 0) return first; else return second; }Why doesn't this code compile?
extends Comparable<T>
to the Pair
type declaration and check that it compiles. Now try this code:
LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.41"); Pair<BigDecimal> p1 = new Pair<>(ld1, ld2); System.out.println(p1.min());Did it compile? What did it print?
Pair<Object>
instead.
LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.41"); Pair<Object> p1 = new Pair<Object>(ld1, ld2); System.out.println(p1.min());What happens? Why?
Pair<LabeledDecimal>
:
LabeledDecimal ld1 = new LabeledDecimal("pi", "3.14"); LabeledDecimal ld2 = new LabeledDecimal("sqrt(2)", "1.41"); Pair<LabeledDecimal> p1 = new Pair<LabeledDecimal>(ld1, ld2); System.out.println(p1.min());What happens? Why?
Pair
class so that min
works with a Pair<LabeledDecimal>
. What is your fix?