Assignment 11, due Apr 22

Solutions

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

  1. Background: Problem 4 on exam 2 asked you to implement a subclass of LinkedList that added one new method finalize(). By default, objects of this class behave exactly like objects of class LinkedList. Once finalized, an object of this new class is fixed, in the sense that the order and identities of the list elements cannot be changed. Here, let's call the new class FinalizableList.

    Assume for the purpose of this problem that class LinkedList has only the following methods: get(int index), set(int index,E element), push(E element), size(E element), clear(), plus one initializer, LinkedList(). files as a starting point for your solution to MP5.

    A problem: Give a complete and syntactically correct Java code for class FinalizableList. No comments, but make it readable. (1.0 points)

    import java.util.LinkedList;
    import java.lang.UnsupportedOperationException;
    
    class FinalizableList  extends LinkedList  {
        boolean finalized = false;
        public void finalize() {
            finalized = true;
        };
        public E set( int index, E element ) throws UnsupportedOperationException {
            if (finalized) throw new UnsupportedOperationException();
            return super.set( index, element );
        }
        public void push( E element ) throws UnsupportedOperationException {
            if (finalized) throw new UnsupportedOperationException();
            super.push( element );
        }
        public void clear() throws UnsupportedOperationException {
            if (finalized) throw new UnsupportedOperationException();
            super.clear();
        }
    }
    

    The problem statement did not specify what to do if a user tries to change a finalized list. The above code throws an exception, the particular exception thrown seems appropriate (if verbose), but given the problem statement, turning forbidden operations into no-ops or throwing some other reasonable exception would make sense.

  2. Background: Focus just on the set() method above. This has two parameters, but it is possible to redesign the class interface to support a programming style that used just one parameter per method call.

    A problem: Give Java code that you could add to your class defined above to define a pair of new methods, setI(int index), and setE(E element), such that a call to o.set(i,e) can be replaced with o.setI(i);o.setE(e). If you have to declare new fields of class FinalizableList to do this, feel free to declare them as needed. (1.0 point)

        private int index;
        public void setI( int i ) {
            index = i;
        }
        public E setE( E element ) throws UnsupportedOperationException {
            if (finalized) throw new UnsupportedOperationException();
            return super.set( index, element );
        }
    

  3. Background: In class on Monday April 18, we discussed an alternative simulation framework and gave an example, converting class Road to use the new framework.

    A problem: Rewrite the lightChanges method from Exam 2 problem 3 to use this new framework. (1.0 points)

    private void lightChanges( float t ) {
        direction = (direction + 1) % incoming.size();
        Road r = incoming.get( direction );
        for (int i = 1; i <= r.waiting; i++) this.pickRoad().entryEvent( t );
        r.waiting = 0;
    
        class LightChangesEv extends Simultion.Event {
            void trigger() {
                StopLight.this.lightChanges( time )
            }
        }
    
        LightChangesEv e = new LightChangesEv;
        e.time = t + interval;
        Simulator.schedule( e );
    }