Assignment 3, due Feb 3

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 the statement if((a!=null)&&(a.x()))a.y();

    a) What feature of the a&&b makes it classify as a control structure? (0.3 points)

    If a is true, b is evaluated, while if a is false, b is not evaluated. In this regard, it is just like an if statement.

    b) Rewrite this as 2 nested if statements. (0.4 points)

    if (a!=null) {
            if (a.x()) a.y();
    }
    

    c) What should be the type of the return value of the method x? (0.3 points)

    It should be boolean.

  2. A simple programming problem: Write a method to compute the function f() defined in homework assignment 1 using Java's ?: operator. Well formatted code for this function occupies about 3 lines of code, but it is not very readable.

    As in the previous assignment, legible handwritten code is sufficient, but you can be more assured that it works if you try it on a computer. (1 point)

    static int f( int i ) {
            return (i < 3)? i: f( i - 1 ) + f( i - 2 );
    }
    

  3. Background: Suppose you were setting out to implement a ternary logic simulator, as described in the notes for January 25, where the ternary logic values are represented as integers, 0 for false, 1 for unknown and 2 for true.

    a) Give Java code that captures all of the attributes of a ternary logic gate No methods are required, just the data attributes. (0.5 points)

    class Gate {
            String name; // the gate's name
    
    	// what wires does this send outputs into
    	List  outputs = new LinkedList  ();
    
    	// what wires does this gate get inputs from (do we need this?)
    	List  inputs = new LinkedList  ();
    
    	int value;   // ternary value from 0 to 2 of the output of the gate
    	float delay; // time delay of gate
    }
    

    b) Give Java code that captures all of the attributes of an wire between logic gates. Again, no methods are required, just the data attributes. (0.5 points)

    class Wire {
    	// the wire's name is based on its source and destination
    	Gate destination;  // where does the wire go
    	Gate source;       // where does it come from (do we need this)
    
    	int value;   // ternary value from 0 to 2 of the output of the wire
    	float delay; // time delay of wire
    }