Assignment 4, due Sept 18

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: The Java library class java.util.Scanner contains the method nextLine() that consumes the remainder of the current line from the scanner's input stream and returns that text as a string. So if s is a string variable and sc is a scanner, it is reasonable to write:
    s = sc.nextLine();
    

    Java allows you to call a method for its side effect and discard the return value. For example, you can write this code to skip the rest of a line and discard the text:

    sc.nextLine();
    

    A Scavenger hunt question: How does this language feature interfere with a possible alternative overloading rule that might otherwise be a useful extension to Java? (0.5 points)

  2. Background: Suppose you have this code:
    class A {
    	int i;
    	A( int i ) {
    		// missing code
    	}
    }
    

    The goal is that the initializer call A(5) should initialize field i of the newly allocated instance of class A to the value 5. The problem is, the field name is the same as the formal parameter name used in the initializer. One way to solve this is to use a different formal parameter name. You could write this:

    class A {
    	int i;
    	A( int j ) {
    		i = j;
    	}
    }
    

    A Scavenger hunt question: There is another solution to this problem that does not involve changing any field or parameter names. What is it? (0.5 points)

  3. Background: Java allows construction of class hierarchies. Classes may be declared as subclasses of other classes.

    In the logic simulation problem, the class gate contains, up to this point, a bug notice saying that there are many kinds of gates, each with different behavior. Specifically, every gate has a method called inputChange(t,i,v) that is used to tell the gate that, at time t, input i, of that gate changed from its previous value to value v. This method determines whether the will schedule an output change event at some later time, depending on all the current values of its inputs and output and depending on the type of gate.

    A Problem: Given that there are gates that perform each of the classical functions of Boolean logic, how would you use the subclass mechanism to describe different kinds of gates? Specifically, which attributes of a gate would be defined in the generic gate class and which attributes would be defined in the specific subclasses for the different kinds of gates? (1 point)

  4. A short question: Up to this point, we have not worried much about access control in our simulation examples (the road network, the logic circuit). Have we discussed any fields of any classes in the simulation that should not be private? If so, should they be public or should they be protected. Your answer to this question will depend in significant part on your answer to the previous question. (1 point)