Write a Program to Calulate volume of Cuboid and cylinder
as per user's choice.
volume of cuboid = lxbxh
volume of cube = S3
Volume of Cylinder = πr2h (Program in Bluej Program)
Answers
Answer:
Java Program to Perform Operations on Cube, Cuboid, Sphere and Cylinder
import java.io.*;
public class Driver
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
Cube c=new Cube(); //creating objects for all classes
Cuboid cd=new Cuboid();
Sphere s=new Sphere();
Cylinder cr=new Cylinder();
System.out.println("1. Cube"); //user input
System.out.println("2. Cuboid");
System.out.println("3. Sphere");
System.out.println("4. Cylinder");
System.out.println("enter your choice:"); //choice reading
int a= Integer.parseInt(br.readLine());
switch (a) //actions based on choice
{
case 1:
System.out.println("Enter value for side:");
Float side= Float.parseFloat(br.readLine());
System.out.println("The surface area for cube is=" + c.surfaceArea(side));
System.out.println("The volume of cube=" + c.volume(side));
break;
case 2:
System.out.println("Enter value of length:");
float length= Float.parseFloat(br.readLine());
System.out.println("Enter value of breadth");
Float breadth= Float.parseFloat(br.readLine());
System.out.println("Enter value of height:");
Float height= Float.parseFloat(br.readLine());
System.out.println("Surface area of cuboid is=" + cd.surfaceArea(length,breadth,height));
System.out.println("volume of cuboid is=" + cd.volume(length,breadth,height));
break;
case 3:
System.out.println("Enter value for radius:");
Float radius= Float.parseFloat(br.readLine());
System.out.println("The surface area for Sphere is=" + s.surfaceArea(radius));
System.out.println("The volume of Sphere=" + s.volume(radius));
break;
case 4:
System.out.println("Enter value for radius:");
float radius1= Float.parseFloat(br.readLine());
System.out.println("Enter value for height:");
float height1= Float.parseFloat(br.readLine());
System.out.println("The surface area for Sphere is=" + cr.surfaceArea(radius1,height1));
System.out.println("The volume of Sphere=" + cr.volume(radius1,height1));
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid Entry!");
}
}
}
class Cube
{
public float surfaceArea(float side) //surface area calculation
{
return 6*side*side;
}
public float volume(float side) //volume calculation using formula
{
return (side*side*side);
}
}
class Cuboid
{
public float surfaceArea(float length,float breadth,float height)
{
return (2*((length*breadth)+(breadth*height)+(height*length))) ;
}
public float volume(float length,float breadth,float height)
{
return (length*breadth*height );
}
}
class Cylinder
{
public float surfaceArea(float radius,float height)
{
return (2*22/7*radius*height );
}
public float volume(float radius,float height)
{
return (22/7*radius*radius*height );
}
}
class Sphere
{
public float surfaceArea(float radius)
{
return 4/3*22/7*radius*radius ;
}
public float volume(float radius)
{
return (4/3*22/7*radius*radius*radius );
}
}
OUTPUT:
1. Cube
2. Cuboid
3. Sphere
4. Cylinder
enter your choice:
1
Enter value for side:
10
The surface area for cube is=600.0
The volume of cube=1000.0
NOTE: It is a Menu Driven Program Written in Java Programming Language and Can Be Executed on any Program eg, Bluej But Not Recommended for Lower Classes than class 9.
//java program to calculate the volume of cuboid, cube and cylinder.
import java.util.*;
public class Volume
{
public static void main (String args [])
{
Scanner in = new Scanner(System.in);
int l, b, h, s, r, h;
double c, b, l;
System.out.println("enter the length,breadth,height of the cuboid, side of a cube, radius and height of a cylinder.")
l = in.nextInt();
b = in.nextInt();
h = in.nextInt();
s = in.nextInt();
r = in.nextInt();
h = in.nextInt();
c = l*b*h;
b = s*s*s;
l = 22/7*r*r*h;
System.out.println("Volume of cuboid =" +c);
System.out.println("Volume of cube =" +b);
System.out.println("Volume of cylinder =" +l);
}
}