Assignment 10, due Nov. 13Solutions
Part of
the homework for CS:2820, Fall 2015
|
In short, we arrange the class hierarchy so that class Simulator.Event is a public abstract class. Any class that needs to be scheduled must be an extension of Simulator.Event. Instead of having actions that can be triggered as a component of an event, we make the trigger() method an abstract method of Simulator.Event.
Consider modifying the logic simulator distributed as a solution to MP3 so that it uses this simulatin framework: In that simulator, there were 4 calls to Simulator.schedule().
a) Write the code for Wire.inputChange() and any associated local classes using the new framework. In this part, input change events should call Wire.outputChange(). That is, you should not optimize. (1 point)
public static class OutputChangeEvent extends Simulator.event { boolean value; OutputChangeEvent( real t, boolean v ) { time = t; value = v; } void trigger() { Wire.this.outputChange( time, value ); } } public void inputChange( float t, boolean v ) { /** Simulation event service routine called * when the input to a wire changes at time t to value v. */ OutputChangeEvent e = new OutputChangeEvent( t + delay, v ); Simulator.schedule( e ); }
b) Write the code for Wire.inputChange() etc using the new framework, optimizing as much as possible to eliminate calls to other methods of class Wire. (1 point)
public static class OutputChangeEvent extends Simulator.event { boolean value; OutputChangeEvent( real t, boolean v ) { time = t; value = v; } void trigger() { // note that driven and value are fields of this wire driven.inputChange( time, input, value ); } } public void inputChange( float t, boolean v ) { /** Simulation event service routine called * when the input to a wire changes at time t to value v. */ Simulator.schedule( new OutputChangeEvent( t + delay, v ) ); }
c) Looking at the code for class Output, how many new classes would you have to introduce in order to rewrite your code for this new framework? (1 point)
Class Output will need a new class DisplayOutputEvent and another DisplayHeadersEvent.