Assignment 10, due Apr 12

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 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" ) );
    }
    

    Given an appropriate class wrapping this code, along with appropriate definitions for X and Y, this code can be compiled.

    a) Give the Java definitions for X and Y. (0.5 points)

    interface X {
        String x( String anyParametrName );
    }
    interface Y {
        X y( X anyParametrName );
    }
    

    The presence of the indicated parameters is required by the problem, but nothing in the problem constrains the parameter names. All the other details given in the above answer are completely determined by the problem.

    b) What output would you expect from this main program? (0.5 points)

    The output will be:

    Value is: p( p( one ) )
    

    You can easily check this; just run this program:

    class MP10problem1 {
        interface X {
            String x( String anyParametrName );
        }
        interface Y {
            X y( X anyParametrName );
        }
        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" ) );
        }
    }
    

  2. Background: The posted solution to MP4 uses class IncountGate as a crutch to implement classes XorGate and ThresholdGate. It maintains an instance variable oldOutput for just one purpose, to determine if an input change requires any change to the output.

    In both parts of this problem, you are to consider two successive calls to outputChange for some particular gate, comparing the parameter value to the value of oldOutput. In the code given, the value of oldOutput is not visible in method outputChange. That does not mean that it does not exist, so on paper, we can still compare the values.

    a) Suppose that the simulated time interval between the two successive calls is longer than the delay of the gate. What is the relationship between value and oldOutput at the first call? At the second call? (0.5 points)

    value will always equal oldOutput at both calls.

    b) Suppose that the simulated time interval between the two successive calls is shorter than the delay of the gate. What is the relationship between value and oldOutput at the first call? At the second call? (0.5 points)

    value will not equal oldOutput at the first call. value will equal oldOutput at the second.

    The reason for the discrepancy is that the input change that eventually causes the second call will have already happened by the time the first call is made.

    To see how this is true, it is useful to draw a graph of the value of the output of a gate (value) and the value of oldOutput for that gate. The value of oldOutput is changed by input changes, while the value of the output of the gate, value, is changed by output change events.

    In the above, the time of each input change event is shown as a thin vertical line, while the times of output change events are shown as dashed vertical lines. Arrows connect each input change to the corresponding output change. The first two input changes are shown closer together than the delay of the gate, while the second pair of input changes are shown farther apart than the delay of the gate.

    Note that each time value is changed, it is changed to a value that agrees with oldOutput except in one case.

    This problem was assigned in order to help people solve MP5. The key output change that needed to be suppressed in MP5 is the one that would have resulted in value being set to disagree with oldOutput.

  3. Background: Run Javadoc on the distributed solution to MP4.

    a) How many .html files does it create? (0.5 points)

    12 of them. (An awful lot of them!)

    b) Explore the documentation, starting with the index.html file. Is it useful? Why or why not? (0.5 points)

    Not very useful. (It lists just one class, MP4 and there are no hot links to anything of interest.)

    The problem is, Javadoc only documents the public classes, and because the source file has not been broken up, there can be only one public class. (Until we break up the source file and mark the classes we want documented as public, Javadoc doesn't do us much good at all!)