go to previous page   go to home page   go to next page highlighting

Answer:

When a reference contains null it is not pointing to any object.


Building a Forest

Here is a start on a Forest class:

// Forest.java
//
public class Forest
{
  // instance variables
  private Tree tree0=null, tree1=null, tree2=null, tree3=null;
   
  // default constructor supplied automatically
  
  // methods
  public void setTree( int treeNum, Tree tree )
  {
    if      (  ) tree0 = tree;
    else if (  ) tree1 = tree;
    else if (  ) tree2 = tree;
    else if (  ) tree3 = tree;
  }
  
  public Tree getTree( int treeNum )
  {
    if      (  ) return tree0;
    else if (  ) return tree1;
    else if (  ) return tree2;
    else if (  ) return tree3;
    
    return null;
  }
}  

In this design, a Forest starts with no trees. The setTree() method takes two parameters: a Tree reference and an integer that says which instance variable to set. Here is how the method is used:

Forest forest = new Forest();
Tree tree = new Tree( 1, 4.5, 5, 21.2, 5, 5, 0)

forest.setTree( 0, tree );

All classes have a constructor. If you don't explicitly write a constructor, a default constructor is supplied. Instance variables are created and initialized as declared.


QUESTION 14:

Fill in the blanks.


go to previous page   go to home page   go to next page