Versions Compared

Key

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

...

  • 1. Create a subdirectory called lab10 for all the files of this lab. Create and save a separate file for each of the following classes and interface.
    Code Block
    /**
     * Abstract list structure.
     */
    public abstract class IntList {
        public abstract Object accept(IntListVisitor v);
        public ConsIntList cons(int n) {
            return new ConsIntList(n, this);
        }
    }
     
    /**
     * Concrete empty list structure containing nothing.
     */
    public class EmptyIntList extends IntList {
        public static final EmptyIntList ONLY = new EmptyIntList();
        private EmptyIntList() {
        }
     
        public 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.
     */
    public class ConsIntList extends IntList {
        private int first;
        private IntList rest;
     
    /* NOTE: Programmer must write constructor code and gettors code in full Java.
     */
        public ConsIntList(int f, IntList r) {
            first = f;
            rest = r;
        }
     
        public int first() {
            return first;
        }
       
        public IntList rest() {
            return rest;
        }
     
        public Object accept(IntListVisitor v) {
            return v.forConsIntList(this);
        }
    }
     
    /**
     * Abstract operation on IntList.
     */
    public interface IntListVisitor {
        public Object forEmptyIntList(EmptyIntList host);
        public Object forConsIntList(ConsIntList host);
    }
    
  • 2. Add the declaration package funList ; to the top of IntList.java . Compile it using DrJava Tools/Compile Current document. You should get an error message saying that you are in the wrong package. Close the file for now.

...

Reopen the file in the funList subdirectory. Now compile again. You should get no error this time.error messages saying  it can't find class IntListVisitor and class ConsIntList this time.  You will need to package all the other classes/intefaces and move them into appropriate subdirectories.

  • 3. Add the package funList ; declaration to the top of EmptyIntList.java , ConstIntList.java , and IntListVisitor.java , and move them into the funList subdirectory. You should be able to compile each file individually. Try it.

Note: if you use the command window to compile with the command javac, you should always compile from your project's main directory. If you compile from within a package subdirectory, it doesn't find all the supporting definitions.

We can't run anything yet, because that's just a piece of the whole program.

  • 3. Add the package funList ; declaration to the top of EmptyIntList.java , ConstIntList.java , and IntListVisitor.java , and move them into the funList subdirectory. You should be able to compile each file individually. Try it.
  • 4. Create and save a JUnit test class called TestEmptyIntList . Do not make TestEmptyIntList.java part of the package. TestEmptyIntList.java does not have a package name, and is thus said to be in the no-name (or default) package.

...

  • 4. Here is the list visitor code. It is not quite compilable on purpose.
    Code Block
    /**
     * Computes the length of the host list using a tail-recursive
     * helper visitor.
     */
    public class GetLength implements IntListVisitor {
        static GetLength ONLY = new GetLength();
        private GetLength() {
        }
       
        /**
         * @return an Integer
         */
        Object forEmptyIntList(EmptyIntList host) {
            return 0;
        }
       
      /**
         * @return an Integer
         */
        Object forConsIntList(ConsIntList host) {
            return host.rest().accept(new GetLengthHelp(1));
        }
    }
     
    class GetLengthHelp implements IntListVisitor {
        int acc;
     
        // need constructor
       
        Object forEmptyIntList(EmptyIntList host) {
            return acc ;
        }
    
       Object forConsIntList(ConsIntList host) {
            return host.rest().accept(new GetLengthHelp(acc + 1));
        }
    }
    

...