The filled in program is below.
public class TestCone { public static void main( String[] args ) { Cone cone1 = new Cone( 2.5, 5.8 ); System.out.println( "cone1 area: " + cone1.area() + " volume: " + cone1.volume() ); Cone cone2 = new Cone( 3.56, 2.12 ); System.out.println( "cone2 area: " + cone2.area() + " volume: " + cone2.volume() ); } }
In this program, two different Cone
objects are constructed,
each with different data,
and two methods are invoked for each.
Each invoked method uses the data in the instance variables of the object that contains it.
The picture shows the situation just after the second object has been constructed
according to the definition of Cone
.
Class TestCone
contains the static main()
method.
There are two objects, but each uses the same names for its instance
variables! How does area()
know which ones to use?