Assignment 6, due Mar 1Solutions
Part of
the homework for CS:2820, Spring 2019
|
A problem: List all of the new classes must you add to a solution of MP2 to make a good solution to MP3. For each class, give a one line description appropriate for use as the first line of a Javadoc comment. (0.5 points)
public static void nextLine( Scanner sc, String msg ) { final String remainder = sc.nextLine(); if (remainder.equals( "" )) return; if (remainder.startsWith( "--" )) return; Errors.warn( msg + ": " + remainder ); }
A problem: This code is broken. It does not properly handle lines with trailing blanks, it cannot recognize comments, and it throws an exception if it sees an ill-formed last line of a file (a line with no trailing newline character). Fix it!
Hint: The Scanner class has some really useful methods called skip, and you will want to use your fixed code in your solution to MP3. (1.0 points)
There are, of course, a huge number of possible solutions. Here is one:
private static final Pattern blanks = Pattern.compile( "[ \t]*" ); private static final Pattern restOfText = Pattern.compile( ".*" ); public static void nextLine( Scanner sc, String msg ) { // first skip trailing blanks, if there are any sc.skip( blanks ); // then get the remainder of the line. Here is a clumsy solution final String remainder; if (sc.hasNextLine()) { remainder = sc.nextLine(); } else { sc.skip( restOfText ); remainder = sc.match().group( 0 ); } // finally check that the remainder of the line is legal if (remainder.equals( "" )) return; if (remainder.startsWith( "--" )) return; Errors.warn( msg + ": " + remainder ); }
The business about using skip to skip a pattern and macth.group to recover the pattern is really obscure, but it seems to be the only way to get the rest of the input if there is no next line. Once you discover that, you can make a very compact replacement for the clumsy code given above, because a pattern can be made that matches text up to the end of line or end of file, whichever comes first, eliminating the need for the if statement and the call to hasNextLine.
a) Identify all of the anonymous objects that are instantiated in the main method. (0.5 points)
The following line instantiates a pair of anonymous objects, a scanner and a stream.
buildNetwork( new Scanner( new FileInputStream( args[0] ) ) );
b) Rewrite the relevant part of the main method so that each of the anonymous object is bound to an appropriately named variable, declared as local to the point use as is reasonable. The purpose of this exercise is to drive home how anonymity can sometimes make code a bit more compact and readable. (0.5 points)
final InputStream myStream = new FileInputStream( args[0] ); final Scanner myScanner = new Scanner( myStream ); buildNetwork( myScanner );
Yes, all the components of this class are static and it is never instantiated, so this is OK.
The consequences have to do with potential abuse. We could implement this interface, but since it does not have any non-static methods, that has no value so it offers little temptation to abuse.