Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. 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.
  2. Click on Project/New
  3. 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).
  4.  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.
    • Wiki Markup
      Project Root = \[project folder\]\src     e.g. "U:\Comp211\Lab09\src"
    • Wiki Markup
      Build Directory =  \[project folder\]\classes   e.g.  "U:\Comp211\Lab09\classes"
    • Wiki Markup
      Working Directory = \[project folder\]\classes   e.g.  "U:\Comp211\Lab09\classes"
  5. Leave the rest of the fields blank for now and click "Ok"

...

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 can code, including the project structure and alternative implementations, can be downloaded hereLab09.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);
}

...

  1. Write a visitor called Length to compute the length of an IntList.
    1. Write a non-tail recursive version (using direct recursion).
    2. Write a tail-recursive version using a helper visitor (in place of a helper method).
  2. Write a visitor called ProdNums that returns the product of the number in the list, using a tail recursive helper visitor.
  3. Write a visitor called Reverse that reverses the list using a tail-recursive helper visitor.
  4. Write a visitor called ListString that uses as tail recursive visitor as done in the method listString.
  5. 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) .

...

  1. )

...

  1. .

...