On this page:
Instructions
Problem 1: Understanding Visitors
Getting started
7.1 Visitors for lists
7.2 Visitors for ancestor trees
Problem 2: The Registrar’s Office
7.1 Data constraints
7.2 Methods and examples to design
8.5

Assignment 7: Visitors and Circular Data

Goals: To practice using the visitor pattern and designing cyclic data.

Instructions

As always, be very careful with your naming conventions. You should not use mutation on the first problem, but the second problem does require mutation.

Due Date: Thursday March 13th at 9:00pm

Problem 1: Understanding Visitors

Getting started

Start a new project, and copy into it the following definitions of IList<T> and its supporting classes.

//represents a generic list interface IList<T> {
 
}
 
//represents an empty generic list class MtList<T> implements IList<T> {
 
}
 
//represents a generic non-empty list class ConsList<T> implements IList<T> {
T first;
IList<T> rest;
 
ConsList(T first, IList<T> rest) {
this.first = first;
this.rest = rest;
}
 
}
7.1 Visitors for lists

Design a visitor for lists:
  • Define an IListVisitor interface. How many type parameters will it need? (Hint: how many type parameters did the IShapeVisitor from class need, compared to how many type parameters IShape itself needed?) What methods will the visitor need? (Hint: what methods did the IShapeVisitor need, and how did that relate to the classes we had that implemented IShape?) Name all your methods visit(...) this is another example of overloading.

  • Define a method ?????? accept(IListVisitor<??????> v) on IList<T>, and implement it on the classes implementing IList<T>. Fill in the question marks with whatever type parameter(s?) are needed.

  • Design a MapVisitor for lists. This visitor should behave like the map method that we designed in class.

  • Design a FoldRVisitor for lists that behaves like the foldr method we designed in class.

  • Design an AppendVisitor for lists which appends a given list to the end of this list (this list is the one that is being visited).

7.2 Visitors for ancestor trees

In the same project, copy the IAT interface and related classes from below into your project.

//represents an ancester tree interface IAT {
 
}
 
//represents an unknown ancestor tree class Unknown implements IAT {
Unknown() { }
}
 
//represents a person in an ancester tree class Person implements IAT {
String name;
int yob;
IAT parent1;
IAT parent2;
 
Person(String name, int yob, IAT parent1, IAT parent2) {
this.name = name;
this.yob = yob;
this.parent1 = parent1;
this.parent2 = parent2;
}
}

Problem 2: The Registrar’s Office

The registrar’s office maintains a great deal of information about classes, instructors, and students. Your task in this problem is to model that data, and implement a few methods on it. We deliberately do not give you the class diagram for this problem: from the description below, you should properly design whatever classes you think are relevant. Please use generic lists (i.e. IList<T>) for this problem.

7.1 Data constraints
7.2 Methods and examples to design