Skip to content

Instantly share code, notes, and snippets.

@tkearney127
Created January 2, 2015 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkearney127/873e2f241fb8a0bf7a41 to your computer and use it in GitHub Desktop.
Save tkearney127/873e2f241fb8a0bf7a41 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Challenge193Easy{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
double volume;
System.out.print("Please input a volume in cubic meters: ");
volume = keyboard.nextDouble();
cubeDimensions(volume);
cylinderDimensions(volume);
sphereDimensions(volume);
coneDimensions(volume);
}
public static void cubeDimensions(double volume){
double side = Math.cbrt(volume);
System.out.printf("Cube: %.2fm width, %.2fm high, %.2fm tall %n", side, side, side);
}
public static void cylinderDimensions(double volume){
double height, diameter, radius;
radius = Math.cbrt(volume / (2 * Math.PI));
height = volume / (Math.PI * Math.pow(radius, 2));
diameter = radius * 2;
System.out.printf("Cylinder: %.2fm tall, Diameter of %.2fm %n", height, diameter);
}
public static void sphereDimensions(double volume){
double radius = Math.cbrt(volume / (4.0/3.0 * Math.PI));
System.out.printf("Sphere: %.2fm Radius %n", radius);
}
public static void coneDimensions(double volume){
double height, radius;
radius = Math.cbrt((3 * volume) / (Math.PI * Math.sqrt(2)));
height = (3 * volume) / (Math.PI * Math.pow(radius, 2));
System.out.printf("Cone: %.2fm tall, %.2fm Radius %n", height, radius);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment