Skip to content

Instantly share code, notes, and snippets.

@jimmykurian
Created February 21, 2012 05:38
Show Gist options
  • Save jimmykurian/1873994 to your computer and use it in GitHub Desktop.
Save jimmykurian/1873994 to your computer and use it in GitHub Desktop.
A Java program that will compute the volume and surface area of a sphere with radius r, a cylinder with circular base with radius r and height h, and a cone with circular base with radius r and height h.
//Geometry.java - Jimmy Kurian
public class Geometry
{
public static double sphereVolume(double r)
{
return (4.0 / 3.0) * Math.PI * r * r * r;
}
public static double sphereSurface(double r)
{
return 4.0 * Math.PI * r * r;
}
public static double cylinderVolume(double r, double h)
{
return h * Math.PI * r * r;
}
public static double cylinderSurface(double r, double h)
{
return (2.0 * r * Math.PI * h) + (2.0 * Math.PI * r * r);
}
public static double coneVolume(double r, double h)
{
return (1.0 / 3.0) * Math.PI * r * r * h;
}
public static double coneSurface(double r, double h)
{
return Math.PI * r * (h + r);
}
}
//GeometryTester.java - Jimmy Kurian
import java.util.Scanner;
public class GeometryTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter the radius: ");
double r = in.nextDouble();
System.out.println("Please enter the height: ");
double h = in.nextDouble();
System.out.println("The volume of the sphere is: "
+ Geometry.sphereVolume(r));
System.out.println("The surface area of the sphere is: "
+ Geometry.sphereSurface(r));
System.out.println("The volume of the cylinder is: "
+ Geometry.cylinderVolume(r, h));
System.out.println("The surface area of the cylinder is: "
+ Geometry.cylinderSurface(r, h));
System.out.println("The volume of the cone is: "
+ Geometry.coneVolume(r, h));
System.out.println("The surface area of the cone is: "
+ Geometry.coneSurface(r, h));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment