Sarah got confused to calculate volume of cylinder and cuboid. Write a Java application to help Sarah to do this.
Create a class called VolumeCalculator that has the following methods
double calculateVolume(double radius,double height)
This method calculates the volume of the cylinder using the formula 3.14*radius*radius*height
double calculateVolume(int length,int breadth,int height)
This method calculates the volume of the cuboid using the formula length*breadth*height
Write a TestMain class to test the application.
Sample Input
Enter the choice
1.Find Volume For Cylinder
2.Find Volume For Cuboid
1
Enter the radius
3
Enter the height
2
Output
Volume of the Cylinder is 56.52
Sample Input
Enter the choice
1.Find Volume For Cylinder
2.Find Volume For Cuboid
2
Enter the length
3
Enter the breadth
2
Enter the height
1
Output
Volume of the Cuboid is 6.00
Answers
Program:
import java.util.*;
class VolumeCalculator
{
double calculateVolume(double radius, double height)
{
return (3.14 * radius * radius * height);
}
double calculateVolume(int length,int breadth,int height)
{
return (length * breadth * height);
}
}
public class TestMain
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
VolumeCalculator vol = new VolumeCalculator();
int n;
System.out.print("Press 1 : Find Volume For Cylinder \nPress 2: Find Volume For Cuboid \nEnter your choice : ");
n = Sc.nextInt();
if(n == 1)
{
System.out.print("Enter radius : ");
double radius = Sc.nextDouble();
System.out.print("Enter height : ");
double height = Sc.nextDouble();
System.out.print("Volume of the Cylinder is " + Math.round(vol.calculateVolume(radius , height) * 100.0) / 100.0 );
}
else if(n == 2)
{
System.out.print("Enter length : ");
int length = Sc.nextInt();
System.out.print("Enter breadth : ");
int breadth = Sc.nextInt();
System.out.print("Enter height : ");
int height = Sc.nextInt();
System.out.print("Volume of the Cuboid is " + vol.calculateVolume(length , breadth , height));
}
else
{
System.out.print("Invalid choice");
}
}
}
Output 1:
Press 1 : Find Volume For Cylinder
Press 2: Find Volume For Cuboid
Enter your choice : 1
Enter radius : 3
Enter height : 2
Volume of the Cylinder is 56.52
Output 2:
Press 1 : Find Volume For Cylinder
Press 2: Find Volume For Cuboid
Enter your choice : 2
Enter length : 3
Enter breadth : 2
Enter height : 1
Volume of the Cuboid is 6.0