COMP 212 Lab 3: Visitor Pattern

This tutorial consists of exercises on the visitor pattern discussed in class. It also helps you prepare for the second homework.

Once again, we will be writing code for lists. First, copy all the Java source code from the directory ~comp212/public_html/01-spring/labs/03/. Do not copy the code in the visitor subdirectory. We will use this as the starting point.

Note that all the code is in the packages list and list.visitor. For this latter class, this means that there is a visitor package within the list package. Corresponding to that, there is a visitor directory within the list directory.

Browse the code to get familiar with it, before modifying it in the following exercises.

Exercises:

  1. Write a visitor called Length to compute the length of a list using direct recursion and without using helpers. This visitor corresponds to the method length() of the original version of lists.

  2. Write a visitor called GetMin to find the minimum element of a list, assuming it contains only Integer objects. GetMin should use a helper visitor called HelpGetMin to compute the minimum. This corresponds to the getMin() method shown in class.

  3. Write a visitor called Sum to compute the sum of the elements of a list, assuming AList contains only Integer objects. Write one version using direct recursion, and one version, called GetSum, using a helper visitor called HelpGetSum.

Compare your resulting code with that in the provided visitor subdirectory.