Tutorial 5: Exam 1 Review


Introduction

This tutorial serves as a review to exam #1.  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.


Study the code and documentation for the linear recursive structure LRStruct.
1.  Explain what the following visitor is supposed to do when executed.

package lrs.visitor;

import lrs.LRStruct;
import lrs.IAlgo;
public class Mystery implements IAlgo
{
    public final static Mystery Singleton = new Mystery ();

    private Mystery()
    {
    }

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

    public Object nonNullCase(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 GetMin to find and return the minimum Integer object in the host.

3. Assume a LRStruct host contains Integer objects.  Write a visitor called RemMin to find, remove, and returns the minimum Integer object in the host.

4. 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 it.

5. 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 inner class as a helper visitor, or use a helper visitor that takes a LRStruct as an argument in the constructor.

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

7. Add the toString() method to LRStruct to display it as a Scheme list.

8. Write a simple GUI application using the MVC pattern to manipulate and display an LRStruct as follows.

 

D. X. Nguyen, Feb. 20, 2000
dxnguyen@cs.rice.edu