No, not in the current program
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 conepublic double volume()calculates and returns the volume of the conepublic void setHeight()changes the height of a conepublic void setRadius()changes the radius of a conepublic double getHeight()returns the height of a conepublic double getRadius()returns the radius of a conepublic String toString()returns a reference to a String that describes this object
Which method from the above list changes the height of a Cone?