Assignment 9, due Oct 27Solutions
Part of
the homework for CS:2820, Fall 2017
|
a) Which law does the code simulate. (0.4 points)
The second. As soon as the light changes to red, waiting vehicles must stop. Only the vehicle actually in the intersection is allowed to continue.
b) One line of the given code is the key to the above conclusion. In what method is that code and what line is it? (0.6 points)
The method is StopLight.departureEvent.
// if there are more vehicles, schedule the next departure // in effect, departure events are a process that drains the queue if (queues[lightDir] > 0) {
I quoted the line of code with its comments here. The key is, lightDir may change between departure events, so with each departure event, we look at the current light direction, without regard to its history.
When an input change event occurs, the new value of the input that changed is recorded and then, if the result is a change in the output, an output change event is scheduled one gate delay later.
When an output change event occurs, the list of wires connected to the corresponding output is traversed to schedule input changes at the far ends of that wire one wire delay later.
In each part of the question below, you could define a distinct version of the event service routine for each concrete class at the leaf of the class hierarchy, or you could move code up the hierarchy.
a) Where in the hierarchy would be the best place to define versions of outputChangeEvent? Equivalently, which leaves of the class hierarchy can share the same versions of outputChangeEvent? (0.5 points)
All logic gates have just one output, so we can define one version of outputChangeEvent in class LogicGate.
Class ConstGate requires its own version of outputChangeEvent; this applies only to the true output and is scheduled exactly once per constant gate, at start-up.
b) Suppose inputChangeEvent directly computes the new output. Where in the hierarchy would be the best place to define versions of inputChangeEvent? Equivalently, which leaves of the class hierarchy can share the same versions of inputChangeEvent? (0.5 points)
We'd have to put one version of inputChangeEvent in each of the classes AndGate, OrGate, NotGate, and ConstGate (this last one is trivial, all it would do is output an error message).
c) How would your answer to part b change if you added a method NewOutput to each gate, so that inputChangeEvent calls NewOutput to see if a change to the output should be scheduled. (0.5 points)
In this case, we can have one version of InputChangeEvent in class TwoInputGate, another in NotGate, and of course, a trivial one in ConstGate.
d) For each method discussed in parts a–c above, must that method be public, could it be private? could it be protected? (0.5 points)
All variants on InputChangeEvent must be public (or default -- we have not really distinguished public from default access rights), since they are called from class Wire.
ConstGate.OutputChangeEvent can be private, since it can be scheduled by the sanity check or constructor code for that class.
NotGate.OutputChangeEvent can be private, since it is scheduled by NotGate.InputChangeEvent.
LogicGate.OutputChangeEvent must be protected, since it is scheduled by input change events in subclasses.
TwoInputGate.OutputChangeEvent must be protected, since it is scheduled from the versions of NewOutput in each subclass.