JAVA   111
ConeArea
Guest on 21st September 2022 01:04:04 AM


  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import.java.text.*;
  5.  import java.text.DecimalFormat;
  6. public class ConeArea {
  7.  
  8.     /**
  9.      * The radius of the cone.
  10.      */
  11.     static double radius;
  12.  
  13.     /**
  14.      * The radius of the cone.
  15.      */
  16.     static double height;
  17.  
  18.     /**
  19.      * The radius of the cone.
  20.      */
  21.     static double theVolume;
  22.  
  23.     /**
  24.      * Calculates the volume of a cone.
  25.      * @return double representing volume of cone
  26.      * @param radius
  27.      * @param height
  28.      */
  29.     static public double volume(double radius, double height)
  30.     {
  31.         // Note: volume = 1/3 PI R^2 x height
  32.         return Math.PI * Math.pow(radius, 2) * height / 3.0;
  33.     }
  34.     public static void main(String[] args) {
  35.         DecimalFormat df = new DecimalFormat("0.00");
  36.         java.util.Scanner s = new java.util.Scanner(System.in);
  37.  
  38.         System.out.print("Enter the radius of the cone > ");
  39.         radius = s.nextDouble();
  40.  
  41.         System.out.print("Enter the height of the cone > ");
  42.         height = s.nextDouble();
  43.  
  44.         theVolume = volume(radius, height);
  45.  
  46.         System.out.println("The volume of a cone with " +
  47.                            "radius " + df.format(radius) +
  48.                            " and height " + df.format(height) +
  49.                            " is " + df.format(theVolume));
  50.     }
  51.  
  52. }

Raw Paste

Login or Register to edit or fork this paste. It's free.