Assignment 6, due Mar 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: Consider these two code fragments:
    while (sc.hasNext( "--*" )) ...                      //1
    
    static final Pattern p = Pattern.compile( "--*" );   //2
    while (sc.hasNext( p )) ...                          //2
    

    a) What strings does the regular expression --* match? (0.3 points)

    The strings -, --, --- and so on; any string of at least one dash.

    b) Why is it likely that the second fragment will be faster? (0.3 points)

    The call to sc.hasNext() is inside a loop. If the parameter is a string, this must be converted to a pattern on every iteration. It is better to convert it just once outside the loop.

    c) Rewrite the following to apply the same optimization as was used to transform fragment 1 above into fragment 2. (0.4 points)

    for (String s: ... ) System.out.println( str + " " + s );
    

    Here is a reasonable answer:

    String q = str + " ";
    for (String s: ... ) System.out.println( q + s );
    

  2. Background: In the code discussed in class on Monday Feb. 27, we converted the constructor for class Intersection into a factory method called newIntersection(). This was called here:
    if (("intersection".equals( command ))
    ||  ("i".equals( command ))           ) {
    	inters.add( newIntersection( sc ) );
    
    } else if (("road".equals( command ))
    

    There are two reasonable options for how newIntersection() can signal that the description of an intersection was so defective that nothing should be added to the list inters. It could throw an exception or it could return null.

    a) Assume newIntersection() throws a BadIntersection exception. Rewrite the code given above to handle this exception in such a way that interes.add() is not called when newIntersection() throws an exception. (0.5 points)

    if (("intersection".equals( command ))
    ||  ("i".equals( command ))           ) {
    	try {
    		inters.add( newIntersection( sc ) );
    	} catch (BadIntersection e) {
    		// do nothing here
    	}
    
    } else if (("road".equals( command ))
    

    b) Assume newIntersection() can return null. Rewrite the code given above to handle this in such a way that interes.add() is not called when null is returned. (0.5 points)

    if (("intersection".equals( command ))
    ||  ("i".equals( command ))           ) {
    	Intersection i = newIntersection( sc );
    	if (i != null) {
    		inters.add( i );
    	}
    
    } else if (("road".equals( command ))
    

  3. Background: Consider this code a.method( b.expensive(c) ); where a is an instance of Aclass which has a method called method() that rarely uses its parameter. The object b is an instance of Bclass which has a very expensive method called expensive; this method takes one parameter of class Cclass and returns a value of class Eclass.

    The declaration for method() begins method(Eclass e). Later, it contains the code if(unlikely)use(e); where use() is some other method in the same class.

    a) You could just rewrite method() so that it is called as a.method(b, c); passing the parameters b and c without evaluating expensive(), thus avoiding wasted computation. Assume this rewrite and give the declaration for the parameters to method() and the new version of the code in the if(unlikely) statement. (0.5 points)

    method( Bclass b, Cclass c ) {
    	...
    	if (unlikely) use( b.expensive(c) );
    	...
    }
    

    b) The above solution to avoiding wasted computation could create problems. Demonstrate the difficulties this would create by describing at least one legal calls to method() that would be difficult to rewrite to take advantage of the rewritten version of method() from part a. (0.5 points)

    Consider this code a.method( f.othermethod(g, h) ); The only thing this has in common with the original is that othermethod() declared in Fclass also returns an object of class Eclass.