Assignment 5, due Feb 17

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: A factory method is a method that, like a constructor (initializer) returns a newly built instance of a class. Consider this example:
    class MyClass {
    	private Field f; // guaranteed to be non null
    
    	// constructors
    	private MyClass() {} // prevent public use of default constructor
    	MyClass( Field p ) {
    		// Bug: missing code to prevent p == null
    		f = p;
    	}
    
    	// factory
    	static MyClass factory( Field p ) {
    		MyClass m = new MyClass();
    		// Bug: missing code to prevent p == null
    		m.f = p;
    		return m;
    	}
    }
    

    Note that, as written above, MyClass(x) and MyClass.factory(x) do exactly the same thing. The options for assuring that the parameter is not null are different, leading to potential different behaviors.

    a) In the constructor, what can you do to detect and prevent MyClass(null)? (0.5 points)

    About the only option that prevents anything would be for the constructor to throw an exception. It could do this directly, for example with if(p==null)throw e; or it could use an assertion such as assert(p!=null);

    Preventing MyClass(null) implies more than merely outputting an error message. In that case, we didn't prevent the constructor from constructing a null field in violation of our assertion that it will never be null. Assigning a non-null default value to the field also fails to prevent the call to the constructor from succeding; that option would allow the call to construct an object that was different from the one the user tried to create.

    b) In the factory, what can you do to detect and prevent MyClass.factory(null)? (Suggestion: If some of the alternatives are the same as in part a, say so and focus on the differences.) (0.5 points)

    Everything that the constructor could do applies here too, but a factory method also has the option of returning null with code such as if(p==null)return(null);.

  2. Background: Ternary logic gates come in several varieties. In the assignment for MP2, they are all described by input lines with the same format, the keyword "gate" followed by the name, the type, the number of inputs permitted, and the delay. In fact, for over half of our gate types, the number of inputs permitted is redundant, since only min and max gates are well defined over arbitrary numbers of inputs.

    It is natural to build a class hierarchy, where classes MinGate, MaxGate, NegGate, etc. are subclasses of Gate. This allows each kind of gate to have its own logic. Naturally, the circuit description language should be modified so one-input gates like neg do not require a count of their inputs:

    gate A min 4 1.0
    gate X neg   0.5
    

    a) Given that the main program picks the keywords gate and wire from the input, calling the constructors for classes Gate and Wire, how would you divide the remaining work between the constructors for classe Gate and its subclasses. That is, which fields from the input line would be processed by class Gate and which fields would be processed by a subclass of Gate. (0.5 points)

    It would make sense to process the number of inputs permitted in the appropriate subclass of Gate. This way, gates like neg could forbid specifying an input count and perhaps set the count value to one.

    b) Would there be value in a deeper class hierarchy? That is, would there be value in having an intermediate layer of subclasses between, say, class Gate and class NegGate or between class Gate and class MinGate? If you answer yes, justify your answer by giving a natural grouping of the subclasses of Gate under each appropriate intermediate classes. (0.5 points)

    Yes, we could have two subclasses of Gate, the names suggested here are merely suggestions:

    Class MultiInputGate could have subclasses MinGate and MaxGate.

    Class SingleInputGate could have subclasses NegGate, IsFalseGate, IsUnknownGate and MaxGate.

  3. Background: In the code distributed with Lecture 11 on February 10, instances of class Intersection are declared to have a field outgoing that is a list of the roads leading away from that intersection. This list is never used in the code that was distributed. It was only included because we speculated that we would need it later.

    The fact is, we will need it. So, when a road is defined from one intersection to another, there must be something in the initializer for class Road like:

    if (source != null) {
    	// Bug: need code here to add this road to source.outgoing
    }
    

    We are likely to also want to add something like this to class Intersection:

    public addOutgoing( Road r ) {
    	outgoing.add( r );
    }
    

    a) Why does the code distributed to the class force us to add a method to class Intersection? (0.5 points)

    Because outgoing was declared to be private.

    If it had not been a private field, we could have written something like
    if (source!=null) source.outgoing.add( this );.

    b) Write the code to call addOutgoing from class Road. This code replaces the bug notice given above. (0.5 points)

    source.addOutgoing( this );