Assignment 12Due Apr. 21, on line
Part of
the homework for CS:2820, Spring 2021
|
In the solutions to MP8 and MP9 the act and time fields of events were declared to be final. Therefore, they cannot be changed, so a) and b) must be wrong. Besides, changing the time field of an item in the pending-event set would violate a fundamental requirement of all priority queue and heap implementations.
The λ expression is used to creat an Action which is stored in the act field of an event, so answer c) is really just a variation on answer a) and it is equally wrong.
Answer e) is nonsense. Cancelling an event cannot not delete any objects aside from the object representing the event itself. If the "get sick" event for a person is to be cancelled, that does not require that we eliminate the person.
Answer d) is correct. Look at the code for Person.infect, which begins with this if statement:
if ( (diseaseState == DiseaseStates.uninfected) // no reinfection && (infectMeTime == now) // if not rescheduled
The variable that is changed in order to cancel or reschedule an infection is infectMeTime.
If class A depends on class B, then either they are in the same layer or A is in a layer above B.
It follows that if there is a cycle, the classes in the cycle must all be in the same layer, so b) is correct.
Answer d) is utter nonsenxe (it refers to a completely different use of the word graph.
Answers a), c) and e) all refer to kinds of acyclic graphs, from which no hard conclusions may be drawn about layering. If the graph is acyclic, it may be that each class is in a separate layer.
plotdata.csv: epidemic.class model java epidemic model > plotdata.csv plot: gnuplot plotfile
There are some missing make dependencies in the above:
a) plot depends on plotdata.csv
b) plot depends on epidemic.class
c) plot depends on model and plotdata.csv
d) plot depends on epidemic.class and plotfile
e) plot depends on plotdata.csv and plotfile
— correct.
plot obviously depends on plotfile since that is an argument to the gnuplot shell command. What else does it depend on? The descriptive text explained that plotfile "explains how to read plotdata.csv." Therefore, plot also depends on plotdata.csv. In sum, e) is correct and a) is incorrect because it only partially lists the dependencies.
plot only depends on epidemic.class and model.class indirectly, through plotdata.csv. In general, makefiles should not list such indirect dependencies. The make utility automatically infers them from the direct dependencies that are given. This rules out b), c) and d).
Simulator.schedule( time + delay, (double t)-> go( time + delay ) );
a) The λ expression cached time+delay,
while reschedule() changed t.
— correct.
b) Rescheduling changed time but not delay.
c) All λ parameters must be final or effectively final.
d) go() needs to be rewritten to allow rescheduling.
e) We needed to get rid of the common subexpression by doing
td=time+delay first.
As we have demonstrated a month or more ago, the code given above really compiles to something like this:
Simulator.schedule( time + delay, new SomeKindOfAction( time, delay ) );
Where SomeKindOfAction implements Event and the parameter initializes cached copies of the effectively final non-local variables found in the body of the λ expression. The trigger method of SomeKindOfAction will call go(cachedTime+cachedDelay).
Given this, it should be clear that b) is wrong because nothing done in reschedule() can change the cached copy of Time. Answer e) is wrong for similar reasons, no rewrite of time+delay will eliminate the cached copy.
Rewriting go() cannot help, since that is outside the control of class Simulation. Rewriting the call to go does help, but that is not what answer d) said – to most programmers, "rewriting go()" suggests a rewrite to the code of the method itself, not a rewrite to the call.
Answer c) is only tangentially relevant and it is also false. The correct statement would be "all variables referenced in a λ expression other than the λ parameters must be final or effectively final."
Answer a) is the closest to correct because time and delay are indeed cached, and because reschedule() can change the lambda parametere t but has no effect on the cached values.
Public Static Class A {} Private Static Class AA extends A { public Thing t; }
Given an instance of AA called aa inside a method of
class X how can the code return
class aa to a user of
X in a form that is encapsulated?
a) return (A)aa; from code declared to return an AA
b) return new A(aa); from code declared to return an A
c) return aa; from code declared to return an A
— correct.
d) return aa; from code declared to return an AA
e) return new A(); from code declared to return an A
The method cannot return an AA because class AA is unknown to the world outside X and therefore if the method returned an AA, the method itself would be effectively invisible outside X even if it was public. Therefore a) and d) are wrong.
Class A does not have a constructor with a parameter, so b) is wrong. Returning a new A cannot be right, because that new A carries no information at all, so e) is wrong.
Therefore, c) is the only answer we have not eliminated. Is it right? Yes, because aa is a legal value to return in any context where a member of class AA is allowed, and that includes places where all parent classes of AA are expected, including class A.