Cone:
Surface Area = π * r * (r + slant height)
Volume = (1/3) * π * r² * h
Cylinder:
Surface Area = 2 * π * r * (r + h)
Volume = π * r² * h
abstract class Shape
{
abstract double area();
abstract double volume();
}
class Cone extends Shape {
private double radius;
private double height;
//Constructor
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
double area()
{
double slantHeight = Math.sqrt((radius * radius) + (height * height));
return Math.PI * radius * (radius + slantHeight);
}
double volume()
{
return (1.0 / 3) * Math.PI * radius * radius * height;
}
}
class Cylinder extends Shape {
private double radius;
private double height;
//constructor
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
double area() {
return 2 * Math.PI * radius * (radius + height);
}
double volume() {
return Math.PI * radius * radius * height;
}
}
public class ShapeTest {
public static void main(String[] args) {
Cone cone = new Cone(5, 10);
Cylinder cylinder = new Cylinder(5, 10);
System.out.println("Cone Area: " + cone.area());
System.out.println("Cone Volume: " + cone.volume());
System.out.println("Cylinder Area: " + cylinder.area());
System.out.println("Cylinder Volume: " + cylinder.volume());
}
}