Assignment 3, due Sept 11

Part of the homework for CS:2820, Fall 2015
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 class 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: Here is a working Java program (from the file t.java):
    class t {
            static int x(int a, int b) {
                    while (b != 0) {
                            int c = a;
                            a = c ^ b;         //a
                            b = (c & b) << 1;  //b
                    }
                    return a;
            }
    
            static int y( int i ) {
                    return (i < 3)? i: x(x(y(i - 1), y(i - 2)), y(i - 3) );
            }
    
            public static void main( String[] args ) {
                    for (int i = 0; i < 11; i++) {
                            System.out.println( "y(" + i + ") = " + y(i) );
                    }
            }
    }
    

    Warning: the above code has no useful comments because it is designed as a scavenger hunt to motivate a close reading of the textbook's summary of Java operators. In any other context, there would be no excuse for writing such obscure code, particularly without any explanatory comments.

    Answer the following: (0.2 points each part)

    a) What does the ^ operator do on the line marked //a?

    b) What does the & operator do on the line marked //b?

    c) What does the << operator do on the line marked //b?

    d) Is ? an operator, and what does it do in the method named y?

    e) Is : an operator, and what does it do in the method named y?

  2. A short question: What does the method named x do in the code given with problem 1? (You may need to do some experiments in order to determine this.) (0.5 points)

  3. Background: Informally, digital logic gates are devices where each has multiple inputs and one output. The output of a gate may be connected to zero or more inputs of other gates by wires. Each gate has a time delay, as does each wire. The fundamental events in a digital logic system are changes in the values of the inputs or outputs of gates.

    There are many types of gates. For example, an and gate produces an output of true as a result of all of its inputs becoming true (but only after a time delay), and it output of false as a result of any inputs becoming false (again, after a time delay). There are an open-ended number of different kinds of gates. For any particuar kind of gate, there are specific named inputs, for example, every two-input and gate might have inputs named a and b.

    Each wire transmits its input (the output of some gate) to its output (a specific input of some gate) after the wire's delay.

    Answer the following: (0.5 points each part)

    a) Give Java code to define a class that captures all of the attributes of a wire, as described above. We are not interested in methods, just a description of the data.

    b) Give Java code to define a class that captures all of the attributes of a gate, as described above. Because there are subclasses of gates, you should only include material that you infer, from the above, is universally applicable to all kinds of gates.

    c) Focusing only on the information given above, suggest, at a fairly high level, what a text file might look like that is used to initialize a simulation model. Think as if you might have to implement this, so don't describe anything complicated. Keep it as simple as possible!