Assignment 4, due Feb 10

Solutions

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

On every assignment, write your name and section number. Write your name as it appears on your university ID card! Use the section number as it appears in your current registration! We will not do detective work to figure out who did what. Homework is due on paper at the start of your discussion section. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God"). Never push homework under someone's door!

  1. Background: (A scavenger hunt question from the assigned readings.) Consider this Java code:
    class X {                         // a
            private int i;            // b
            private X(){}             // c
            static X factory(int v){  // d
                    X r = new X();    // e
                    r.i = v;          // f
                    return r;         // g
            }                         // h
            ... other methods ...     // i
    }                                 // j
    

    a) If line c was deleted, is there a risk that code outside of class X could create an improperly initialized instance of X? (0.2 points)

    Deleting line C would make the default constructor for X available to the outside world. All of the fields of X would be initialized with default values determined by Java and not by the needs of the other methods.

    b) Most Java compilers are not smart enough to figure out your answer to part a; as a consequence, they demand that you make a change to your program. What change? (0.2 points)

    This question was formulated in error, so everyone got full credit.

    c) If the keyword static was omitted on line d, how could code outside of class X create a new instance of X? (0.3 points)

    Given an existing instance of class X called, for example, ix, code outside of X could create a new instance by calling ix.factory(i). The problem is, there is no way for code outside of class X to get that first instance, so the outside code cannot create any instances of X.

    d) Assume that a program using the above code is completely debugged and that it works perfectly. Under these circumstances, would deleting the keyword private from line c but making no other changes have any effect on the correctness of the program? (0.3 points)

    Removing the keyword private from working programs has no effect. The program will still work the same.

  2. Background: Suppose you were setting out to implement a ternary logic simulatior, as described in the notes for January 27. Your focus here is on class Gate, where each gate has the following data:
    public class Gate {
            String name;     // the name of this gate.
            float delay;
    
            int output;      // the current ternary output value
            LinkedList <Wire> wires;
    }
    

    The simulation starts at time 0.0, at which time, the output of the gate is ternary false (represented by 0). The list wires starts out empty. The other fields are initialized from a neural network description file. The lines of this file dealing with gates look like this:

        gate A 0.6
        gate B 1.7
    

    The first number after the gate name is the delay. We will add more attributes later, but for now, the above file suffices.

    a) Write an initializer for class Gate 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 gate since the caller would only initialize a new gate 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)

    /** construct one new logic gate by scanning its description
     *  @param sc the scanner from which the description comes
     */
    Gate( Scanner sc ) {
            // assume that the keyword Gate has already been scanned
            // Bug:  what if the name is missing?
            name = sc.next();
            // Bug:  what if the delay is missing?
            delay = sc.nextFloat();
            sc.nextLine(); // consume the rest of the input line
    
            // initialize fields that don't come from this input line
            output = 0;  // that is, ternaryFalse;
            wires = new LinkedList  ();
    }
    

    b) Write a ToString() method for objects of class Gate 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)

    /** reconstruct the string describing the gate
     */
    String toString() {
            return(
                    "Gate " + name + delay
            );
    }