CS334 Assignment 7


Assignment 7 ----------------------------- Due 4/29/97

  1. When overriding a method's definition in a subclass, why must the type of the new method fit in some way with the type of the method being overridden? Give an example of a class and a subclass in which a method is overridden by one with a new type which will cause type-checking of a (different) inherited method to fail.

  2. a. Suppose class B has been declared to inherit from class A in Eiffel, and thus B is a subclass of A. Suppose we include the following code in a method:
    	errormethod is
    		local 	a : A;
    					b : B
    		do .....
    			a := b; ....;
    			b := a; ....
    Suppose B really behaves as a subtype of A (i.e. all contexts which require an object of class A will work fine when provided with an object of class B). Which of these assignments is always guaranteed to be type-safe? Give an example of code which would break if the other assignment were allowed. (Hint: Consider accessing a method which is defined in B, but not in A. )

    b. Suppose Eiffel allowed assignments to formal parameters in methods (i.e., Eiffel essentially used call-by-reference). Suppose we have classes A and B as above in which B is a subclass of A (and a subtype). Give an example in which it could break the type system if an actual parameter of class B were passed in for a formal parameter of class A.

    c. In Java, if B is a subtype of A then Array of B is considered to be a subtype of Array of A. Please explain why this is not type-safe. (Ignore problems with subscripts, you may assume that both arrays have exactly the same set of legal subscripts.) Note that Java makes up for this flaw in the static type rules by putting in a dynamic check to avoid errors.

  3. Please write and turn in a program in Eiffel to read in 10 integers and print the average. (Important note: Ebench seems to be incredibly touchy about taking input from a window. Points to be aware of:

    i. Launch ebench without "&".

    ii. When you start execution (by clicking on the running person), move the mouse immediately to the window which launched ebench. If you get there too late the system will halt with an "Interrupted system call" message. You then must start execution one more time to get it to actually halt. If you move the mouse outside of the window, at any point during execution you will get similar behavior (and it may store up key strokes to use in a later run of the program!).

    iii. If you find this too annoying, you can execute your program from outside of ebench. After you have melted the changes in your program, go to the directory where the appropriate Ace for the program is kept. Go into the subdirectory EIFGEN/W_code and you will find a file with the same name as the system name you put in the Ace file. Just type that name to begin executing the program. Warning: It will still crash if you move the mouse out of the window!)

  4. In my directory ~kim/cs334stuff/Eiffel/binarytree you will find an implementation of a binary tree as well as some supporting classes. The file btreedriver.e is the main program class which exercises a binary tree of strings. Please run the program, study the code, and then answer the following questions about this program.

    a. The class NODE represents a node of the binary tree. It inherits from the library class LINKABLE. You will notice that if you look at the "shortflat" version of NODE, you will see that it has features value, left, right, put_left, put_right, change_value, node_action, etc. The type of left and right is "like Current". What does this mean? In particular, if I create an object from class NODE, what type will left and right actually be?

    b. The class BSCHNODE is defined as a subclass of NODE. if I create an object from class BSCHNODE , what type will left and right actually be?

    c. The header of BSCHNODE includes [T -> COMPARABLE]. What does this mean?

    d. Look at the class BINARYTREE. TreeRoot holds the node which is at the root of the tree, while special always refers to a node of the tree. The value of special is changed by executing the method find which takes a parameter rel which will have one of the values root, leftChild, or rightChild and will result in moving special either to the root, to the left subchild or to the rightsubchild. The other operations should be rather self-explanatory. How is an empty binary tree represented?

    e. Please write a subclass of BINARYTREE which represents a binary search tree and supports the methods bschfind(key:T) and bschinsert(key:T) which do the appropriate operations on a binary searchtree. Note that you should use BSCHNODE rather than NODE as the type of TreeRoot, and will have to constrain the type T in this new subclass. (Notice the other declarations in BinaryTree which are of the form "like treeRoot".) Be sure to hide those methods from the superclasses which should no longer be available to clients. Test your program by writing a program similar to my btreedriver. (Feel free to steal as much as you want from my program.)