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

Answer:

The calculateMPG() method can not use the parameters of the constructor.


Can't Use Constructor's Parameters in a Method

// file: Cone.java
//
public class Cone
{
  private double radius;  // radius of the base
  private double height;  // height of the cone
  
  public Cone( double R, double H )
  {
    radius = R;
    height = H;
  }
  
  public double volume()
  {
    return Math.PI*R*R*H/3.0;  
  }
  
}

The volume() method can not use the parameters of the constructor. Another way of saying this is that the scope of the parameters is limited to the body of the constructor. The compiler will complain that R, and H are "undefined variables" because they are used outside of their scope.


QUESTION 19:

Can the height and radius of a Cone be changed?


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