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

Could a main() method create a Cone object?

Answer:

Sure, as long as it has access to the class definition.


Using a Cone Object

Look at the constructor:

public Cone( double radius, double height )

The constructor must be called with two double precision values.

User interaction in the following is similar to previous programs. All input values are double precision.


import java.util.Scanner ;

public class TestCone
{
  public static void main( String[] args ) 
  {
    Scanner scan = new Scanner(System.in);

    double radius, height;

    System.out.print("Enter radius: " ); 
    radius = scan.nextDouble();

    System.out.print("Enter height: " ); 
    height = scan.nextDouble();
 
    Cone cone = new Cone( ,   );

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

If Cone.java contains the definition of the Cone class and TestCone.java contains the above program and both are in the same disk directory, then the program can be compiled and run by doing:

C:\JavaCode>javac TestCone.java
C:\JavaCode>java  TestCone
Enter radius: 3.2
Enter height: 4.1
Area 84.45567607387638 Volume: 43.96554198943796

The compiler javac will automatically look for Cone.java in the same disk directory. But first we need to write it.



QUESTION 3:

Fill in the blanks so that the Cone constructor uses data from the user.


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