1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import java.util.List;
interface Shape { double area(); }
class Rectangle implements Shape { private double width; private double height;
public Rectangle(double width, double height) { this.width = width; this.height = height; }
@Override public double area() { return width * height; } }
class Circle implements Shape { private double radius;
public Circle(double radius) { this.radius = radius; }
@Override public double area() { return Math.PI * radius * radius; } }
class AreaCalculator { public double calculateArea(List<Shape> shapes) { double totalArea = 0; for (Shape shape : shapes) { totalArea += shape.area(); } return totalArea; } }
public class Main { public static void main(String[] args) { AreaCalculator calculator = new AreaCalculator(); List<Shape> shapes = List.of(new Rectangle(5, 10), new Circle(7)); double totalArea = calculator.calculateArea(shapes); System.out.printf("Total area: %.2f%n", totalArea); } }
|