Tutorial 5: Exam 1 Review


Introduction

This tutorial serves as a review to exam #1.  It consists of several exercises on AListFW and LRStructure.   It is by no means exhaustive.  You need to also review all the design patterns and their implementations provided to you so far.  You also should review all the labs and all the homework assignments.   All methods must be written without checking for the type nor for the state of objects in questions.  Feel free to write helper methods to help do the job.


1.  Explain what the following visitor on LRStruct is supposed to do when executed.  Draw some pictures to visualize what goes on.

package lrs.visitor;

import lrs.*;

public class Mystery implements IAlgo
{
    public final static Mystery Singleton = new Mystery ();

    private Mystery()
    {
    }

    public Object forEmpty(LRStruct host, Object input)
    {
        return assign (host, (LRStruct)input);
    }

    public Object forNonEmpty(LRStruct host, Object input)
    {
        return assign (host, (LRStruct)input);
    }

    private Object assign (LRStruct lhs, LRStruct rhs)
    {
        lhs.insertFront (null);
        lhs.setRest (rhs);
        lhs.removeFront();
        return null;
    }
}

2. Assume a LRStruct host contains Integer objects.  Write a visitor called RemMin to find, remove, and returns the minimum Integer object in the host.
Formulate an equivalent version of this problem for AListFW and solve it.

3. Assume a LRStruct host contains Integer objects.  Write a visitor called MoveMin2First to find the minimum Integer object in the host, move it to the front of the host, and return this minimum. 
Formulate an equivalent version of this problem for AListFW and solve it.

4. Assume a LRStruct host contains Integer objects.  Write a visitor called SwapMinWithFirst to find the minimum Integer object in the host, swap the content of the first node in the host with it, and return it.  Hint: use a helper visitor that takes a LRStruct as an argument in the constructor. 
Formulate an equivalent version of this problem for AListFW and solve it.

5. Assume a LRStruct host contains Integer objects.  Write a visitor called Product to compute and return the product of the elements in the host.

6. Design question 1: does it make sense to apply the visitor pattern to AWord in the hangman project?  Explain your answer and if yes, decribe your design for AWord in UML diagrams.

7. Design question 2: does it make sense to apply the visitor pattern to WordChar in the hangman project?  Explain your answer and if yes, decribe your design for WordChar in UML diagrams.

D. X. Nguyen, Oct. 1, 2000
dxnguyen@cs.rice.edu