Assignment 7, due Mar 21 (Monday)

Part of the homework for CS:2820, Spring 2016
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of lecture on the day indicated (MONDAY). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: Recall a program like this from exam 1:
    class Toy {
            public static void main( String[] args ){
                    Integer i = 1;
                    Integer j = 10;
                    while (i != j) {
                            System.out.println( "" + i );
                            i = i + 1;
                    }
            }
    }
    

    This program runs as a naive programmer would expect, but it might surprise someone who knows that the != operator in Java compares object identities, not their values. If you change the initial values to 1001 and 1010, the code behaves the way a naive programmer would expect while it would surprise a naive programmer.

    Obviously the implementors of class Integer did some extra work to make it behave as a naive programmer would expect so long as the integer values were within a small range. Don't worry about finding the bounds of the range.

    a) (0.5 points) Identify every point in the above code where autoboxing or unboxing take place in the above code by rewriting the above code to use explicit calls to Integer() constructors where autoboxing is used, and to use explicit calls to the intValue() method where unboxing is used. (0.5 points)

    b) The heavy lifting required to help class Integer satisfy naïve programmers must be done by addint some static data structures to the class and adding special code to the Integer() constructor. Suggest an appropriate static data structure to add to the class. (Hint: One static declaration is all it takes.) (0.5 points)

    c) It is not clear that you can write legal Java code to do what the Integer() constructor does, but if you replace new Integer() with calls to a newInteger() factory method, you can write legal Java code to do something like what the Integer class is doing to satisfy beginners. (Hint: If you stretch the code out and format it carefully, it's about 10 lines of code.) (0.3 points)

  2. Background: Look at the lambda expressions in the alternative solution distributed for MP3. The official documentation for Java states that any variables referenced from within a lambda expression must be either final or effectively final.

    a) What variables are referenced from within these lambda expressions. Please fully qualify their names. That is, don't just say v, say class.v or class.method.v. (0.5 points)

    b) Of the variables you identified in part a), which of them can be explicitly declared as final? (The experiment is easy, just take the code, mark them all as final, and see which declarations the compiler does not complain about.) (0.5 points)

  3. Background: The lambda expressions in the alternative solution distributed for MP3 were all passed in order to gain the benefits of deferred evalution of expressions passed as parameters.

    Background: Lambda expressions are passed for a different reason when creating a new instance of class PriorityQueie (for example, in the notes for March 11). Explain this reason. (0.5 points)