Assignment 4

Due Feb 17 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. On MP2, you probably called the constructor new Role( ... ). Why was it necessary in MP2 to create some kind of list or collection of all Roles?

    a) So we can look up roles by name.
    b) So we do not forget any roles after their populations are created.
    c) So we can create the people for each role after we know all roles. — correct
    d) So we can find all the people associated with each role.
    e) all of the above.

    It is not necessary to look up roles by name for MP2, although MP3 will need this, so a) is a poor fit. Roles will not be forgotten if each person has a link to the associated role, so b) is wrong. Nothing in MP2, or even in MP3 requires searching for people by role, so d) is wrong. The math for determining how many people to create for each role requires knowing the sum of the numbers for all roles, so it is impossible to create the population for any role before all are known. This forces listing all the roles, making c) correct.

    Note that one issue was not listed above that also justifies keeping a list of all roles: Such a list is needed to detect redefinition of a role, or put another way, to detect two roles with the same name.

  2. Our input languages for the road network and epidemic could have required that each item be listed on a line by itself. Instead of requiring one item per line, we opted to use a semicolon to mark the end of each item. We did this because:

    a) Semicolons make more sense as terminators than periods.
    b) Class Scanner offers no help finding line boundaries.
    c) Class Scanner mostly treats line boundaries like space or tab. — correct
    d) The choice was entirely arbitrary.
    e) Java offers no tools for working input one line at a time.

    Only programmers seem to think semicolons are a sensible way to terminate something, so a) is wrong. Class scanner does offer findInLine, hasNextLine and nextLine methods, so b) is false. Option c) is certainly true — most of the methods of class Scanner treat newlines like space and tab by default. Option d) is wrong, because the decision was made only after looking at the tools provided by class Scanner and its competitors. Finally, option e) is wrong, among other things, because of the reasons offered to show that b) is wrong.

  3. Why can't we use the waterfall model to develop our epidemic simulator?

    a) The epidemic simulator is too big for the Waterfall model.
    b) The waterfall model only applies to government projects.
    c) Waterfall specifications take the form of screen shots.
    d) The waterfall model does not work with object-oriented code.
    e) The waterfall model needs a complete specification to start with. — correct

    The waterfall model works with any size problem (the GPS satellite system discussed in class is a huge example problem), so a) is wrong. Nothing about the waterfall model requires that it apply to government, so b) is wrong. Nothing about the waterfall model require the use of screen shots (and the GPS example never mentioned this), so c) is wrong. Nothing about the waterfall model cares about the programming language being used, so d) is wrong. This leaves e). The most elementary discussion of the waterfall model always begins with a complete specificaiton before any work on coding begins.

  4. Suppose Java did not have constructors, so new X() always created a new object of class X with only default initialization for the instance variable of X. If you wanted a parameterless constructor that did something more complicated, but you were stuck with this limited version of Java, you could rewrite your constructor as a method with the following header:

    a) void newX()
    b) X newX()
    c) static void newX()
    d) static X newX() — correct
    e) all of the above could be made to work

    This question is purely about Java, and drove many students to look at the textbook. If we replace a call to a complex constructor new X() with X.newX(), then the newX method must return an object of class X, making a) and c) wrong. If the method call is replacing a constructor call, it must not need some object x of class X to be called, as in x.newX(), so b) is wrong.

  5. In object-oriented programming languages, the call o.m(x) calls method m passing parameters (x), where m must be a method defined for class c, assuming that o is an instance of c.

    a) m must not be static. — correct (see note)
    b) class c must be public.
    c) method m must not be defined in a superclass of c.
    d) o must not be a static variable.
    e) x must not be a private variable.

    This question is also purely about Java, and drove many students to look at the textbook. First, a) is trivially true (see note). If class c is not public, the call in question would still be legal within the body of class c or within other classes defined in the same source file, so b) is wrong. If class c inherits m from a superclass, that makes no difference so c) is wrong. So long as o is visible in the context where o.m(x) is used, the fact that o is static, an instance variable, a local variable or a parameter is irrelevant, so d) is wrong. Similarly, so long as x is visible where used, the fact that it is public or private is irrelevant, so e) is wrong.

    Note: As it turns out, Java does allow o.m(x) when m is a static method. I did not know this little obscurity of Java until after this was graded, and even knowing it, it's not easy to figure out why it's legal, since calling o.m(x) for a static method cannot have an effect that depends on or makes any changes to o, so such method calls are going to be very rare in practice.

    So how do you deal with questions like this? The categories true and false are very binary, but English has gradiations of meaning. All 5 options have the word "must" in them. If you shift "must" to "should," replacing absolute prohibition with recommendation, a) becomes true while the others remain nonsense. There are perfectly reasonable reasons for violating the b) through e) in recommended form, while a) in recommended form is a quite reasonable recommendation.