Lab 09 – List Visitors
Setting up a DrJava Project
DrJava has the ability to create "projects" which enable you to efficiently manage the large numbers of source and compiled files that a Java application involves. A good project structure separates the source code from the compiled code and enables the user to work open all the required source files at once and to easily add/delete/edit them.
A DrJava project involves several pieces:
- A well-defined directory (folder) structure:
- Project directory, containing the DrJava project file and below it:
- "
src
" -- the "source" directory which holds the "XXX.java
" files - "
classes
" or "bin" directory which holds the compiled "XXX.class
" files
- "
- Project directory, containing the DrJava project file and below it:
- The DrJava ("
XXX.drjava
") file, which holds the information about what files are in the project and other project-related data.
To set up a DrJava project:
- On your hard disk, create a folder to hold your project, e.g. "
Lab09
". It's easiest to build the "src
" and "classes
" subdirectories at this point, or you can wait until step 4 below. - Click on
Project/New
- Browse to the project folder and save your DrJava project file with an appropriate name, e.g. "
Lab09.drjava
" (DrJava will automatically add the file extension). - In the
Project Properties
dialog that comes up, fill out the following fields. You will need to click the "...
" button to browse to and create the directories if they do not already exist.Project Root
=[project folder]\src
e.g. "U:\Comp211\Lab09\src
"Build Directory
=[project folder]\classes
e.g. "U:\Comp211\Lab09\classes
"Working Directory
=[project folder]\classes
e.g. "U:\Comp211\Lab09\classes
"
- Leave the rest of the fields blank for now and click "
Ok
"
To open a project, click on "Project/Open...
" in DrJava and browse to your XXX.drjava
project file. If your computer is set up to do it, you might also be able to automatically open a DrJava project by simply double-clicking the project file.
To turn in your project-based work, simply zip up the entire project folder and submit the zip file.
Limitations of the Interpreter Design Pattern
...
Applied to the list structure, the visitor pattern takes the following coding pattern. The clarity following code is simplified as the Functional Java language level would accept. The Full Java language level code, including the project structure and alternative implementations, can be downloaded here: Lab09.zip
Code Block |
---|
/** * Abstract list structure. */ abstract class IntList { abstract Object accept(IntListVisitor v); ConsIntList cons(int n) { return new ConsIntList(n, this); } } /** * Concrete empty list structure containing nothing. */ class EmptyIntList extends IntList { Object accept(IntListVisitor v) { return v.forEmptyIntList(this); } } /** * Concrete non-empty list structure containing an int, called first, * and a rest, which is a list structure. */ class ConsIntList extends IntList { int first; IntList rest; Object accept(IntListVisitor v) { return v.forConsIntList(this); } } /** * Abstract operation on IntList. */ interface IntListVisitor { Object forEmptyIntList(EmptyIntList host); Object forConsIntList(ConsIntList host); } |
...
- Write a visitor called Length to compute the length of an IntList.
- Write a non-tail recursive version (using direct recursion).
- Write a tail-recursive version using a helper visitor (in place of a helper method).
- Write a visitor called
ProdNums
that returns the product of the number in the list, using a tail recursive helper visitor. - Write a visitor called
Reverse
that reverses the list using a tail-recursive helper visitor. - Write a visitor called
ListString
that uses as tail recursive visitor as done in the method listString. - Write a visitor called
MakePalindrome
that returns a list consisting of the input list and its mirror around the last element, using a (non tail-recursive) helper with an accumulator. For example,(1, 2, 3).accept(MakePalindrome.ONLY)
returns the list(1, 2, 3, 2, 1)
.
...
)
...
- .
...