Title: How do visitors work
1How do visitors work?
- This set of slides traces through the execution
of a visitor weve seen before (the
lengthVisitor) on a short list. - It shows how the runtime stack makes recursion
possible. - First lets recall
- how the visitor is defined
- how execute is defined in the two list state
classes
2Length visitor
- in the empty case the answer is zero
- in the non-empty case the answer is one more than
the length of the rest of the list - public Object emptyCase(LRS host, Object _)
- return new Integer(0)
-
- public Object nonEmptyCase(LRS host, Object _)
- Integer lengthOfRest(Integer)(host.getRest().ex
ecute(this,_)) - return new Integer( 1 lengthOfRest.intValue()
)
3execute definition in states
- Empty state
- public Object execute(LRS host, IAlgo visitor,
Object input) - return visitor.emptyCase(host, input)
-
- NonEmpty state
- public Object execute(LRS host, IAlgo visitor,
Object input) - return visitor.nonEmptyCase(host, input)
-
4Now lets trace execution of the visitor
- LRS list new LRS()
- list.insertFront(Wilma).insertFront(Fred)
- IAlgo visitor new LengthVisitor()
- Integer len (Integer) list.execute(visitor,null)
- int length len.intValue()
- System.out.println(The length is length)
- Focus on the evaluation of
- list.execute(visitor,null)
5Tracing the execution of a visitor
list
visitor
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
6Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
7Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
8Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
_state
this
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
_state
9Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
_state
10Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
11Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
12Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
_state
13Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
14Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
15Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
Polymorphic method call
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
16Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
17Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
host
0
_ (null)
this
18Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
algo
input (null)
owner
this
0
19Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
algo
_state
inp (null)
this
0
20Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
0
21Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
host
_ (null)
lengthOfRest
this
_state
1
0
22Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
algo
input (null)
owner
Wilma
this
_datum _rest
_state
1
0
23Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
algo
inp (null)
this
_state
Wilma
_datum _rest
_state
1
0
24Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
_state
1
0
25Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
this
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
host
Fred
_ (null)
lengthOfRest
_datum _rest
this
_state
Wilma
_datum _rest
2
_state
1
0
26Tracing the execution of a visitor
list
visitor
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
algo
input (null)
owner
_state
this
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
2
_state
1
0
27Tracing the execution of a visitor
list
visitor
?space for return value
algo
inp (null)
this
public Object emptyCase() public Object
nonEmptyCase()
_state
Driver Integer len (Integer)
list.execute(visitor, null) LRS public Object
execute(IAlgo algo, Object inp) return
_state.execute(algo, inp, this) NonEmptyState
public Object execute(IAlgo algo, Object input,
LRS owner) return algo.nonEmptyCase(owne
r, input) EmptyState public Object
execute(IAlgo algo, Object input, LRS owner)
return algo.emptyCase(owner,
input) LengthVisitor public Object
nonEmptyCase(LRS host, Object _) Integer
lengthOfRest(Integer)(host.getRest().execute(this
,_)) return new Integer( 1
lengthOfRest.intValue() ) public Object
emptyCase(LRS host, Object _) return new
Integer(0)
Fred
_datum _rest
_state
Wilma
_datum _rest
2
_state
1
0
28Thats it!
29Exercise
- Assume you are given an LRS containing Integer
objects (all non-null). Write a visitor which
traverses the list structure and calculates the
sum of all the int values. - Ex If the list contains Integers for 1, 2, 3, 4
and 5, the visitor must return an Integer
containing 15.
30A solution
- public class SumVisitor implements IAlgo
- // The answer in the case of an empty list is
zero - public Object emptyCase(LRS host, Object inp)
- // TODO Auto-generated method stub
- return new Integer(0)
-
- // The answer in the case of a non-empty list
is the sum of the first item - // in the list and the sum of the rest of the
items in the list - public Object nonEmptyCase(LRS host, Object inp)
- // value stored at current position of list
- int currentValue ((Integer)
host.getFirst()).intValue() - // the sum of the rest of the values is
calculated by - // recursive application of the visitor
- int sumOfRest ((Integer) host.getRest().execut
e(this,inp)).intValue()
31Additional exercises
- Write a visitor for an LRS of Integers which
returns the largest Integer. - Write a visitor for an LRS of Integers, which you
can assume appear in order from smallest to
largest, which inserts its input in the correct
(in order) position within the LRS.