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

Answer:


  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 = trunk.getRadius();
   double circle = Math.PI*rad*rad;
   return total - 2*circle;
  }

Growth

// 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 ;
  }
  
  // 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 = trunk.getRadius();
   double circle = Math.PI*rad*rad;
   return total - 2*circle;
  }

  public void grow( double rate )
  {
   
  }
    
} 

Trees grow every year. Finish the method that increases all dimensions of a tree by rate. For example, if rate is 0.10 and the height of the cone is 100, then change the height of the cone to 110. Make similar changes to the other three dimensions of a tree. This is probably not a realistic model of tree growth.


QUESTION 10:

Fill in the method.


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