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

Answer:

A working Cone is below. Copy it to a file and run it with ConeTester.

More methods need to be added to Cone, but it is good to compile and run programs as soon as possible to find bugs and design errors.


Working Cone

// file: Cone.java
//
public class Cone
{
  private double radius;  // radius of the base
  private double height;  // height of the cone
  
  public Cone( double radius, double height )
  {
    this.radius = radius;
    this.height = height;
  }
  
  public double area()
  {
    return Math.PI*radius*(radius + Math.sqrt(height*height + radius*radius) );
  }
  
  public double volume()
  {
    return Math.PI*radius*radius*height/3.0;  
  }
  
}


// file: TestCone.java
//
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( radius, height );

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

Each class is contained in a separate file. The volume() method returns a double value to the caller. Since it returns a value, there must be a return statement within its body that returns a value of the correct type.


QUESTION 11:

When you compile this program how many class files will there be?


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