C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Function Overloading - Volume Program - Java Programs

Design a class to overload a function volume() as follows:

(i) double volume(double r) – with radius (r) as an argument, returns the volume of sphere using the formula: v = 4 / 3 × 22 / 7 × r3

(ii) double volume(double h, double r) – with height (h) and radius (r) as the arguments, returns the volume of a cylinder using the formula: v = 22 / 7 × r2 × h

(iii) double volume(double l, double b, double h) – with length(l), breadth (b) and height (h) as the arguments, returns the volume of a cuboid using the formula: v = l × b × h


import java.util.Scanner; public class Overload { double volume(double r) { double vol = (4 / 3.0) * (22 / 7.0) * r * r * r; return vol; } double volume(double h, double r) { double vol = (22 / 7.0) * r * r * h; return vol; } double volume(double l, double b, double h) { double vol = l * b * h; return vol; } public static void main(String args[]) { Overload obj = new Overload(); System.out.println("Sphere Volume = " + obj.volume(6)); System.out.println("Cylinder Volume = " + obj.volume(5, 3.5)); System.out.println("Cuboid Volume = " + obj.volume(7.5, 3.5, 2)); } }

Output

 
 OUTPUT : 
Sphere Volume = 905.142857142857
Cylinder Volume = 192.5
Cuboid Volume = 52.5