Assignment 4, due Sept 18

Solutions

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

  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)

    Consider two methods, one declared float a() and the other int a(). In the statement x=a() we could look at the class of x to distinguish between these two methods named a(). Unfortunately, it is legal to write a() as a statement by itself, in which case the return value is discarded. The decision of the designers of the Java language to permit this ruled out allowing for this overloading rule.

  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)

    Here is a solution:

    class A {
    	int i;
    	A( int i ) {
    		this.i = i;
    	}
    }
    

  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)

    First, a note: We have not discussed any behavioral attributes yet, but only data attributes. Here, let's look at the attributes discussed in the posted solution to Homework 3, problem 3 part b:

    The instance variables delay, name, driven, and driver all apply equally to every gate and therefore belong in the generic gate class.

    The names of the multiple named inputs are specific to each subclass. Therefore, the list of these names must, at the very least, be initialized in the subclass, and in fact, this list might be an attribute of the subclass.

    We have not discussed any other attributes at this 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)

    It depends on how the initialization is done. We could write initializers that load the values into the fields, in which case, the fields can all be private, or we could rely on the default initialization of the fields (null lists, empty strings, numerical zeros) and then reach in and set the fields from outside. In that case, the fields cannot be private.

    When we start writing methods, we will be able to give more refined answers to this quesiton.