interface Shape { double getArea(); double getPerimeter(); }
class Circle implements Shape { private double r; public Circle(double r) { this.r = r; } @Override public double getArea() { return Math.PI * r * r; } @Override public double getPerimeter() { return 2 * Math.PI * r; } }
class Rectangle implements Shape { private double l, w; public Rectangle(double l, double w) { this.l = l; this.w = w; } @Override public double getArea() { return l * w; } @Override public double getPerimeter() { return 2 * (l + w); } }
public class Main { public static void main(String[] args) { Shape c = new Circle(5); System.out.println("Circle area: " + c.getArea()); System.out.println("Circle perimeter: " + c.getPerimeter()); Shape r = new Rectangle(4, 6); System.out.println("Rectangle area: " + r.getArea()); System.out.println("Rectangle perimeter: " + r.getPerimeter()); } }