// © Copyright 1996-1999, Joseph Bergin. All rights reserved. package cs1; public class SumVisitor implements LispwVisitor.Visitor { // Precondition: the list contains Integer objects only and no nulls. // Usage: // SumVisitor visitor = new SumVisitor(); // aLispwVisitor.accept(visitor); // Postcondition: visitor.sum() will be the sum of the elements in the list. // Caution: Intended to be used once. There is no way to reinitialize the count // to zero after usage. public Object apply(Node aList) { sum += ((Integer) aList).intValue(); aList.tail().accept(this); // This is the recursion step. } public Object apply(NIL empty) { // nothing for the nil object } public int sum(){ return sum;} private int sum = 0; }