Assignment 3, due Feb 3Solutions
Part of
the homework for CS:2820, Spring 2017
|
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!
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.
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 ); }
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 Listoutputs = 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 }