C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Function Overloading - Volume Program - Java Programs

Write a class with the name Volume using function overloading that computes the volume of a cube, a sphere and a cuboid.
Formula:
volume of a cube (vc) = s * s * s.
volume of a sphere (vs) = 4 / 3 * π * r * r * r where π = 3.14 or 22 / 7.
volume of a cuboid (vcd) = l * b * h.

class Volume{ public static double volume(double s){ return Math.pow(s, 3); } public static double volume(double pi, double r){ return 4.0 / 3 * pi * Math.pow(r, 3); } public static double volume(double l, double b, double h){ return l * b * h; } }

Output