Assignment 12, due Apr 28

Solutions

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

On every assignment, write your name and section number. Write your name as it appears on your university ID card! Use the section number as it appears in your current registration! We will not do detective work to figure out who did what. Homework is due on paper at the start of your discussion section. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God"). Never push homework under someone's door!

  1. Background: The code distributed for MP6 is not the end of the story. Consider the code given in class TernaryLogic. This class contains several subclasses of Simulation.Event.

    It might make sense to crack initPrint and PrintEvent into a separate source file. It is easy enough to rip them out of TernaryLogic, but they need access to a variable or variables that are private to TernaryLogic.

    Suppose we name the new class PrintEvent, with the static method initPrint local to PrintEvent. This architectural change has consequences.

    a) What variable or variables pose difficulties? (0.3 point)

    The code in class PrintEvent needs to access the list of gates, gates When PrintEvent was an inner class, it could access this variable directly, but now that we propose PrintEvent a global class, this class is no longer accessible without qualification. We must use TernaryLogic.gates and we might need to make it public or perhaps pass it as a parameter.

    b) In light of part a, what are the parameters to initPrint? (0.3 point)

    If we do make TernaryLogic.gates public, no change is needed.

    If we want to avoid making TernaryLogic.gates public, we should pass gates as a parameter of type LinkedList;<Wire> to initPrint.

    c) In light of parts a and b, what private static variables should be in class PrintEvent? (These may replace or supplement existing variables.) (0.4 point)

    As usual, there are multiple possible design decisions.

    We could have static variables in class PrintEvent holding both printInterval and gates. This leads to the simplest code.

    Alternatively, we could make one or both of these variables instance variables of each event, passing them as parameters to the initializer each time a new event is created. This makes the code unnecessarily complex, both to the human reader and from the point of view of requiring more computation.

  2. Background: Look at class Wire in the code distributed for MP6.

    a) One of the simulation classes can be private. Which one, and why must the other one remain public? (0.5 point)

    Class Wire.OutputChangeEvent can be private because the only reference to this class is from within the code for Wire.InputChangeEvent.

    In contrast Wire.InputChangeEvent cannot be private because it is used from within other classes Gate.

  3. Background: Look at the code for class Gate in the solution distributed for MP5.

    a) Which methods in class Gate must be replaced by inner classes (with their own methods) in a solution to MP6? (0.5 point)

    Methods inputChangeEvent and outputChangeEvent.

    b) One of these new classes can be a private or protected inner class. Which one and if it must be protected, explain why with reference to the code distributed for MP6. (0.5 point)

    Method outputChangeEvent (and the class that replaces it, called OutputChangeEvent) can be protected, but not private, because it is used from within the subclasses of Gate as well as being used directly from within InputChangeEvent.

    c) One of these new classes cannot be private or protected because of the way it is referenced from elsewhere. Where is the reference to it from elsewhere in the code distributed for MP6. (0.5 point)

    Method inputChangeEvent (and the class that replaces it, called InputChangeEvent) cannot be private or protected because it is used from within class Wire.