Assignment 12, due Apr 26

Solutions

by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Enough people had difficult on the exam that it's worth going back and working on some λ-expressions. Here's a familiar looking Java main program:
    public static void main( String[] args ) {
        ClassA p = (int i) -> 2 * i;                             // a
        ClassB q = (ClassA j) -> ( (int k) -> j.x( j.x( k ) ) ); // b
        ClassA r = q.y( p );                                     // c
        int    s = r.x( 1 );                                     // d
    }
    

    You saw this in Exam II, and you are welcome to use the code from the posted solution to the exam to test your solution to this probmem.

    A problem: Rewrite the line that defines and initializes q so that it uses explicitly declared and named outer classes and no λ-expressions.

    This is just like HW11, except that you now have to get rid of all nested classes, which mans things get more interesting. Since working code has been posted for both the homework and exams, you are free to test your solution. Work incrementally! (1.0 points)

    interface ClassA {
        int x(int i);
    }
    
    interface ClassB {
        ClassA y(ClassA f);
    } 
    
    class MyA implements ClassA {                                // b
        ClassA jj;                                               // b
        MyA( ClassA j ) {                                        // b
            jj = j;                                              // b
        }                                                        // b
    
        public int x(int k) {                                    // b
             return jj.x( jj.x( k ) );                           // b
        }                                                        // b
    }                                                            // b
    
    class MyB implements ClassB {                                // b
        public ClassA y(ClassA j) {                              // b
            return new MyA( j );                                 // b
        }                                                        // b
    }                                                            // b
    
    public class T {
        public static void main( String[] args ) {
            ClassA p = (int i) -> 2 * i;                         // a
            ClassB q = new MyB();                                // b
            ClassA r = q.y( p );                                 // c
            int    s = r.x( 1 );                                 // d
            System.out.println( "Value is: " + s );
        }
    }
    

    In the above, all of the lines marked b are part of the solution. The rest is included so that this answer is embedded in executable code that you can actually compile and run to see that it works.

  2. Background: In last Friday's class, we drew class dependency diagrams for the logic simulator. Class A depends on class or interface B if A is a subclass or implementation of B (we used fat arrows for this), or if A makes reference to variables or code in B, for example with B.xxx or new B() (we used thin arrows for this).

    We divided the classes into layers, putting classes that depended on none of the other classes in our project at the top, and arranging things so that all dependency arrows between layers pointed upward. Within a layer, it was common to have knots, where some of the classes in a layer may depended on each other with circular dependency loops.

    A problem: Draw the class dependency diagram for the road network simulator. Be warned that this will not be the same as the diagram for the logic simulator because of an important difference in their internal structure.

    Please make a rough draft to throw away first, because neatness really does count. Also, please do not include extra arrows: If class A depends on class B and class B depends on class C, there is no need to put arrows from A to C even if there is a direct reference in A to C. These extra arrows just make the whole diagram harder to read. (1.0 points)

    The grey bars divide levels in the class dependency hierarchy. They are not a required part of the answer.

  3. Background: Here is a little class hierarchy:
      A
     /|\
    B C=D
     \|/
      E
    

    I can't type arrow heads, so here's a translation into English: B depends on A, C depends on A and D, D depends on A and C, and finally, E depends on B, C and D.

    Please assume that each class is in a separate .java file.

    a) If you type javac A.java which classes get compiled? (0.2 points)

    Just A

    b) If you type javac B.java which classes get compiled? (0.2 points)

    Just A and B

    c) If you type javac C.java which classes get compiled? (0.2 points)

    A, C and D

    d) If you type javac D.java which classes get compiled? (0.2 points)

    A, C and D

    d) If you type javac E.java which classes get compiled? (0.2 points)

    A, B, C, D and E