Assignment 11

Due Apr. 15, 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. The version of the vehicle simulator distributed on Mar. 31, and on all versions from after that date, the following code appears in class StopLight:
    class LinkedListVehicle extends LinkedList<Vehicle> {}
    private final LinkedListVehicle[] blocked;  // who's waiting for green?

    Class LinkedListVehicle is needed because:
    a) it prevents an over-length line.
    b) arrays of generic (parameterized) classes are not allowed in Java. — correct
    c) final variables can't be of generic (parameterized) classes in Java.
    d) later we will some add methods to class LinkedListVehicle.
    e) the class is needed to create entries in the array blocked.

    Try to compile this:

    private final LinkedList<Vehicle>[] blocked;

    You will get a somewhat cryptic error message, and a bit of web searching will show you that b) is the correct answer. Many of the proposed solutions on the web are quite ugly, involving expensive mechanisms like reflection.

    All the other answers suggested above are simply false or misleading nonsense. While e) may sound vaguely plausible, entries could have been created with new LinkedList<Vehicle>().

  2. The file extension .md is ...
    a) used for MarkDown files. — correct
    b) a required file type for README files.
    c) used for files having holding lists of Medical Doctors.
    d) used for files of Makefile Data.
    e) None of the above.

    Answers c) and d) are nonsense crafted to use the initials M and D. Readme files need not have the extension .md, but if you add that extension, things like web browsers can automatically format them by following the markdown. Therefore, b) is wrong but a) is right.

  3. The makefile distributed on April 9 does not ...
    a) document dependencies between files.
    b) give instructions for how to compile the program.
    c) demonstrate the application.
    d) show how to build a shell archive.
    e) document circular dependencies between files. — correct

    It does document many dependencies between files, it does include rules for compiling the program, it does show how to demonstrate the application, and it does show how to build a shell archive. Therefore, a) through d) are wrong. It does not document circular dependencies between classes Road and Intersection so e) is correct.

  4. Consider this bit of code from one of our simulators:
    Simulator.schedule( time + delay, (double t)-> go( time ) );

    a) This code could lead to events being simulated out of order. — correct
    b) This code does not need to use a lambda expression.
    c) We could have simply called go( time ).
    d) This code completely ignores delay.
    e) This code looks good.

    When the simulator triggers its action at time time+delay, it will call go passing just the value time. If go schedules any events based on this, they may be before other events that have already been simulated. Therefore, a) is correct.

    There is a sense in which b) is correct, since any λ expression can be rewritten using cumbersome code to create an object of an implementation of an interface, but logically, that clumsy code is still passing the same object as the λ expression. To say that c) is true is therefore analogous to saying that while a+b might mean string concatenation, the following does not involve concatenation because there is no use of the concatenate operator:

    new StringBuilder(a).append(b).toString()

    You could argue that d) is correct because delay was omitted in the call to go(time), which you might argue should have been written go(time+delay), but this is wrong on two counts: First, delay was not omitted — it was taken into account in the call to Simulator.schedule. Second, the λ expression go(time+delay) should have been written as go(t). Duplicating the expression time+delay might not cause an error because both values could be "final or effectively final," but it is unsafe programming.

    The answer c) also has a degree of plausibility, but it is also weak because the code so clearly schedules go() to be done at some future time. This code is based on problematic code in several student solutions to the machine problems and is certainly not correct, so e) is also wrong.

  5. In the posted solution to MP9, there is a variable in class Person called infectMeTime.
    a) This avoids the need to repeat time + delay in several places.
    b) This is used to keep statistics on when people get infected.
    c) This is needed so class Simulator can schedule events.
    d) This allows infect events to be effectively cancelled. — correct
    e) This is used to optimize simulator performance.

    Answers a), b) and c) and e) are nonsense. In fact, use of infectMeTime seems to avoid just one repetitionof time+delay, it is not used for any statistics gathering, and it is not needed in scheduling, since the expression time+delay could have been used equally well. It certainly does not optimize performance! In fact, using it to cancel events is a performance problem.

    Answer d) is correct. If an infect event occurs and infectMeTime is not now, that means that the time of infection has been changed since that event was first scheduled, and therefore, nothing should happen.