Assignment 6

Due Feb 3 on line

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. Look at the code for the solution distributed for MP3. In this solution, objects of class Place are created where the number of people associated with a place is distributed as required the median and scatter fields on the place line in the input file. Even so, there is no field in class Place to indicate the place size. Such a field is not included because ...
    a) place sizes can change during the simulation.
    b) the number of people was set by PlaceKind.findPlace().
    c) the Place constructor took care of this.
    d) this was handled by Role.populateRoles().
    e) the omission of this field is an error in the code.

    Examining the code for findPlace shows that most of that code is involved with computing the log-normal distribution and counting the population. Clearly, therefore, b) is true.

    a) is simply false. Examination of the Place constructor and populateRoles shows no code that deals with place size, so c) and d) are false. The code does limit the number of people associated with each place, so e) is false.

  2. The code distributed to solve MP3 creates a correlation between roles and places because ...
    a) new Place objects are created one at a time.
    b) Role.populateRoles creates all the people.
    c) new Person() links the person to all of its places.
    d) new Person objects are created one at a time.
    e) PlaceKind.findPlace() creates Place objects.

    Looking at the code for the Person constructor, this constructor both creates a Person object and links it to an instance of all the kinds of places associated with that person's role. The fact that places are created in findPlace on demand is a contributing factor, so if the answer c) and e) were offered, that would be the best answer. Lacking that answer, the question is, what is the best answer. We can break the correlation most easily by deferring placement of persons until after all people are created, allowing the list of people to be shuffled. This suggests that placement of people in the Person constructor is the central problem, so the best answer is c).

    Suppose, instead, we insisted on emplacing people as they are created. In this case, breaking the correlation would require randomly selecting places for each person at person creation time, which would require that all the places be constructed in advance. Advance computation of all the places for each kind of place would require major re-thinking of the entire program.

    Except in the case of multithreaded code (which this is not) all operations are done one at a time, so a) and d) are irrelevant. Looking at the code of populateRoles, there is no mention of places, it merely creates Person objects. Merely creating objects of one class does not create a correlation, so b) is wrong.

  3. The code we've written requires spaces before semicolons and other punctuation because ...
    a) that's just the way class Scanner works.
    b) class Scanner is very inflexible.
    c) we did not use the right methods of class Scanner.
    d) we did not change the delimiter used by the Scanner.
    e) nobody minds eccentric punctuation requirements.

    Class scanner has all kinds of options that can be set and a huge number of different methods for input scanning, making it a very flexible tool. This makes a) and b) obvious wrong answers. Huge numbers of programmers hate semicolons an other eccentric punctuation required in programming languages, so e) is obviously wrong.

    Changing the delimiter used by the scanner is an inviting option. If we make semicolon a delimiter, for example, much of the notation could still be scanned, but it would be hard to determine where the list of places associated with a role ends. Furthermore, nonsense such as place;;a;10;5; would be legal. If we want semicolons to remain a distinct token, the scanner's tokenizing methods are a problem because they require at least one delimiter character between consecutive tokens. This makes d) wrong.

    The scanner provides lots of methods that use the delimiter to delimit tokens, but it also provides non-token-based methods such as the various skip and find methods. This makes c) a good answer.

  4. Which of the following regular expressions best describes an identifier of the type that might be appropriate for a role name or the name for a category of places:
    a) [a-z]*
    b) [a-zA-Z][a-zA-Z0-9]*
    c) [^ ]*
    d) [a-z][^ ]*
    e) [a-zA-Z0-9]*

    a) c) and e) permit the empty string. Empty strings are not good names, so we can rule these out. c) and d) permit strings like a(*&e^%i$y# which do not match any standard ideas of naming. Similarly, e) permits names that start with a digit like 17fish; these are problematic to scan in contexts where the leading digit might make you think you were getting a number.

    b) allows names that begin with a letter (upper or lower-case), followed by any number of letters or digits. This is typical of the kind of names we use for variables and classes in programming languages, where names like i and Person and x5 are quite common. This is the best answer.

  5. In class Scanner many methods come in pairs such as next(String s) and next(Pattern p). Why offer both, since they ultimately do the same thing?
    a) The versions with String are faster.
    b) The versions with Pattern are faster.
    c) String literals avoid the need to understand regular expressions.
    d) The versions with Pattern can match more complex patterns.
    e) The versions with String are more flexible.

    Every method with a string parameter, for example skip("this") is equivalent to first calling Pattern.compile() and then calling the corresponding method. The documentation for class Scanner indirectly suggests that the pattern versions of these methods are the primitives and that the string versions are all provided for convenience, with a common pattern of construction something like this:

    public Scanner skip( String s ) {
        return skip( Pattern.compile( s ) );

    As a result, calling one of these methods with a string parameter will aways be slower than calling it with a pattern parameter, so a) is false and b) is true.

    The string passed to each of these methods is a regular expression, so using a string does not eliminate the need to understand regular expressions. So, c) is false.

    Pattern p=Pattern.compile(s) converts the regular expression s to the pattern p, and p.pattern() converts p back to a regular expression. This shows that patterns and regular expressions are equivalent and have exactly the same expressive power. Therefore d) and e) are both false.