Function overloading java program to compute volume of sphere,cube,cuboid
Answers
Answered by
11
Hey There!!
Here is a sample program code:
import java.util.Scanner;public class Volume
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("1. Volume of Sphere");
System.out.println("2. Volume of Cube");
System.out.println("3. Volume of Cuboid");
System.out.print("\nEnter your choice [1-3]: ");
int ch = sc.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter radius: ");
double r = sc.nextDouble();
System.out.println("Volume of Sphere = "+Vol(r)+" cubic units");
break;
case 2:
System.out.print("Enter length: ");
int l = sc.nextInt();
System.out.println("Volume of Cube = "+Vol(l)+" cubic units");
break;
case 3:
System.out.print("Enter length: ");
l = sc.nextInt();
System.out.print("Enter length: ");
int b = sc.nextInt();
System.out.print("Enter length: ");
int h = sc.nextInt();
System.out.println("Volume of Cuboid = "+Vol(l,b,h)+" cubic units");
break;
default:
System.out.println("Invalid Choice");
}
}
static double Vol(double r)
{
double vol = 4*(3.1416)*r*r*r;
vol = vol/3;
return vol;
}
static int Vol(int l)
{
return (l*l*l);
}
static int Vol(int l, int b,int h)
{
return (l*b*h);
}
}
Hope it helps
Purva
Brainly Community
Here is a sample program code:
import java.util.Scanner;public class Volume
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("1. Volume of Sphere");
System.out.println("2. Volume of Cube");
System.out.println("3. Volume of Cuboid");
System.out.print("\nEnter your choice [1-3]: ");
int ch = sc.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter radius: ");
double r = sc.nextDouble();
System.out.println("Volume of Sphere = "+Vol(r)+" cubic units");
break;
case 2:
System.out.print("Enter length: ");
int l = sc.nextInt();
System.out.println("Volume of Cube = "+Vol(l)+" cubic units");
break;
case 3:
System.out.print("Enter length: ");
l = sc.nextInt();
System.out.print("Enter length: ");
int b = sc.nextInt();
System.out.print("Enter length: ");
int h = sc.nextInt();
System.out.println("Volume of Cuboid = "+Vol(l,b,h)+" cubic units");
break;
default:
System.out.println("Invalid Choice");
}
}
static double Vol(double r)
{
double vol = 4*(3.1416)*r*r*r;
vol = vol/3;
return vol;
}
static int Vol(int l)
{
return (l*l*l);
}
static int Vol(int l, int b,int h)
{
return (l*b*h);
}
}
Hope it helps
Purva
Brainly Community
Similar questions