The calculateMPG()
method can not use the parameters of the constructor.
// 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.
Can the height
and radius
of a Cone
be changed?