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

Answer:

No, not in the current program


Another Error

The program below will not compile. It tries to access the private instance variables of the object, and this cannot be done.


public class TestCone
{
  public static void main( String[] args ) 
  {
    Cone cone  = new Cone( 1.2, 4.56 );
    System.out.println( "cone area: " + cone.area() 
      + " volume: " + cone.volume() );

    cone.height = 4.5;         // Can't accrss
    cone.radius = 13.06;       // private members

    System.out.println( "cone area: " + cone.area() 
      + " volume: " + cone.volume() );
  }
}

One approach to this problem is to make the instance variables public. But violates the idea of modularity. A better approach is to include methods that can be used to access the variables.

Here is the full list of methods from the beginning of this chapter:

Methods

  • public double area() calculates and returns the area of the cone
  • public double volume() calculates and returns the volume of the cone
  • public void setHeight() changes the height of a cone
  • public void setRadius() changes the radius of a cone
  • public double getHeight() returns the height of a cone
  • public double getRadius() returns the radius of a cone

QUESTION 20:

Which method from the above list changes the height of a Cone?


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