Assignment 7, due Mar 8

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

  1. Background: Consider this bit of code involving Java Lambda expressions:
    ClassA x = (int i) -> i + 1;
    ClassB y = (ClassA j, int k) -> j.f( j.f( k ) );
    int    z = y.g( x, 1 );
    

    a) What is the value of z? (0.5 points)

    z is equal to 3

    b) Give the appropriate definition for ClassA. (0.5 points)

    interface ClassA {
        int f(int q); // implied by the use of j.f() above
    }
    

    c) Give the appropriate definition for ClassB. (0.5 points)

    interface ClassB {
        int g(ClassA r, int s); // implied by the use of y.g() above
    }
    

    The following little Java program works and if you run it, it demonstrates the answers to all of the above questions.

    public class T {
        interface ClassA {
            int f(int q);
        }
        interface ClassB {
            int g(ClassA r, int s);
        }
        public static void main( String[] args ) {
            ClassA x = (int i) -> i + 1;
            ClassB y = (ClassA j, int k) -> j.f( j.f( k ) );
            int    z = y.g( x, 1 );
            System.out.println( "Value is: " + z );
        }
    }
    
  2. Background: Up to to and including RoadNetwork version 2019-03-01, the ScanSupport.nextLine() method used sc.nextLine() to skip and return the remainder of the current line. This would throw an exception if the last line of the file did not end with a newline.

    The revised version 2019-03-04, based on the solution to Homework 6, used sc.skip(theRest) followed by sc.match().group(0) to get and return what was skipped.

    This change works despite the fact that sc.nextLine() and sc.skip(theRest) leave the scanner in different states.

    a) What is the difference in the scanner state? (0.5 points)

    sc.nextLine() skips the newline and leaves the scanner sitting on the first character of the next line.

    sc.skip(theRest) skips leaves the scanner sitting on the new line.

    b) Why does this difference in scanner state have no impact on the behavior of the rest of the program? (0.5 points)

    Because the rest of the program uses various scanner hasNext() and next() methods, all of which skip leading whitespace, including newlines.

  3. Background: Lectures 21 and 22 introduce the concept of a pending event set for discrete event simulation. In Lecture 22, the class PriorityQueue is used, but Java's standard library contains other classes that might also be used to do this job. (Part of the problem is that PriorityQueue should almost certainly be an abstract class with many implementations, but they locked down the name with one implementation.)

    A problem: What are some other classes in the Java library that could be used? (0.5 points)

    Suggestion: Hunt through the Java documentation starting at PriorityQueue looking at the interfaces it implements and the other methods that implement those interfaces.

    There is the very similar class PriorityBlockingQueue that, in the absence of multithreading, is pretty much the same as PriorityQueue.

    Of more interest, there is NavagableSet interface and its implementations TreeSet and ConcurrentSkipList.