Assignment 9, due Oct 30

Solutions

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

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of class on the day indicated (usually Friday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: Consider adding output gates to a logic simulator, as discussed in the assignment for MP4, with an attempt to display the output of the simulator as a graph going down the page, where each line of output represents one time unit. The display of a simulation with just one output x might look like this:
        |_
         _|
        |
        |-
        |_
         =|
    

    There are numerous forms of output (they are all listed in the assignment for the machine problem). You could select the form of output using a long cascade of if statements such as: If the output used to be false and it is now true and it changed just once, use the string " |_ ".

    A Question: Give a simple algorithm that picks the correct string out of an array of strings using just the current value of this output and the number of changes to this output since the last time it was printed. Note that the previous value of the output need not be tested if the number of output changes is known. (1 point).

    Suppose the new (current) value of the output is false:

    ChangesCharacter
    0 " |   "
    1 "  _| "
    2 " |-  "
    3 " ,=' "
    4 " |=  "
    odd>3 " ,=' "
    even>4" |=  "

    For even values, we just use the mirror image of the string. So we will use an array of 5 strings, trueArray (or two arrays of 5 strings, trueArray and falseArray where trueArray[i] is the reverse of falseArray[i]). The code to output the right string could look like this:

    if (changes > 4) changes = 4 - (changes & 1);
    if (current) {
       System.out.append( trueArray[ changes ] );
    } else {
       System.out.append( falseArray[ changes ] );
    }
    

  2. Background: In the lecture notes for October 27, three versions of the code to link Road.enter() to Intersection.enter() are given:

    1. Road.enter() schedules both Road.exit() and Intersection.enter() to happen at the same time.

    2. Road.enter() schedules just Road.exit(), and then when Road.exit() is triggered, it schedules Intersection.enter() to happen immediately.

    3. Road.enter() schedules just Road.exit(), and then when Road.exit() is triggered, it directly calls Intersection.enter().

    Suppose we add this code to the very end of Road.exit():

    System.out.println( "Exiting " + this.toString()
            + " at time = " + t
    );
    

    And we add this code to Intersection.enter():

    System.out.println( "Entering " + this.toString()
            + " at time = " + t
    );
    

    A Question: One of the 3 approaches used above guarantees that the exit message will be printed before the enter message. One of the three approaches guarantees that the enter message will be printed before the exit message, and one offers no guarantees because it depends on decisions made elsewhere in the simulator that would be correct in a formal sense for either order. Which of the three leads to which order guarantee? (1.0 points)

    Having Road.exit() schedule Intersection.enter() guarantees that the exit message will print before the enter message.

    If Road.enter() schedules both Road.exit() and Intersection.enter(), the order depends on how the event scheduler breaks ties. This is outside our control.

  3. A small problem: Find all of the top-level classes and interfaces in the road-network code posted on October 27 (ignore inner classes) and make a class dependenchy diagram, that is, a directed graph with classes at the vertices and arrows from each class to the other classes it depends on. One class depends on another if the dependent class makes any reference to the other class (declaring an instance, using a definition from, referencing a method of, etc.)

    Note: Legibility matters! We strongly suggest that you draw it once just to get the details right, and then redraw your diagram to look nice, with some kind of ordelry arrangement where classes that depend on none of the other classes in this project on top, classes that are codependent in the middle, and classes that depend on them at the bottom. (1.0 points)

    First, here are all the classes:

    Simulator   ByName      Errors
    SyntaxCheck Road        Intersection
    NoStops     FourWay     Source
    Sink        PRNG        Vehicle
    Event       RoadNetwork
    

    Second, here are the "depends on" relations:

    Simulator    ->
    
    ByName       ->
    
    Errors       -> 
    
    SyntaxCheck  -> ByName       Errors
    
    Road         -> Intersection Errors
                    SyntaxCheck  (byName)
                    Vehicle      Simulator
    
    Intersection -> Road         RoadNetwork
                    SyntaxCheck  (byName)
                    Vehicle
    
    NoStops      -> Intersection Vehicle
                    Errors       Simulator
    
    FourWay      -> Intersection Vehicle
                    Errors
    
    Source       -> Intersection Simulator
                    Vehicle      Road
                    Errors
    
    Sink         -> Intersection Errors
    
    PRNG         ->
    
    Vehicle      -> Road         PRNG
    
    Event        ->
    
    RoadNetwork  -> Intersection Road
                    NoStops      FourWay
                    Source       Sink
                    Errors       Simulator
    

    With this worked out, we can now try to draw a nice figure.

    This figure is a tangled enough snarl to raise doubts about the utility of this kind of graphic notation. It is clear that the dependency relationships between the different classes in the program should be documented, but despite the saying, a picture is not always worth a thousand words. The lists of dependencies given in textual form above seem more useful than the diagram.