Machine Problem 12

Due Apr 26, on line

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


The posted solutions to MP8 through MP10 all require that previously scheduled events be rescheduled, but their pending-event-set implementations do not support rescheduling. The code that needs this is in class Person and all of it involves a private variable called infectMeTime.

The code for the road network simulator distributed with the notes for the lectures on April 12, 16 and 19 all include compatible versions of class Simulator that include new methods to cancel or reschedule previously scheduled events. These differ only in the extent to which the prevent users from abusing fields of class Event that should not be accessible to users. Correct code running under any of these will work identically under the others, with only small changes in performance.


Make a minimal rewrite of class Person that eliminates use of infectMeTime and instead uses event cancellation and or rescheduling to accomplish the same thing. To test this code, you will have to use an augmented version of class Simulator that supports these operations.

Use the newest version of Person and Simulator as your starting points.

The tricky part of this involves the fact that sometimes, you may need to make a choice between rescheduling an event or cancelling it. What happens when the value of InfectMeTime becomes infinite because the number of contageous people goes to zero. Since InfectMeTime and all other times in the simulator are of type double, division by zero does not throw an exception, it simply returns positive or negative infinity. These sort correcty relative to finite values, but you might want to simply cancel events instead of scheduling them infinitely far in the future. This can have a real impact on the efficiency of the simulator.

Simulation Behavior

This assignment does not involve any changes to the behavior of the simulation. It is all about making the code run a little faster and eliminating some rather clunky kluges.


You should submit just the file; we will test this in the context of the remainder of the pieces of the posted solution to MP11.

As usual, submit using the ~dwjones/submit utility.

Questions and Answers

A student asked: You have mentioned efficiency several times this semester. How do we measure how fast our code is, if we are curious?

You can use the time shell command. Here is an example:

[dwjones@fastx02 ~/epidemic]$ time java Epidemic testa
  ... lots of deleted lines ...
0.382u 0.108s 0:00.22 218.1%	0+0k 8+64io 8pf+0w

The time shell command takes, as a command line argument, a shell command. It runs that command and then outputs a report of the time and other resources used in running that command. The report breaks down as follows:

Starting a Java program has a high cost, since the J-code interpreter must find, read and link all the .class files before any user code runs. Even the smallest Java test programs like the famous take about 0.2 seconds of user time.