Computer Applications

Write a Java Program to define overloaded function volume( ) to calculate the volume for Cube, Cuboid and Sphere

  • Write a Java Program to define overloaded function volume( ) to calculate the volume for the following:
    1. Volume of Cube                  [vc = s * s * s]
    2. Volume of Cuboid               [vcd = l * b * h]
    3. Volume of Sphere               [vs = 4/3 * π* r *r *r, where π = 3.14]

import java.io.*;
import java.util.*;

class Overloaded
{
	void volume( int s )
	{
		int vol = s*s*s;
		System.out.println(" Volume of Cube = " + vol );
	}
	void volume( int l, int b, int h )
	{
		int vol = l*b*h;
		System.out.println(" Volume of Cuboid = " + vol );
	}
	void volume( double r )
	{
		double vol = 4/3*3.14*r*r*r;
		System.out.println(" Volume of Sphere = " + vol );
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	Overloaded ob = new Overloaded( );
  	ob.volume( 6 );
  	ob.volume( 8, 3 );
  	ob.volume( 5.3 );
  }
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top