Assignment 7

Due March 10, 2021

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

Simple multiple choice questions, 1 point each:

  1. The Java regular expression that matches a begin parentheses is "\\(" Why the two backslashes in this string?
    a) To put a single backslash in a string, you need to double it.
    b) In a pattern, an un-quoted begin paren means start of group.
    c) The pattern that matches begin paren requires a leading backslash.
    d) Backslash is a quote mark in both Java string constants and patterns.
    e) All of the above. — correct

    A comment here seems unnecessary.

  2. Given that the pattern \s (lower case) means any whitespace character, and the pattern \S (upper case) means the opposite, any character that is not whitespace, here is a proposed implementation of the next(String pattern) method in class Scanner:
        public String next( String pattern ) {
             this.skip( "\\s*" ); // skip whitespace
             this.skip( pattern )
             return this.match().group()

    Now, how can we write hasNext(String pattern) using more primitive parts of the class (the parts that ignore delimiters)?

    a) We can't because hasNext() would have to skip whitespace.
    b) The hasNext() method can be built on top of findWithinHorizon().
    c) Just return false if skip throws an exception.
    d) We can't because the more primitive Pattern matching methods of class Scanner all advance the scanner over whatever the pattern matches. — correct
    e) We can use skip() to see if the pattern is there and then, if it is, use reset() to put it back so our version of next() can re-skip it.

    If we made hasNext() skip whitespace, you would have to work very hard to notice that it had done so, so a) is not a big problem. Both findWithinHorizon() and skip() advance the scanner if the pattern matches. As a result, there is no way these can be used to do hasNext() unless there is a way to back up the scanner and un-skip the pattern that just matched. This rules out b) and c) and also suggests that d) may be the answer. The answer would be e) except for one big detail, reset() on a scanner does not "put it back" to where it was before a call to skip().

  3. Consider this Java class:
    abstract class C {
        protected int i;
        public abstract int m( int i );

    a) Subclass of C must offer a method m. — correct
    b) It is illegal to create subclasses of C.
    c) Implementations of method m cannot access i.
    d) Instances of C will have field i but no methods.
    e) all of the above.

    The main point of abstract classes is to allow them to be used to form subclasses, so b) is very wrong. Declaring a field to be protected permits it to be seen from subclasses, so c) is wrong. You cannot create an instance of C, so d) is not quite right. Any wrong answers rule out e).

  4. Warning: There is a typo in the following text! The reference to MP5 is wrong. This is referring to the versions of the scanner distributed on Mar 3 and revised on Mar 5-10 in ways that do not change its behavior in a way that impacts this problem.

    For MP5, the MyScanner class implementation works with semicolons, dashes and parentheses so long as they are ...
    a) both preceeded and followed by whitespace.
    b) preceeded by whitespace, but may be followed by anything.
    c) followed by whitespace, but may be preceeded by anything.
    d) the last character on their line.
    e) are not the first character on the line.

    The version of MyScanner in question uses hasNext() to see if the pattern is there. If there was whitespace before the pattern, it will be ignored, but whtespace is not required there, as you can easily determine by running the highway network simulator to see how it works with spaces before and after semicolons. Using hasNext() does require whitespace after the semicolon. Therefore, a) and b) are false, and c) is true. Semicolon as the first character on the line is unconventional and as the last character on a line is conventional, but all the scanner ever cared about was whitespace, and end of line is just whitespace, so d) and e) are misleading, at best.

  5. Knowing what you know about string concatenation, how many new objects are created in order to evaluate the return statement below:
    int a = some + unknown + expression;
    return "a = " + a;

    In answering this, assume that if sb is a StringBuilder, then sb.toString() is not optimized; it always makes a copy of the stringBuilder into a string. Also, do not forget about autoboxing.

    a) 1
    b) 2
    c) 3
    d) 4 — correct
    e) 5

    First, let's go through the exercise of removing the syntactic sugar from that return statement. First, let's use a StringBuilder to do the concatenation:

    return new Stringbuilder( "a = " ).append( a.toString() ).toString();

    That's just a start, because a is an int, not a real object. To apply the toString() method to it, we need convert it to class Integer; this is what autoboxing does:

    return new Stringbuilder("a = ")
    	  .concat(new Integer(a).toString())

    Now, we can start counting:

    • new Stringbuilder()
    • new Integer()
    • Integer.toString
    • StringBuilder.toString

    That comes to 4, so d) is right.

    The string constant "a = " does not involve creating a new object at run-time. This constant was created by the compiler and put in memory when your program begins running. No matter how many times you call the method that has this return statement, it will always use the same constant and not create any new objects.