Class 15 — Monday, October 19
The A6 presentation comes first. Quiz 6 follows it, and closes the class too. Sections 1 and 2 run on the projector, keyboards down, predicted before they run. Section 3 is in pairs, in a seat of your own, opened from inside LockDown Browser, with the task list in the seat.
- A list of integers as a list of numbers — predict first
- Two bounds at once — predict first
- A Matrix for any number, in your seat: getting in, then the four tasks
Your seat opens from a link inside a second quiz, Class 15 seats, which runs only in LockDown Browser. Nobody starts it until section 3. Your work goes to your Classroom 50 repository for this class: accept it from the link in Discord if you have not.
1. A list of integers as a list of numbers
Keyboards down. Predict before it runs.
In week 2 an Integer went wherever a Number was asked for, because an Integer is a Number. On Wednesday three of you wrote that a chain of integers cannot go where a chain of numbers is asked for. Here is the same question with the library’s list:
List<Integer> ints = new ArrayList<>();
ints.add(1);
List<Number> nums = ints;
nums.add(2.5);
System.out.println(ints);Which line does the compiler refuse? And if it allowed that line, what would the line after it do?
The third line: incompatible types: List<Integer> cannot be converted to List<Number>. A List<Integer> is not a List<Number>, even though an Integer is a Number.
The reason is the next line. A List<Number> takes any number, so nums.add(2.5) is fine for it. But nums would be the same list as ints, and a Double would now sit in a List<Integer>, where the next int x = ints.get(1) would fail at run time. The compiler refuses the assignment, so the line after it can never run. Reading an Integer out as a Number was never the problem; putting a Double in was.
2. Two bounds at once
Keyboards down. Predict before it runs.
On Wednesday max needed one bound, Comparable<T>, to call compareTo. A method that compares two numbers and hands back the larger one’s value needs compareTo and doubleValue. No one bound gives it both, so the header names two, joined with &. Fraction below is yours as A2 left it: it extends Number, and nothing more yet.
public static <T extends Number & Comparable<T>> double largerValue(T a, T b) {
T larger = a.compareTo(b) >= 0 ? a : b;
return larger.doubleValue();
}
public static void main(String[] args) {
System.out.println(largerValue(4, 9));
System.out.println(largerValue(2.5, 1.5));
System.out.println(largerValue("pear", "fig"));
System.out.println(largerValue(new Fraction(1, 2), new Fraction(1, 3)));
}Which lines of
maincompile, and what do they print? For each line that is refused, which of the two bounds refuses it? Then: take one bound out of the header. What is refused now, and where?
The first two compile and print 9.0 and 2.5. The third is refused: a String can be compared, but it is not a Number. The fourth is refused the other way round: A2’s Fraction is a Number, and nothing in it says it can be compared. Both say inference variable T has incompatible bounds. The Fraction you finished in A4 implements Comparable<Fraction>, and with it the line compiles and prints 0.5: A2 gave it one of the two bounds, A4 the other. A bound is a promise a class makes, and in your seat the same Fraction makes two more.
Take a bound out and the refusal moves inside the method. Without Number &, larger.doubleValue() is refused: cannot find symbol. Without & Comparable<T>, a.compareTo(b) is. A bound works both ways: it lets the method call what the bound promises, and it keeps out every type that does not keep the promise. & means the type must keep both.
A class among the bounds comes first: T extends Comparable<T> & Number is refused with interface expected here. Your seat’s Matrix is declared the same way, with two interfaces: T extends Addition<T> & Multiplication<T>.
3. A Matrix for any number, in your seat
3.1 Getting in
Pairs. In LockDown Browser.
Go to the quiz Class 15 seats and start it (it will require LockDown browser). It has no questions, only two links at its top. Open my seat lands you in an editor with the files already there, saved as you type. Submit my files sends what is in your seat to your repository on GitHub; press it as often as you like, and at the end it is done for you anyway. Leave the quiz open while you work. If LockDown closes, start the quiz again: the seat remembers your files, and the desk remembers your computer.
The README.md in the seat is the task list, and test/MatrixTest.java is the tests, run from the arrow beside class MatrixTest. The first time the folder opens, the editor fetches the project’s libraries, Commons Numbers and JUnit; the status bar says Java: Ready when it has. The tests do not run until task 1 is done, and the editor says where. Sit with your pair at one seat; the other seat can hold the README.
3.2 The four tasks
The Matrix from A3, holding T instead of double. Its header says what a T must be able to do: T extends Addition<T> & Multiplication<T>, two interfaces from a real library, Apache Commons Numbers, and the & means both. Between them they ask for six methods. Two are add and multiply; another is zero(), which multiply needs: A3’s started each sum from 0.0, and there is no 0 for a T. The Fraction from A2 and A4 is there, finished. It adds and multiplies, but it does not say it is an Addition<Fraction> or a Multiplication<Fraction>, so Matrix<Fraction> is refused until task 1 says so. The README in the seat has each task in full, and each of tasks 1 to 3 ends with a test of your own: the value worked out by hand, then assertEquals, at the bottom of MatrixTest.
- Make
FractionanAddition<Fraction>and aMultiplication<Fraction>: it hasaddandmultiplyalready; writezero(),one(),negate()andreciprocal(), one line each. The tests run, and1/10plus2/10is3/10, exactly, where0.1 + 0.2is not0.3. Your test: a fraction times its reciprocal is one. - Write
add: the same shape orDimensionMismatchException, then each cell’saddof its twin. Your test: adding zeros changes nothing. - Write
multiply: a row of this against a column of that, each sum started from a cell’szero(). Your test: a 2-by-1 times a 1-by-2. - Ask the editor about
Matrix<Double>: a number, and still refused.
Then the spares: Real, a class of one double that is an Addition<Real> and a Multiplication<Real>; and magnitude() through T extends Number & Addition<T> & Multiplication<T>.
Done when every test in MatrixTest is green, yours too. Nothing from the seat is graded; what is measured today is the seat itself. At the end, press submit my files, then submit the quiz and close LockDown: the Journal and Quiz 6’s second attempt are in your ordinary browser. Your files stay in your repository afterwards.