Assignment 6Due Feb 3 on line
Part of
the homework for CS:2820, Spring 2021
|
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.
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.
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.
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.
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.