Professional Documents
Culture Documents
interface IArith {
// to return the result of
applying given visitor to an
IArith
<R> R accept(IArithVisitor<R>
iav);
}
// to represent a constant
class Const implements IArith {
double num;
Const(double num) {
this.num = num;
}
// to return the result of
applying given visitor to a Const
public <R> R
accept(IArithVisitor<R> iav) {
return
iav.visitConst(this);
}
}
// to represent a formula
class Formula implements IArith {
IFunc2<Double, Double,
Double> fun;
String name;//must be "plus",
"div", "mul", "sub"
IArith left;
IArith right;
Formula(IFunc2<Double,
Double, Double> fun, String name,
IArith left, IArith right) {
this.fun = fun;
this.name = name;
this.left = left;
this.right = right;
}
// to return the result of
applying given visitor to a
Formula
public <R> R
accept(IArithVisitor<R> iav) {
return
iav.visitFormula(this);
}
}
// to represent a function
interface IFunc2<A1, A2, R> {
// to return the result of
applying given function
R apply(A1 a1, A2 a2);
}
// to represent function add
class Add implements
IFunc2<Double, Double, Double> {
// returns addition of double
a1 and double a2 as a double
public Double apply(Double
a1, Double a2) {
return a1 + a2;
}
}
// IListVisitor<T, U> operates on
List of T returns type U. U
visitMT(MT<T> n) U
visitCons(Cons<T> n)
interface IArithVisitor<R> {
return answer;
// to filter a list of T
class FilterVisitor<T> implements
IListVisitor<T, IList<T>> {
IPredicate<T> pred; //
predicate to determine what to
filter
FilterVisitor(IPredicate<T>
pred) {
this.pred = pred;
}
// filter empty list
public IList<T> visitMT(MT<T>
t) {
return new MT<T>();
}
// filter non empty list
public IList<T>
visitCons(Cons<T> t) {
if (pred.apply(t.first))
{
return new
Cons<T>(t.first,
t.rest.accept(this));
}
else {
return
t.rest.accept(this);
}
}
}
public String
removeAt(ANode<T> node) {
node.prev.next =
node.next;
node.next.prev =
node.prev;
return "";
}
public ANode<T>
compare(IPred<T> pred) {
return this;
}
}
class Node<T> extends ANode<T> {
T data;
Node(T data) {
super.next = null;
super.prev = null;
this.data = data;
}
Node(T data, ANode<T> next,
ANode<T> prev) {
if (next == null || prev
== null) {
throw new
IllegalArgumentException();
}
super.next = next;
super.prev = prev;
this.data = data;
super.next.prev = this;
super.prev.next = this;
}
}
// goes in Cons and MT, nothing
goes in IList interface
// iterates over the list
public Iterator<T> iterator()
{
return new
IListIterator<T>(this);
}
// iterates through an ILIst,
IList must extend Iterable<T>
class IListIterator<T> implements
Iterator<T> {
IList<T> items;
IListIterator(IList<T> items)
{
this.items = items;
}
// does items have next
public boolean hasNext() {
return
this.items.isCons();
}
// gives the next element in
the list
public T next() {
Cons<T> itemsAsCons =
this.items.asCons();
T answer =
itemsAsCons.first;
this.items =
itemsAsCons.rest;
}
// removes
public void remove() {
throw new
UnsupportedOperationException("Do
n't do this!");
}
}
class FibonacciIterator
implements Iterator<Integer> {
int prevVal = 0;
int curVal = 1;
// There are always more
Fibonacci numbers
boolean hasNext() { return
true; }
// Compute the next Fibonacci
number
Integer next() {
int answer = this.prevVal +
this.curVal;
this.prevVal = this.curVal;
this.curVal = answer;
return answer;
}
public void remove() {
throw new
UnsupportedOperationException("Do
n't do this!");
}
}
for (T item : myList) {
// iterates forward through
myList
...
}
class BreadthFirstIterator<T>
implements Iterator<T> {
Deque<IBinaryTree<T>> worklist;
BreadthFirstIterator(IBinaryTree<
T> source) {
this.worklist = new
Deque<IBinaryTree<T>>();
this.addIfNotLeaf(source);
}
// EFFECT: only adds the given
binary-tree if it's not a leaf
void addIfNotLeaf(IBinaryTree
bt) {
if (bt.isNode()) {
this.worklist.addAtTail(bt);
}
}
public boolean hasNext() {
// we have a next item if the
worklist isn't empty
return this.worklist.size() >
0;
}
public T next() {
// Get (and remove) the first
item on the worklist -// and we know it must be a
BTNode
BTNode<T> node =
this.worklist.removeAtHead().asNo
de();
// Add the children of the
node to the tail of the list
this.addIfNotLeaf(node.left);
this.addIfNotLeaf(node.right);
// return the answer
return node.data;
}
public void remove() {
throw new
UnsupportedOperationException("Do
n't do this!");
}
}