No. The statement could be written as follows. The toString()
method will automatically be called:
if ( tree0 != null ) str += "Tree 0: " + tree0 + "\n";
A Forest
object might need an area()
method.
This might be useful if you were modeling a real forest
and needed to know how much CO2 the forest was absorbing.
Here is the class, with more blanks:
// Forest.java // public class Forest { // instance variables private Tree tree0=null, tree1=null, tree2=null, tree3=null; // methods public void setTree( int treeNum, Tree tree ) public Tree getTree( int treeNum ) public String toString() public double area() { } public double volume() { } }
Fill in the methods. Beware: some of the Tree
reference variables might be null
.