Assignment 8

Due Oct 15, on line

Part of the homework for CS:2820, Fall 2020
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Simple multiple choice questions, 1 point each:

  1. Here is a bit of Java code:
    Thing t = new Thing();
    t.method( (int i)-> i + 1 );

    Here is a skeleton for class Thing:

    class Thing {
        public interface Mystery {
        public void method( Mystery param ) {
            // body of method omitted

    Which text below would work where ????? is given above:
    a) void method( int i );
    b) int method( int i );
    c) abstract int method( int i );
    d) abstract void method( int i );
    e) private int method( int i );

  2. Consider the final simulation framework given at the end of the notes for Oct. 13. Suppose the following code is used in the initialization:
    Simulator.schedule( 8.0*hour, (double t)-> );
    Assume that w is a workplace, and every workplace has a method open(double t) that simulates the workplace opening for business at time t.

    The value 8.0*hour is passed as t to through the following steps that are not given in order (and some steps are omitted):
    v) e is removed from eventSet.
    w) 8.0*hour is put in e.time for some Event e.
    x) is called.
    y) e.act.trigger(e.time) is called.
    z) e drifts to the head of eventSet.

    What is the correct order for these steps?
    a) vwyzx
    b) xwyvz
    c) ywzvx
    d) wzvyx
    e) zywvx

  3. Assume that time zero is midnight on day zero of the simulation, so 8*hour is 8AM on day zero. Given that w is a workplace with methods open(t) and close(t), and given that the work day is 8 hours long, a workplace that opens at 8AM should close at 5PM. This requires the following code:
    public void open(double time) {
        Simulator.schedule( time + 8*hour, ------- );
    What code belongs where the above shows -------?
    a) ()-> open( time )
    b) (double t)-> open( t )
    c) time )     open should be close in all of these.
    d) ()-> time )
    e) (double t)-> t )

  4. Given that we are using type double to represent time, we could define day = 1.0 so that hours are fractional days. If we do this, we should define hour as:
    a) final double hour = 0.4166;
    b) double hour = 1 / 24;
    d) final double hour = day * 0.4166;    0.4166 should be 0.04166 in all of these.
    c) double hour = day * 0.4166;
    e) final double hour = day / 24.0;

  5. Suppose we want to model weekends? To do this, when a workplace closes at 5PM on a normal weekday, it schedules itself to reopen 16 hours later at 8AM, but on Friday afternoon, it should schedule itself to open 54 hours later, that is, skipping an extra 48 hours. Assume that day zero begins at the midnight separating Sunday from Monday. What expression below is true only on Friday?

    a) (Double.intValue( time ) % 7) == 4
    b) ( time % 7) == 4
    c) (Double.intValue( time ) % 7) == 5
    d) ( time % 7) == 5
    e) (Double.intValue( time ) / 7) == 0.5

Machine Problem 6 -- due Monday, Oct 19

a) Add class Simulatior to your epidemic simulator.

b) Add a new input line to your model description file that specifies the end of time. For example, to simulate 10 days, you would say:

end 10.0;

c) At the beginning of time, you should schedule the end of time. Initially, since we have nothing special to do, just schedule a call to System.exit(0) at that time.

d) For each workplace, schedule it to open for the first time at 8:00 AM on day zero.

e) When a workplace is opened, it should print a debugging message saying Workplace XXX@XXX open at time XXX, and then it should schedule itself to close 8 hours later.

e) When a workplace is closed, it should print a debugging message saying Workplace XXX@XXX closed at time XXX, and then it should schedule itself to re-open 16 hours later. Ignore weekends!

Time output can be raw floating point numbers, no need to convert to days, hours, minutes and seconds. This is still debug output. It is worth asking, where should the constants for time units day hour and minute be declared? Your choice, but be sensible!

A student asked: Does that mean that the end time, on the input, is specified in days?

Yes. If the input file says end 7.5 that means 7 days and 12 hours.

A student asked: If hour is defined as day/24 and day=Double.intValue(time) does that mean that hour is not constant?

You are confusing day as a time unit, where it must be constant, with the day number, counting from the start of time, which changes. In software, the same identifier can have different meanings in different contexts.

A student asked: How do you schedule something to happen at a particular time of day.

These two examples illustrate scheduling an event at an absolute and a relative time:

Simulator.schedule( AbsoluteTime, (double t) -> SomeThing(t) );
Simulator.schedule( time + RelativeTime, (double t) -> SomeThing(t) );
You can seem several examples of this in the code distributed with the Oct. 15 lecture. The end-of-time is scheduled there as an absolute time, and within the constructors for StopLight and Source intersections, the initial light change or vehicle production event is absolute. Later events for each StopLight or Source intersection are scheduled relative to the previous events.

For the problem at hand, the first opening of a business would be scheduled at an absolute time, 8AM on day zero, while all later openings and closings of that business would be scheduled at relative times, relative to the previous opening or closing.