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

Answer:

  // return the sum of two volumes
  public double volume()
  {
    return trunk.volume() + branches.volume();
  }


Area

// Tree.java
//
public class Tree
{
  // instance variables
  private double x, y, z;
  private Cone branches;
  private Cylinder trunk;  
  
  // constructor
  public Tree( double trRad, double trHeight, double brRad,  double brHeight, 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 ;
  }
  
  // return the sum of two volumes
  public double volume()
  {
    return trunk.volume() + branches.volume();
  }

  public double area()
  {
   
  }
  
} 

Right circular cone atop a cylinder

Calculating the area is trickier. If you assume the tree is one solid figure the area of the circle at the top of the cylinder and its intersection with the base of the cone are not part of the solid's area. So that twice that circular area must be subtraced from the sum, once for the cylinder and once for the cone.

Recall that both Cone and Cylinder have an area() method. Cylinder has a getRadius() method which may be useful.


QUESTION 9:

Fill in the method.


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