Assignment 4, due Feb 12

Solutions

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

  1. Background: (A scavenger hunt question from the assigned readings.) Consider this Java code:
    class X {
            private int i = 1;
            public int j = 2 * i;
            private X(){}
            public X( int i ){
                    this.i = i;
            }
            ... other methods of X ...
    }
    ... other classes that use instances of X ...
    

    a) Where is it legal to write X x = new X(); (0.4 points)

    Inside any method, initializer or finalizer in class X (it should even be possible to define a static field of X this way, but not a field of X because that would lead to an infinite storage requirement).

    b) Suppose the program worked correctly, and then someone deleted the line private X(){}. Would the program still work correctly? (0.3 points)

    Yes.

    c) After the call to X x = new X(5); what is the value of x.j?

    x.j is 2. This is twice the initial value of x.i. The point of this exercise is tht initializations done in the declarations of fields take place before the body of the initializer method is executed.

  2. Background: Suppose you were setting out to implement a neural network simulation, as described in the notes for January 25. Your focus here is on class Neuron, where each neuron has the following data:
        public class Neuron {
                String name;     // the name of this neuron.
                float threshold; // the threshold voltage for this neuron.
                float voltage;   // the voltage of this neuron at the given time.
                float time;
                LinkedList <Synapse> synapses;
        }
    

    The simulation starts at time 0.0, that is the initial value of time. The list synapses starts out empty. The other fields are initialized from a neural network description file. The lines of this file dealing with neurons look like this:

        neuron A 1.0 0.95
        neuron B 1.0 0.0
    

    The first number after the neuron name is the threshold, the second number is the voltage at time 0.0

    a) Write an initializer for class Neuron that takes a Scanner as a parameter and scans one line of the input file, scanning the name and the initial values from the input file. Note that the caller of this initializer should have already scanned over the keyword neuron since the caller would only initialize a new neuron when a line of the input file begins with that keyword.

    For this assignment, do not worry about detecting duplicate names. Turn in legible well formatted and commented code. You need not test this code, but you will be graded on correctness and readability. (1.0 points)

    // initializer
    public Neuron( Scanner sc ) {
            // scan and process one neuron
            name = sc.next();           // the neuron name
            threshold = sc.nextFloat(); // the neuron threshold
            // Bug: what if no next float?
            voltage = sc.nextFloat();   // the initial voltage of this neuron
            // Bug: what if no next float?
            time = 0.0;                 // the initial time
            // synapses is already null
            String skip = sc.nextLine();
            // Bug:  What if more junk at end of line (skip not empty)
    }
    

    The comments above are based in part on the comments in the road-network code as of Feb. 10. It is reasonable to assume that students doing this assignment can take advantage of that code as a template. None of these comments is strictly needed, but comment free code is not acceptable here.

    b) Write a ToString() method for objects of class Neuron that returns a string representing the entire line of the input file that could have been used to initialize that object.

    Turn in legible well formatted and commented code. You need not test this code, but you will be graded on correctness and readability. (1.0 points)

    // other methods
    public String toString() {
            return (
                    "Neuron " +
                    name + " " +
                    threshold + " " +
                    voltage
            );
    }
    

    The lack of comments above is based in part on the similar lack of comments in the road-network code distributed as of Feb. 10. This code is just too simple to need much if any commentary.