Assignment 7, due Mar 21 (Monday)

Solutions

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

  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)

                    Integer i = Integer( 1 );
                    Integer j = Integer( 10 );
                    while (i != j) {
                            System.out.println( "" + i );
                            i = Integer( i.intValue() + 1 );
                    }
    

    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)

    private static Integer values[Max]; // all fields are initially null
    

    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.)

    Integer newInteger( int i ) {
            Integer newI = null;
            if (i < Max) newI = values[i];
            if (newI == null) {
                    newI = new Integer();
                    newI.value = i;
                    if (i < Max) values[i] = newI;
            }
            return newI;
    }
    
    (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)

    Neuron.this
    Synapse.Synapse.name
    Synapse.Synapse.sourceName
    Synapse.this
    

    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)

    Both of the following can be explicitly declared final:

    Synapse.Synapse.name
    Synapse.Synapse.sourceName
    

    These are implicitly final and cannot be explicitly declared:

    Neuron.this
    Synapse.this
    

  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)

    In this case, the lambda expression passes a function that will be retained by the PriorityQueue object and used over and over again, each time the queue needs to compare two elements.