Assignment 3, due Feb 5

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

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of lecture on the day indicated (usually Friday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: (A scavenger hunt question from the assigned readings.) Consider these two Java expressions: a&b and a&&b. (0.2 points each)

    a) In a&b, to what class (type) must a and b belong.

    Class int (or other integer types such as short or long, but this is going beyond the basic question).

    b) In a&&b, to what class (type) must a and b belong.

    Class boolean.

    c) In a[i++]&b[i++] how many times is i incremented?

    Exactly twice.

    d) In a[i++]&&b[i++] how many times is i incremented?

    Once or twice, depending on the value of the element of array a. Many students forget that the && operator is also a control sturcture, so that a[i++]&&b[i++] is exactly equivalent to a[i++]?b[i++]:false.

    e) What is similarity between the & and && operators that justifies using related symbols for them?

    They are both logical and operators, the first operating on single boolean values, the second operating on the bits of the binary representations of integers.

  2. A simple programming problem: Write a method to compute the Fibonacci function using the ?: operator in Java. Well formatted code for this function occupies about 3 lines of code — it is very compact but not very readable.

    As in the previous assignment, legible handwritten code is sufficient, but you can be more assured that it works if you try it on a computer. (1 point)

    static int fib(i) {
            return i<2 ? i : fib(i-1)+fib(i-2)
    }
    

  3. Background: Suppose you were setting out to implement a neural network simulation, as described in the notes for January 25.

    a) Give Java code that captures all of the attributes of a neuron cell body. No methods are required, just the data attributes. (0.3 points)

    class Neuron {
            float potential; // membrane potential at time
            float time;      // the time at which the potential was measured
            float threshold; // when the potential exceeds this, the neuron fires
            LinkedList  axons; // the axons involved when the neuron fires
    }
    

    b) Give Java code that captures all of the attributes of an axon. Again, no methods are required, just the data attributes. (0.3 points)

    class Axon {
            float delay; // how long it takes a signal to reach the synapses
            LinkedList  synapses; // the synapses at the end of the axon
    }
    

    c) Give Java code that captures all of the attributes of a synapse. Again, no methods, just the data attributes. (0.3 points)

    class Synapse {
            float strength; // neuron's potential change when this synapse fires
            Neuron destination;
    }
    

    d) Comment briefly on the correctness of this statement: There would be no loss of generality if axons and synapses were folded together into a single class combining the attributes of both. (0.1 points)

    The statement is correct. Anything that can be modeled as above could also be modeled using synapses with a delay attribute included.

    It was not necessary to give the following alternate data structures that result from this alternative design, but it is useful to see this code and compare it with the above:

    class Neuron {
            float potential; // membrane potential at time
            float time;      // the time at which the potential was measured
            float threshold; // when the potential exceeds this, the neuron fires
            LinkedList  synapses; // the synapses from this neuron
    }
    class Synapse {
            float delay;    // how long before this synapse fires
            float strength; // neuron's potential change when this synapse fires
            Neuron destination;
    }