Assignment 8, due Oct 23

Solutions

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

  1. Background: In a logic simulator, when the simulation is started, all of the inputs to all of the gates are false, and all of the outputs are false. If false is encoded electronically with zero volts and true with some non-zero value, this corresponds to the state of the system when the power is turned on, before any of the gates have had time to react to their inputs.

    Recall that logic gates have an inherent delay, the time it takes for a change in the input to percolate through the gate to change the output.

    a) Supposing that there are just 3 types of gates in your simulation, and, or and not, which of these would need to change their output shortly after the simulation is started, and when would the output change. (0.5 points)

    All not gates will change their outputs after power-up. Specifically, if a not gate has delay d and if time starts at zero, that gate will change its output from false to true at time d.

    b) Suggest where in the simulation program the initial events could be scheduled. (There are several options, you need only identify one that will work). (0.5 points)

    The initial events need to be scheduled with the knowledge that the gate is a not gate, but we can do this several ways. The following discussion refers to the code distributed as a solution to MP2.

    We can do it in NotGate.scan() as part of the creation and initialization of a new gate.

    We could define NotGate.checkInputs(), having it call super.checkInputs() before scheduling the initial event.

    Just because it is sensible to schedule the initial events inside class NotGate doesn't mean we have to do it there. For example, we could include code like this in main() right before running the simulation:

    for (Gate g: gates) {
            if (g instanceof NotGate) Simulator.schedule( g.delay, ... );
    }
    

    For the above code fragment, we might have to make certain fields of class Gate public.

  2. A short scavanger hunt question: What is a checked exception? (0.5 points)

    In Java, the compiler checks, whenever you throw an exception, whether you have declared, in the method declaration, that this method might throw that exception. If the method implements an interface or overrides a declaration in a superclass, the interface declaration or superclass determines what exceptions you can throw. This fact is discussed on pages 457-458, but the term checked exception is not used there. On page 459, class RuntimeException is discussed, introducing the term unchedked exceptions, and by impliction, defining checked exceptions as those that are not unchecked. The term checked exception is discussed explicitly on page 494 and is the explicit subject in the section beginning on page 497.

  3. A small problem: In the notes for Oct. 15, there is a discussion of how the class ListQueue can be implemented in Java. The only methods given are enqueue() and dequeue. Give code for the method indexOf(o), where this method is defined identically to the method by that name in Java's class LinkedList. (0.5 points)

    There are many ways to write this code, but this is reasonably compact and reasonably clear:

    public int indexOf( Element e ) {
            Node p = head;
            int i = 0;
            while (p != NULL) {
                    if (p.e == e) return i;
                    i = i + 1;
                    p = p.next;
            }
            retur -1;
    }
    

  4. Background: In order to be able to schedule events, you need some kind of priority queue of events. Each event in that queue will, when its turn comes, call a method of one of the simulation routines. There are lots of ways of doing this. In the vehicle network simulation code distributed on Oct. 15, this bug notice appeared in Road.enter():
    // Bug: must schedule this.exit( v, t + travelTime )
    

    Consider rewriting this as follows:

            Simulator.schedule(
                    t + travelTime,
                    (float time) -> this.exit( v, time )
            );
    

    Inside class Simulator, the following bits of code occur

            public void schedule( float time, Action a ) { ... }
            ...
                    a.trigger( time )
    

    A probem: The second argument to this method call uses Java's version of lambda notation. Given what you can infer about class Action above, rewrite the call to Simulator.schedule() using explicit named classes and objects instead of anonymous classes and objects. (1.0 points)

    Class Action could be defined inside Simulator or it could be global. For notational simplicity, I'll assume it's global here:

            class MyAction implements Action {
                    void trigger( float time ) {
                            this.exit( v, time );
                    }
            }
            MyAction act = new MyAction();
            Simulator.schedule( t + travelTime, act );