Assignment 11, due Apr 19

Solutions

Part of the homework for CS:2820, Spring 2019
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 ) {
        X      p = (String s) -> "p( " + s + " )";
        Y      q = (X j) -> ( (String s) -> j.x( j.x( s ) ) );
        X      r = q.y( p );
        System.out.println( "Value is: " + r.x( "one" ) );
    }
    

    You saw this in Homework 10, and you are welcome to use the code from the posted solution to homework 10 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 inner classes and no λ-expressions.

    Suggestion: Eliminate the outer λ-expression first, test your result, and then work on eliminating the inner one. (1.0 points)

        public static void main( String[] args ) {
            X p = (String s) -> "p( " + s + " )";
            class MyY implements Y {
                public X y(X j) {
                    class MyX implements X {
                        public String x(String s) {
                             return j.x( j.x( s ) );
                        }
                    }
                    return new MyX();
                }
            }
            Y q = new MyY();
            X r = q.y( p );
            System.out.println( "Value is: " + r.x( "one" ) );
        }
    

    Note that the above uses nesting to good effect. Had we tried to move class MyX outside of class MyY we'd have had to do essentially all of the work needed to make MyX into an outer class. That is an exercise worth doing, but more than what was assigned.

  2. Background: Consider the following code from class wire in the posted solution to MP4 (it is unchanged in MP5):
    /** tell the wire that its input has changed
     *  @param now the time at which the change occurs
     *  @param value the new value of that input
     */
    public void inputChange( float now, int value ) {
        Simulation.schedule(
            now + delay, (float t) -> this.outputChange( t, value )
        );
    }
    
    private void outputChange( float now, int value ) {
        final int comp = 1 - value; // logical complement of the value
        System.out.println( "time "+ now +" "+ comp +"->"+ value +" "+ this );
        destination.inputChange( now, value );
    }
    

    A problem: Rewrite this to work in the new framework distributed on April 15. Suggestion: You can entirely eliminate the outputChange method. Warning: Beware of problems with this. (1.0 points)

    public void inputChange( float now, int value ) {
        Simulation.schedule(
            final Wire here = this;
            new Event( now + delay ) {
                final int comp = 1 - value; // logical complement of the value
                System.out.println(
                    "time "+ time +" "+ comp +"->"+ value +" "+ here );
                }
                destination.inputChange( time, value );
            }
        );
    }
    

  3. Background: The previous problem suggested eliminating Wire.outputChange. Now, consider the Gate.outputChange method.

    a) How many places in the code of MP4 (the answer is the same for MP5) is this called or scheduled from? (0.5 points)

    InputCountGate.inputChange schedules it.
    InputGate.SanityCheck calls it.
    InputGate.nextChange calls it.

    So, a short answer is 3.

    b) What consequence does this have with regard to eliminating Gate.outputChange. Specifically, would you recommend eliminating it? Why or why not? (0.5 points)

    Do not eliminate it in order to avoid duplicating the three lines of code in its body.