Skip to content

Instantly share code, notes, and snippets.

@awhit012
Created January 8, 2015 21:55
Show Gist options
  • Save awhit012/e09d436eae20d63a3f11 to your computer and use it in GitHub Desktop.
Save awhit012/e09d436eae20d63a3f11 to your computer and use it in GitHub Desktop.
Simple Ruby program that accepts a volume in cubic meters and gives the dimensions of shapes that contain that volume
class Volume_To_Shapes
def initialize volume_input
@volume_input = volume_input
@height = cube_root @volume_input
get_cube_dimensions
get_sphere_dimensions
get_cylinder_dimensions
get_cone_dimensions
end
def cube_root input
Math.exp(Math.log(input.to_f)/3.to_f).round(8)
end
def get_cube_dimensions
puts "Cube: " + @height.round(2).to_s + "m on each side"
end
def get_sphere_dimensions
puts "Sphere: " + (cube_root (0.75 * @volume_input / Math::PI) ).round(2).to_s + "m radius"
end
def get_cylinder_dimensions
puts "Cylinder: " + Math.sqrt(@volume_input/ (Math::PI * @height)).round(2).to_s + "m radius, " + @height.round(2).to_s + "m height"
end
def get_cone_dimensions
puts "Cone: " + Math.sqrt( 3 * ( @volume_input / (Math::PI * @height) ) ).round(2).to_s + "m radius, " + @height.round(2).to_s + "m height"
end
end
Volume_To_Shapes.new 27
puts "-" * 40
Volume_To_Shapes.new 42
puts "-" * 40
Volume_To_Shapes.new 1000
puts "-" * 40
Volume_To_Shapes.new 2197
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment