CPSC 329 Software Development Fall 2017

CPSC 329 Lab 12: Visitor

This lab gives you some practice working with the Visitor pattern. In particular, you will implement the Visitor pattern for applying operations to an expression tree and consider its pros and cons.

Objectives

Successful completion of this lab means that you can:

Collaboration

Work in pairs (and one group of three) to complete this lab. Only one person needs to carry out the steps in the lab but everyone in the group should make sure they understand what is going on. There is no limit on collaboration with others, but you need to make sure that you understand for yourself how (and why) to do things.

Due Date and Deliverables

due Tue Dec 5 at the start of class

One handin per group.

To hand in the lab, create a handin tag as directed. Make sure the names of your group members are in the commit comment.


Setup

Create a project for this lab:

Provided Code and Files

The provided code is an implementation of an expression tree - internal nodes of the tree contain operators and leaf nodes contain values. (Only the operators + and * are included in the provided code.) ExpressionMain is a simple driver program which illustrates creating a small expression tree, printing it, and evaluating it. Review the code so that you understand how it works.


Tasks

Design Evaluation

Consider the provided code with respect to change:

  1. What modifications (new classes and/or changes to existing classes) would be needed to support additional binary operators (such as subtraction, division, or mod)?

  2. What modifications (new classes and/or changes to existing classes) would be needed to support unary operators (such as -)? (Is it any different from binary operators?)

  3. What modifications (new classes and/or changes to existing classes) would be needed to be able to print the expression using postfix notation (the operator follows the expressions being operated on) or to draw a graphical representation of the expression tree?

Put your answers in a text file called answers.txt in your project. (Make sure it goes at the top level of the project, not in the src directory.) Note that you only need to explain what modifications are needed - you don't need to actually make them.

Visitor

Modify the provided code to implement the print and evaluate functionality using the Visitor pattern instead of special evaluate and toString methods hardcoded into Expression and its implementing classes. First make the Expressions into things that can be visited:

Next, implement the visitors:

Finally, update main:

Test the program - it should do the same thing as before.

Design Evaluation II

Consider the Visitor version of the expression tree with respect to change:

  1. What modifications (new classes and/or changes to existing classes) would be needed to support additional binary operators (such as subtraction, division, or mod)?

  2. What modifications (new classes and/or changes to existing classes) would be needed to support unary operators (such as -)? (Is it any different from binary operators?)

  3. What modifications (new classes and/or changes to existing classes) would be needed to be able to print the expression using postfix notation (the operator follows the expressions being operated on) or to draw a graphical representation of the expression tree?

  4. Compare and contrast the design of the Visitor version with the original design of the provided code. Which design is better for expression trees? Which design is better in general? Explain.

Put your answers in the answers.txt file.

If You Have Time

(This section is optional.)


Handin


Valid HTML 4.01!