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

Answer:

Depends on the application. But to be useful, a Tree should at least have getters.


Setters and Getters

Here is a Tree with getter methods filled in.

// Tree.java
//
public class Tree
{
  // instance variables
  private double x, y, z;
  private Cone branches;
  private Cylinder trunk;  
  
  // constructor
  public Tree( double trHeight, double trRad, double brHeight, double brRad, double x, double y, double z)
  {
    trunk = new Cylinder( trHeight, trRad );
    branches = new Cone( brHeight, brRad );
    this.x = x; this.y = y; this.z = z;
  }
  
  // methods
  public String toString()
  {
    double totalHeight = branches.getHeight() + trunk.getHeight();
    double width = branches.getRadius();
    return "Tree. Height: "  + totalHeight + ", width: " + width + 
           ", area: " + area() + ", volume: " + volume() ;
  }
  
  // more methods
  public double volume()
  {
   // return the sum of two volumes
   return trunk.volume() + branches.volume();
  }

  public double area()
  {
   // return the sum of two areas
   // minus twice the area of the trunk's circular top
   double total = trunk.area() + branches.area();
   double rad = branches.getRadius();
   double circle = Math.PI*rad*rad;
   return total - 2*circle;
  }

  public void grow( double rate )
  {
   // increase all dimensions by rate
   double bHeight = branches.getHeight();
   branches.setHeight( bHeight*(1.0+rate) );
   
   double bRadius = branches.getRadius();
   branches.setRadius( bRadius*(1.0+rate) );

   double tHeight = trunk.getHeight();
   trunk.setHeight( tHeight*(1.0+rate) );
   
   double tRadius = trunk.getRadius();
   trunk.setRadius( tRadius*(1.0+rate) );
  }
  
  public double getX()
  {
    return x;
  }  
  
  public double getY()
  {
    return y;
  }  
  
  public double getZ()
  {
    return z;
  }

  public Cone getBranches()
  {
    return branches;
  }  

  public Cylinder getTrunk()
  {
    return trunk;
  }  
} 

QUESTION 12:

How many trees are there in a Forest?


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