Assignment 4, due Feb 12

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 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)

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

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

  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)

    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)