Assignment 6, due Mar 1

Solutions

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

  1. Background: MP3 has a number of requirements.

    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)

    • Class XorGate
      A subclass of Gate for the logical XOR function
    • Class ThresholdGate
      A subclass of Gate for the threshold function
    • Class InputGate
      A subclass of Gate for the inputs
    • Class OutputGate
      A subclass of Gate for the outputs

  2. Background: In class, we developed this bit of code to clean up at the end of line after each command. Note, all the comments have been deleted.
    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.

  3. Background: The notes for Lecture 18 discuss anonymous classes and objects. Anonymous classes may be a new idea, but we have been dealing with anonymous objects for a while, that is, objects that are not directly bound to named variables. There are several examples of anonymous objects in the code distributed on Feb. 15.

    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 );
    

  4. A short question: Is it legal to declare class ScanSupport in our running example as an interface instead of a class? Explain the consequences of doing so. (0.5 points)

    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.