Assignment 8, due Mar 15

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

Assignments are to be turned in on paper. On every assignment, write your name, course and section number at the top of each page. Write your name as it appears in your university records! Use the section number for which you are registered! We will not do detective work to figure out who did what. Work must be legible.

Homework is due on paper in discussion section, usually at the start except when the assignment indicates that time to work on the assignment will be provided in class. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God" outside your control). Never push homework under someone's door!

  1. Background: Consider this bit of code that uses class Simulation distributed with the notes for Lecture 22:
    static void tortise( float time, float delay ) {
        Simulation.schedule( time + delay, (float t) -> tortise( t, delay/2 ) );
    }
    

    Assume that the simulation is started with this code:

    Simulation.schedule( 0.0F, (float t) -> tortise( t, 1.0F ) );
    Simulation.run();
    

    a) The code for tortise includes code to call tortise. Explain why, nonetheless, tortise is not recursive. (0.5 points)

    b) The simulation program will never terminate, but How long will the simulation run, in simulated time? (0.5 points)

  2. Background: In lecture Monday, two alternative were given for the call to enter a road at the current time within the body of the method that creates a new vehicle in a Source intersection.

    One of these schedules a new event at the current time using our simulation framework, the other just does the job, ignoring the simulation framework. This line of code (either version) was followed by code that could schedule other events and change some of the state variables of the simulation, depending on the values of those variables.

    a) What is the difference between these, in terms of the order in which different bits of code are executed. (0.5 points)

    b) Why is is that (in the example given) either version of the code will work? (Hint: Think about how pickOutgoing() or enter() could interact with other code, and explain why they are unlikely to do so.) (0.5 points)

  3. Background! Consider this bit of code, using class java.util.Random:
    Random r = new Random();
    while (true) {
        int a = new Random().nextInt();
        int b = new Random(1).nextInt();
        int c = r.nextInt();
        System.out.println( "" + a +"\t"+ b +"\t"+ c );
    }
    

    a) Which sequence of values (a, b or c) is actually a correct use of Random (only one is). (0.5 points)

    b) Explain why one sequence is completely and obviously non-random. (0.5 points)