Assignment 8, due Oct 23Solutions
Part of
the homework for CS:2820, Fall 2015
|
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.
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.
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; }
// 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 );