Assignment 3, due Feb 1

Solutions

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

  1. Background: Step 6 of MP1 asks you to not merely complain about extra arguments, but to output an error message that includes the extra argument. It is easy to solve this so that it outputs the first extra argument, and that is all that is required by the assignment. Error messages that belabor the issue are not usually worth the effort.

    For the sake of this exercise, though, consider the problem of outputting all of the extra arguments, so that the program behaves as follows:

    [HawkID@fastx?? ~]$ java MP1 this that
    MP1: Extra argument that
    MP1: Can't open this
    [HawkID@fastx?? ~]$ java MP1 this that and the other
    MP1: Extra arguments that and the other
    MP1: Can't open this
    

    a) Explain how you would change the code in method main would change in order to output either "Extra argument" or "extra arguments" as illustrated above. Think before you write. Suggest the easiest solution. Note: Don't write detailed code for this, instead, write a brief description of the change you would make. (0.5 points)

    As written, it just used (length > 1) to detect and report extra arguments. The easy change to make it operate as suggested could use (length == 2) to report one extra argument and (length > 2) to report multiple extra arguments.

    b) Explain what you would have to do in order to make the call to Errors.warn print all of the arguments, not just the first, in the event that the message begins "extra arguments." Again, don't actually write the code, but instead, give a brief description of the code required. (0.5 points)

    In the case where it reports multiple extra arguments, it would need to iterate through the extra arguments concatenating them (with spaces between them) and then concatenate that to the error message as a parameter to Errors.warn().

  2. Background: We will be using Java's Scanner class too process text files in this class. There are a number of ways to open a file and scan the text from it:

    When you encounter a class with such a variety of constructors, it is quite likely that one of them is the basic constructor that directly builds the class instance, while the others are quite probably "convenience" constructors that do some preliminary work before they call the basic constructor or alternatively before they duplicate the code of the basic constructor.

    a) Which of the above is probably the basic constructor for class Scanner? (0.5 points)

    Scanner s = Scanner( FileInputStream( File( "filename" ) ) );

    b) One of the above constructors does the most auxiliary work before calling the basic constructor. Write the code for that constructor, using a call to the basic constructor to finish the job. (0.5 points)

    public Scanner( String name ) {
    	this( FileInputStream( File ( "filename" ) ) );
    }
    

    A quick web search was all it took to find out how to call one constructor for a class from another constructor for the same class.

  3. Background: When we start to build a logic simulation, we will need to describe the system of logic gates (a logic circuit) somehow. An obvious way to do this is with a text file that shows how the gates are interconnected. Consider the following:
    gate a 0.5 xor
    gate b 1.0 threshold 1 F
    gate c 0.7 threshold 2 T
    wire a 3.0 b c
    wire b 1.8 c a
    wire c 0.9 a
    

    In the above, gates have names, here a, b and c, and time delays from input to output, here 0.5, 1.0 and 0.7. Gates also have types, here xor and threshold. Threshold gates have additional attributes, the number of inputs that must be true for the output to be asserted, and the value to assert. Thus, gate b will assert false on its output if at least one input is true, and true otherwise, while gate c will assert true if at least two inputs are true, and false otherwise.

    Wires have an origin, a delay and one or more destinations. Thus, the first wire listed above goes from gate a to gates b and c with a delay of 3.0.

    The delays for gates and wires must, of course, be non-negative.

    a) Suppose a threshold gate has three inputs, how do you set the threshold and output so that it computes the boolean and of these inputs? (0.3 points)

    Set the threshold to 3 with a true output. That is, the output will be true if all three inputs are true and false otherwise.

    b) Suppose a threshold gate has two inputs, how do you set the threshold and output so that it computes the boolean nor of these inputs? (0.3 points)

    Set the threshold to 1 with a false output. That is, the output will be false if any input is true and true otherwise.

    c) (0.4 points) Suppose the only gate types in our simulation are xor and threshold. For each xor gate, a logic simulator could check, before starting the simulation, that there are exactly two input connections to that gate. What sanity checks could the simulator perform for each threshold gate?

    If the threshold is n, there should be at least n inputs connected to that gate. If there are fewer inputs, then the gate performs no useful computation because there is no way for its threshold to be met.