Assignment 5, due Feb 19

Solutions

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

  1. Background: Look at the example code distributed with the Feb. 12 lecture. There are several bug notices that ask what happens if there is no next token. For example, after the assginment to sourceName in the initializer for class Road.

    a) What happens. This is a scavenger hunt question best answered by an internet search to find the relevant Java documentation. (0.4 points)

    It throws a NoSuchElementException if there are no more tokens.

    b) Suppose you wanted missing identifiers to default to the string "---". Write code to replace the initialization of sourceName that does this. (0.6 points)

    First, we need to do some planning. Road descriptions are of the form road src dst time. If either the source or destination is missing, there will be one intersection name; you cannot distinguish these, so the easy (and therefore sensible) thing to do is to assume that, when looking for a source, if you find an intersection name, that is sensible. If you find a number, both must be missing. We don't need to worry about throwing an exception, since we can use "has next" methods to test.

    So, we replace this code:

            String sourceName = sc.next();
            source = RoadNetwork.findIntersection( sourceName );
    
    with something like this:
            String sourceName = null;
            if (sc.hasNext() & !sc.hasNextFloat()) {
                    sourceName = sc.next();
                    source = RoadNetwork.findIntersection( sourceName );
            } else {
                    sourceName = "---";
                    source = null;
            }
    

    The above code goes beyond the requirements of the assignment (it doesn't simply replace the name, it also sets the source object to null). The code is also not perfect. It doesn't detect newlines in the wrong place; doing that would be going beyond what we've discussed how to do up to this point in the semester.

  2. Background: There are bug notices in the code that ask "should we prevent creation of this object" in cases where the object is so malformed that it might not make sense to create it.

    a) How can a Java object constructor exit in such a way that no object is created and returned? (0.5 points)

    By throwing an exception in the constructor, and leaving it to the caller to catch that exception.

    There are two ways to exit any method (including exiting constructors). Either you return, or you exit abnormally by throwing an exception. In the case of constructors, the object you are constructing is already allocated by the time the code for your constructor begins executing, and if you return normally, that object will be returned.

    If you exit a constructor by throwing an exception, the object that was initialize on entry to the constructor is discarded. The caller never learns that it existed, briefly. The only thing the caller gets is whatever information was thrown with the exception.

    b) Suppose you wanted the code that constructs the object to return null as a signal that no object was constructed. A constructor cannot return null, so you would have to replace calls to new Neuron() with calls to the method named newNeuron(). How can you allow code within the newNeuron() method to use the default constructor for class Neuron without allowing outside code to call that constructor?

    Explicitly declare an initialier Neuron() as private. Once you do this, that initializer can only be called from within methos of class Neuron and it is illegal to call it from outside.

  3. Background: Our model of a neuron based on a model proposed by McCulloch and Pitts in 1943, with minor enhancements to reflect the basic biology of neurons. In our model, the two basic types of synapse, excitatory and inhibitory, have been distinguished by just one thing, the sign of the synaptic strength. Excitatory synapses send positive signals, inhibitory synapses send negative signals. This is a gross oversimplification of reality.

    In fact, there are fast excitatory synapses, slow excitatory synapses, fast inhibitory synapses, and slow inhibitory synapses, each involving different neurotransmitters (the chemicals that actually convey the message across the synaptic gap). Furthermore, there is also a special kind of synapse (we'll call it a secondary synapse). Where primary synapses connect axons to the cell body of a different neuron, secondary synapses connect axons to other (primary) synapses. When a secondary synapse fires, it decreases the strength of the primary synapse to which it is connected.

    A question: How could you use polymorphism in a Java neuron-network simulator in order to account for these differnces. Specifically, what class would be the parent class and what would be the appropriate subclasses needed to incorporate the new information presented above. (1 point)

    The simplest answer is that class Synapse would be the parent and classes FastExitatorySynapse, FastInhibitorySynapse, SlowExitatorySynapse, SlowInhibitorySynapse and SecondarySynapse would all be extensions of the same base class.

    The first 4 differ only in neurotransmitter, while the final one differs in topology (it connects to a named primary synapse), so we could refine this as follows:

    • Base class:
    • Subclasses of Synapse: PrimarySynapse and SecondarySynapse.
    • Subclasses of PrimarySynapse: FastExitatorySynapse, FastInhibitorySynapse, SlowExitatorySynapse, and SlowInhibitorySynapse.

    Further refinement is not justified by the information given above.