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 modified solution to MP4 that was broken up into multiple source files as a starting point for your solution to MP5.

    a) How many source files do you get when you unpack this shell archive by following the directions given at the head of the text? (The easiest way to do this problem is to simply follow the instructions and then count the number of files you get, but you could also read through the archive and count them in their archived form.) (0.5 points)

    Here's what I got when I followed the instructions:

    [dwjones@serv16]$ mkdir mp5
    [dwjones@serv16]$ cd mp5
    [dwjones@serv16 mp5]$ sh < ../mp5shar.txt
    [dwjones@serv16 mp5]$ ls
    Errors.java  NeuronNetwork.java   ScanSupport.java       Simulator.java
    Neuron.java  PrimarySynapse.java  SecondarySynapse.java  Synapse.java
    [dwjones@serv16 mp5]$ 
    

    So, the answer is 8

    b) If you look at the Javadoc comments in the the new code and compare them to the Javadoc comments in the solution distributed to MP4, you'll see that the volume of commentary has been significantly enlarged. Why is this appropriate? (0.5 points)

    With all the classes separated into different source files, connections between classes that were documented by the way they were grouped in a single source file are no longer there, so the connections must be made by comments.

  2. Background: Read the assignment for MP5 and explain what methods you can infer must exist inside class SimulationOutput. For each method, the assignment allows you to infer different information. For some, you can infer the method name. For some, you can infer whether the method is static, for some, you can infer whether the method is public or private, and for some, you can infer the parameter list. For each method, give a one sentence description of what it does and then give the items you can infer and briefly state the reason you can infer that. (1.0 point)

    The new class has the following methods:

    • static void setOutput(Scanner sc)
      Scans the output line from the input file and initializes this class. This information is all inferred from the assignment.
    • private static void [cannot infer name] (float t) Print the header line and schedule the first output event. Private because it is not part of the public interface to the class, static because no instance of class SimulationOutput is ever created, void because it is scheduled and scheduled activities never return anything; the parameter t is there because it will be scheduled, and this heading follows the typical heading of scheduled events.
    • private static void [cannot infer name] (float t) Print a line of output and schedule the next output event. The logic of all the attributes given is the same as above.

    Note: The header-print method need not have a parameter, since it is always scheduled at time zero. Also, if you add some state variables, for example, a variable indicating that the header is already printed, you could make the print output routine print the header the very first time it is triggered.

  3. Background: The version of ScanSupport.java included in the multi-file source distribution given for MP5 contains marginally decent Javadoc documentation. It is the only file in the distribution that is up to this standard.

    A problem: Give a version of Errors.java that meets this standard. (1.0 points)

    /** Error reporting methods.
     *  @author: Douglas W. Jones
     *  @author: Andy W.M. Arthur
     *  @version: April 19, 2016
     * 
     *  This code is extracted from the April 6, 2016 version of NeuronNetwork.java,
     *  which, in turn was based on the non-alternative solution to MP3,
     *  as well as a simulation framework from the March 11 lecture notes.
     *  Better Javadoc comments were added April 19.
     */
    class Errors {
    
        /** Count of the number of errors reported so far.
         *  This is incremented by calls to methods of class {@code Errors}
         *  and my be inspected to see if and how many errors have been
         *  reported.
         */
        public static int errCount = 0;
    
        /** Called to report a fatal error
         *  @param message  the text of the error message
         *  @return void    does not return
         *  Reports the error to the {@link System.err} stream and then
         *  terminates the application.
         */
        public static void fatal( String message ) {
            errCount++;
            System.err.println( "Fatal error: " + message );
            System.exit( 1 );
        }
    
        /** Called to warn of a non-fatal error
         *  @param message  the text of the error message
         *  @return void
         *  Reports the error to the {@link System.err} stream and then
         *  returns, allowing continued processing.
         */
        public static void warning( String message ) {
            errCount++;
            System.err.println( "Error: " + message );
        }
    }