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

Answer:

The total volume is the sum of the cone volume and the cylinder volume.


Turn up the Volume

Luckily the Cone and Cylinder classes each have a volume() method. Here is the Tree class so far:

// 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( trRad, trHeight );
    branches = new Cone( brRad, brHeight );
    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 ;
  }
  
  // more methods to come ...
  public double volume()
  {
   
  }
  
} 

QUESTION 8:

Complete the volume() method.


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