Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

  1. 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
  2. The DrJava ("XXX.drjava") file, which holds the information about what files are in the project and other project-related data.

...

  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 MarkupProject 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"

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.

...

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);
}

...

  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. .

...