Assignment 11Due Apr. 15, on line
Part of
the homework for CS:2820, Spring 2021
|
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>().
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.
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.
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:
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.
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.