Assignment 11, due Apr 21

Solutions

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

On every assignment, write your name and section number. Write your name as it appears on your university ID card! Use the section number as it appears in your current registration! We will not do detective work to figure out who did what. Homework is due on paper at the start of your discussion section. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God"). Never push homework under someone's door!

  1. Background: The assignment for MP5 created two changes in the behavior of the logic simulator: First, the output was changed from a textual listing of events to a graph of the output values of gates. Second, instead of stopping when the logic circuit reached a steady state, the program went into an infinite loop.

    A problem: Explain why it went into an infinite loop. (0.5 point)

    The code in Simulation.run() terminates when the event set is empty. The code that the assignment for MP5 said to add to class TernaryLogic creates a logical process that is a sequence of printGates() events, where each one schedules a new one. As a result, the event set will never be empty and the simulation will not terminate.

    Some students omitted what they perhaps thought was obvious, that the original program terminated when the event set was empty. Failing to state this makes for an inadequate answer because the mere fact that there is an unending logical process does not imply infinite simulation -- after all, there are other ways the simulation could have been terminated.

  2. Background: On the exam, several people suggested declaring a method to be abstract final.

    A problem: Explain the problem this creates. (0.5 point)

    Declaring a method to be abstract-final in some class first requires all subclasses of that class to provide implementations of that method and forbids any subclass from doing so.

    Several students merely explained what an abstract method is without ever explaining the contradiction.

  3. Background: A mathematician might define the successor function as succ(i) = i + 1. In Java, this function would have to be a method. The abstract definition of this method would be something like:
    static abstract int succ( int i );
    

    a) Give Java code that implements this function. Assume this is in a context where it can be done. (0.5 point)

    int succ( int i ) {
            return i + 1;
    }
    

    b) Give Java code that evaluates this function. Assume this is in a context where it can be done. (0.5 point)

    j = succ( k );
    

    Some asked: Why ask such a trivial questions? On the midterm, it appears that some students had vocabulary problems with the words implement and evaluate.

  4. Background: Look at class Simulation from the posted solution to MP4.

    A problem: List (in the order they occur in the code) all of the declarations in this code that can be marked as final. (1.0 points)

    private static class Event {
    	final float time;
    	final Action act;
    	Event( final float t, final Action a ) {
    private static final PriorityQueue  eventSet =
    public static final void schedule( final float time, final Action act ) {
    public static final void run() {
    

    Some students missed some of these, but the omissions didn't follow a pattern.