Assignment 5, due Sept 25

Solutions

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

  1. Background: Given that w is an instance of the Wire class, there are several alternatives for how you could output a line representing a wire to solve MP1. Consider the following:

    a) Add a method to class Wire called printMe(), so you would print the wire by calling w.printMe();. Give code for the printMe() method, assuming it fits in the class definiton given in the posted solution to Homework 3, problem 3. Note that none of the fields are declared private or protected in that code. (0.5 points)

    class Wire {
            float  delay;      // delay of this wire
            Gate   driven;     // what gate does this wire drive
            String input;      // what input of that gate does this wire drive
    
            Gate   driver;     // what gate drives this wire
    
    	public void printMe() {
    		System.out.println(
    			"wire "
    			+ driver.name
    			+ ' '
    			+ driven.name
    			+ ' '
    			+ input
    			+ ' '
    			+ delay
    		);
    	}
    }
    

    b) Add a method to class wire called toString(), so you would print the wire by calling System.out.println( w );. Give code for this method, under the same assumptions as above. (0.5 points)

    	public String toString() {
    		return  "wire "
    			+ driver.name
    			+ ' '
    			+ driven.name
    			+ ' '
    			+ input
    			+ ' '
    			+ delay
    		;
    	}
    

    c) Which of these seems the better alternative? Consider issues such as information hiding and ease of retargeting the output to other files. (0.5 points)

    They are exactly equal from the point of view of how they hide information about class wire from the outside world, and how they use information about class gate (they both need to know that there is a name field in every gate.

    The printMe() method targets System.out and would have to be changed in order to direct its output anywhere else. In contrast, the toString() method returns the text without any knowledge of where the value is being output, or indeed, whether the text is to be output. As such, it is a condsiderably more flexible interface.

  2. Background: Consider the problem of processing this line of input from a circuit description:
    wire a b in1
    

    Processing this requires that you check that gate b indeed has an input called in1 and that this input has not yet been connected by any other wire. There are three smaller problems embedded in this big problem:

    a) First, gate b must know what inputs are permitted, for example, with a list of strings. Suggest where this list goes and how it is initialized. (0.5 points)

    Every single gate has a list of permitted inputs. Therefore, this list should naturally be added as a field of each gate.

    b) Second, once a wire is connected to one of the inputs of a gate (for example, input in1 of gate b), no further connections are permitted to that input. You could do this by deleting the input from the list (delete in1 from the input list of gate b), or you could do this by keeping an auxiliary boolean variable for each input used to indicate whether it is connected or not. Assuming that you will never need the input names again, which alternative appears to lead to a simpler initializer. (0.5 points)

    If each gate has a private copy of the list of permitted inputs, then deletion is an easy way to keep track of which inputs have been used.

    If each gate of the same type (for example, each and gate) shares the same list of input names, then deletion will not work.

    Therefore, so long as we initialize each instance of class Gate to have a copy of the same list, deletion becomes an easy way to track input use.

    c) Third, where does the logic for preventing duplicate connections go? There seem to be several places it could go, for example, the code for the wire initializer could direclty manipulate the gate data structures, or the code for the wire initializer could call a special input check method of the gate, or perhaps there is another alternative. Explain how you intend to do this. (0.5 points)

    Having the code to check the legitimacy of an input anywhere outside of the gate class would reduce the effectiveness of the information hiding mechanism. Therefore, this code clearly belongs in class Gate.

    Even if we introduce subclasses of Gate devoted to different types of gates, the logic for ticking off which inputs have been used is unlikely to differ from one subclass to another. Only the initialization of the list of legal names is likely to differ. Therefore, the code belongs in the generic Gate class and not in any specific subclass.