1. Write a program to find the area and volume of cuboid, cube, cylinder, cone and sphere for a given choice of input character.
Answers
Java program as below.
import java.io.*;
public class Driver
{
public static void main(String[] args) throws IOException
{
BufferedReader buffread = 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(buffread.readLine());
switch (a) //actions based on choice
{
case 1:
System.out.println("Enter value for side:");
Float side= Float.parseFloat(buffread.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(buffread.readLine());
System.out.println("Enter value of breadth");
Float breadth= Float.parseFloat(buffread.readLine());
System.out.println("Enter value of height:");
Float height= Float.parseFloat(buffread.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(buffread.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(buffread.readLine());
System.out.println("Enter value for height:");
float height1= Float.parseFloat(buffread.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));
buffreadeak;
case 5:
System.exit(0);
default:
System.out.println("Invalid Entry!");
}
}
}
class Cube
{
public float surfaceArea(float sidelength) //surface area calculation
{
return 6*sidelength*sidelength;
}
public float volume(float sidelength) //volume calculation using formula
{
return (sidelength*sidelength*sidelength);
}
}
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 radiuslength)
{
return 4/3*22/7*radiuslength*radiuslength ;
}
public float volume(float radiuslength)
{
return (4/3*22/7*radiuslength*radiuslength*radiuslength );
}
}
Answer:
"#include <bits/stdc++.h>
using namespace std;
// utility function
double areaCuboid(double l, double h, double w)
{
return (l * h * w);
}
double surfaceAreaCuboid(double l, double h, double w)
{
return (2 * l * w + 2 * w * h + 2 * l * h);
}
// driver function
int main()
{
double l = 1;
double h = 5;
double w = 7;
cout << ""Area = "" << areaCuboid(l, h, w) << endl;
cout << ""Total Surface Area = ""
<< surfaceAreaCuboid(l, h, w);
return 0;
}
"
Explanation: