Program to calculate total surface area of 3D shapes (Cube, Cuboid, Sphere, Cylinder) based on user choice. Also display an error message if user choice does not match.
Java
import java.util.*;
public class ShapeArea3D
{
public static void main(String args[])
{
int choice=0;
double l=0.0,b=0.0,h=0.0,r=0.0,s=0.0,result=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("1-To calculate area of cube");
System.out.println("2-To calculate area of cuboid");
System.out.println("3-To calculate area of sphere");
System.out.println("4-To calculate area of cylinder");
System.out.print("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
{
System.out.print("Side: ");
s=sc.nextDouble();
result=6*s*s;
System.out.println("Area of cube: "+result);
break;
}
case 2:
{
System.out.print("Length: ");
l=sc.nextDouble();
System.out.print("Breadth: ");
b=sc.nextDouble();
System.out.print("Height: ");
h=sc.nextDouble();
result=2*((l*b)+(b*h)+(h*l));
System.out.println("Area of cuboid: "+result);
break;
}
case 3:
{
System.out.print("Radius: ");
r=sc.nextDouble();
result=4*(22/7)*r*r;
System.out.println("Area of sphere: "+result);
break;
}
case 4:
{
System.out.print("Radius: ");
r=sc.nextDouble();
System.out.print("Height: ");
h=sc.nextDouble();
result=2*(22/7)*r*(r+h);
System.out.println("Area of cylinder: "+result);
break;
}
default:
{
System.out.println("Invalid choice");
}
}
}
}Java