Assignment 11, due Nov. 13

Solutions

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

  1. Background: A popular wrong answer on exam 2 was to suggest that the Output.outputChange() method was the right place to put "do it once" logic (such as, if(!done){outputHeaders();done=true}) to print the output heading and start the logical process to print all the outputs once per time unit.

    A Problem: Give an example circuit that clearly demonstrates the inadequacy of this solution, either because it would produce no output or because it would produce misleading output. (The circuit need not have complex or interesting behavior -- the only thing it must do is demonstrate the incorrectness of the proposed solution.) (1 point)

    gate and a 1.0
    gate output b 0.0
    wire a a in1 1.0
    wire a a in2 1.0
    wire a b in 1.0
    

    The notable thing about this circuit is that nothing ever changes, so there will be no output change events anywhere in the circuit. The correct output, however, would be:

     b
     |
    

  2. Background: The code distributed to set people up to do MP5 uses very simple logic to stop the reading of the circuit description so that the input gates can read the input file using the same scanner.

    A Problem: Find and explain this logic. (1 point)

            private static void readCircuit( Scanner sc ) {
                    /** Read a logic circuit, scanning its description from sc.
                     */
    
                    while (sc.hasNext() && !sc.hasNextFloat()) {
    

    The test, at the start of each line, for hasNextFloat() terminates the reading of the circuit description, leaving the scanner positioned to read the list of input change events.

  3. Background: With the output gates, it was natural to make the displayOutput() method static because its job was to traverse the list of all output gates and print all their values at one instant of simulated time. Note that the choice between a static method and gate-specific outputChange() events was the result of a constraint on the output of the program, not a choice that followed from some internal aesthetic.

    A Problem: With input gates, is there a need for a similar static readInput() method, or should this job be done by some kind of inputChange() event that is specific to one particular input gate at a time? Explain why. (1 point)

    Each line of input looks like this:

    1.0 a true
    
    If each input event ends by reading the time of the next event, we do not yet know which input is involved, but only the time of some input (or the end of the simulation). Because of this, we know that we can schedule a readInput() but we cannot apply it to a specific gate until we read onward. Admittedly, we could do so, but it seems simpler to stop at this point. Therefore, the most obvious code seems to involve a static readInput() method.