Write a program in java that computes the volume of a cube, a sphere and a cuboid and call all the
member methods under main method.
Formula :
Volume of a cube (vc) = s*s*s
Volume of a sphere (vs) = 4/3 * pi * r * r * r(where pi = 3.14 or 22/7)
Volume of a cuboid (vcd) = l * b * h
Answers
Answer:
Program:-
import java.util.*;
public class Volume
{
int ch=0;
double s,r,l,b,h,vc=0.0,vs=0.0,vcd=0.0;
public void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter 1 for volume of cube");
System.out.println("Enter 2 for volume of sphere");
System.out.println("Enter 3 for volume of cuboid");
System.out.println("Enter your choice");
ch=in.nextInt();
}
public void calculate()
{
switch(ch)
{
case 1:
System.out.println("Enter the side of cube");
s=in.nextDouble();
vc=s*s*s;
System.out.println("The volume of cube="+vc);
break;
case 2:
System.out.println("Enter the radius of sphere");
r=in.nextDouble();
vs=4.0/3.0*3.14*r*r*r;
System.out.println("The volume of sphere="+vs);
break;
case 3:
System.out.println("Enter the length, breath and height of cuboid");
l=in.nextDouble();
b=in.nextDouble();
h=in.nextDouble();
vcd=l*b*h;
System.out.println("The volume of cuboid="+vcd);
break;
default:
System.out.println("Invalid choice");
}
public static void main(String args[ ])
{
Volume vl=new Volume();
vl.input();
vl.calculate();
}
}