Assignment 7, due Mar 8
Part of
the homework for CS:2820, Spring 2019
|
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 ); } }
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.
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.