Assignment 5, due Feb 17

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)

    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)

  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)

    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)

  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)

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