Assignment 6, due Mar 4

Assignment 6, due Mar 4

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

  1. Background: Consider the problem in MP3 of recognizing the - token that means that a synapse has no name. Assume this declaration:
    static final Pattern dash = Pattern.compile( "-" );
    

    Now consider these two approaches to identifying whether the next token is a dash:

    if (sc.hasNext( "-" )) ...
    if (sc.hasNext( dash )) ...
    

    a) What is the advantage of using a Pattern instead of just a string. (0.3 points)

    sc.hasNext(string) is defined as being equivalent to sc.hasNext(Pattern.compile(string)), and evidently, Pattern.compile() involves complicated computation. Therefore, doing this job just once makes sense, instead of doing it over each time an attempt is made to recognize a dash.

    b) If either hasNext() is true, how do you skip over the token it found? (0.3 points)

    Use the corresponding next() method, that is, either next("-") or next(dash). In either case, discard the returned value, since you already know that it will be "-".

    c) Why not just use name=sc.next() and then ask if name is "-"? (0.3 points)

    This consumes the dash from the input, but if it is not dash, you have to hold onto the text and determine what to do with it. By using hasNext(), you leave the next token ready to be consumed by whatever part of the code needs it.

  2. Background: Consider the options of setting the name field of a synapse to null with storing the value - to signal a missing name.

    a) Note that most synapses will be anonymous. How many different strings equal to "-" will the code store if you just say name=sc.next() and then keep the string it returns as the name, regardless? (0.3 points)

    You will store as many different dashes as there are anonymous synapses.

    b) If you use null to represent missing names, how does that complicate the synapse's toString() method? (0.3 points)

    The to-string method must be prepared to substitute useful text for the null representing a dash.

  3. Background: For MP3, you must deal with both named neurons and some named synapses. There are two ways to handle this:

    a) Assuming you have a findName() method in the one-list scheme, and it returns either an object o or null if there no object had the given name. Write code to make the 3-way determination whether o is null, a synapse or a neuron. (0.5 points)

    Object o = findName( ... );
    if (o == null) {
            // o is null
    } else if (o instanceof Neuron) {
            // o is a neuron
    } else {
            // can we assume that o is a synapse?
            // yes, if the rest of the code is correct
    }
    

    b) If you want to keep just one list with the simplest possible code for findName(), what should be the relationship between classes Neuron and Synapse (they were totally unrelated in MP2). Do not give code. One carefully thought out sentence will answer this question. (0.5 points)

    They should be subclasses of a common parent class.

    c) Which of the above schemes (one-list or two-list) would be the easiest to use in the context of the code you already have for MP2? Explain your reasoning. (0.5 points)

    For my solution to mp2, the two-list scheme is easier, but I would have to write a second name-lookup routine. The reason it is easier is that this allows me to avoid re-structuring my class hierarchy.