Java Reference
In-Depth Information
private
String toString(String indent){
StringBuffer buffer
#
new
StringBuffer();
buffer.append("[").append(label);
if
(!isLeaf()){
buffer.append("\n");
int
i;
for
(Arc current
#
firstArc; current!
#
null
;
current
#
current.nextArc()){
String arcLabel
#
current.label();
Node dest
#
current.destination();
buffer.append(indent)
.append(" (")
.append(arcLabel)
.append(")--> ")
.append(dest.toString(indent
!
indentStep));
}
buffer.append(indent);
}
buffer.append("]\n");
return
buffer.toString();
}
We define a
toString()
method also for class
DecisionTree
; the implementa-
tion is straightforward, since it just invokes the
toString()
method of the root
node.
public
String toString(){
return
"Decision tree: \n"
!
root.toString();
}
4.5.4
Test
The test for the representation of the tree can address two main issues:
the representation of a single node (a leaf) and the representation of a
whole three. We introduce a degree of flexibility by testing the presence of
substrings rather than a perfect match. Actually we test if the represent-
ation contains the labels of the node and arcs in the proper order. This
approach allows us to modify the width of indentation and extra white
spaces.
The first test method (
testSingleNode()
) checks if the representation of a
leaf node contains the label of the node. The second test method
(
testSimpleTree()
) checks if the first label is the label of the root node and if
each node label is preceded by the label of the arc to that node.
public class
TestRepresentation
extends
TestCase {
public
void testSingleNode(){
String label
#
"the label";
Node node
#
new
Node(label);