Comp 212 Lab 04: Inner Classes as Visitor Helpers


Introduction

This tutorial consists of exercises on the visitor pattern applied to the immutable list framework and the use of anonymous inner classes as helpers to the visitors.  Most of the exercises are lifted from previous homeworks, lab exercises or examples of list algorithms that do not use inner classes as helpers.

List Algorithms as Visitors

In all of the exercises that follow, first write appropriate JUnit test cases for each of the visitors, and then write the code for the visitors so specified.  The unit test cases are in a sense the specification of the problem to be solved.  Writing the test code before writing the actual code to solve the problem helps you focus more clearly on the problem and identify the issues that need addressing before working on a solution.  As such, you will gain a deeper understanding of the problem at hand.  All the helper visitors are to be written as anonymous inner classes. 

First, click  here to download the code for the list framework.

  1. Write a visitor called ToString to compute as String representation of IList with matching parentheses as in Scheme.  For example, ToString for the list containing 1, 2 ,3, should return (1 2 3), and ToString for the empty list should return ().
  2. Write a visitor called Reverse to compute and return the host in reverse order.
  3. Write a visitor called LastElement to find and return the last element of the host.  Do this by traversing the host only once.
  4. Write a visitor called RemLast to find and return a list that contains the same elements (listed in the same order) as the host without the last element of the host.  Do this by traversing the host only once.
  5. Write a visitor called RemMin to find and return a list that contains the same elements (listed in the same order) as the host without the minimum element of the host.  Assume the host list contains Integers and the minimum is unique.  Do this by traversing the host only once.
  6. Write a visitor called Merge that merges the host list with the input list into a third sorted list.  Assume the host list and the input list contain Integers sorted in ascending order.

Preparing for Exam 1

Exam 1 is scheduled for Wednesday, Feb. 16, 2005 from 7:30 PM to 10 PM.  Next week's lab will focus on helping you prepare for the exam.  If you are done with the above problems, you can take a look at the following exams and start working on them as a way to prepare for this semester's exam.

 


D. X. Nguyen   Last revised February 07, 2005
dxnguyen at rice.edu