import java.util.ArrayList; import java.util.List; public class CompositeShape implements Shape, Resizable { private List shapes; private String color; public CompositeShape(String color) { this.shapes = new ArrayList<>(); this.color = color; } public CompositeShape() { this("Default"); } // Add a shape to the composite public void addShape(Shape shape) { if (shape != null) { shapes.add(shape); } } // Remove a shape from the composite public boolean removeShape(Shape shape) { return shapes.remove(shape); } // Get all shapes in the composite public List getShapes() { return new ArrayList<>(shapes); // Return a copy to prevent external modification } @Override public double calculateArea() { // Total area is the sum of areas of all child shapes double totalArea = 0; for (Shape shape : shapes) { totalArea += shape.calculateArea(); } return totalArea; } @Override public double calculatePerimeter() { // For composite shape, perimeter is more complex to calculate // For simplicity, we'll sum the perimeters of all child shapes // Note: This is not geometrically accurate for overlapping shapes double totalPerimeter = 0; for (Shape shape : shapes) { totalPerimeter += shape.calculatePerimeter(); } return totalPerimeter; } @Override public String getColor() { return color; } @Override public void setColor(String color) { this.color = color; // Optionally, we could also set the color of all child shapes } @Override public void resize(double factor) { // Resize all child shapes that implement Resizable for (Shape shape : shapes) { if (shape instanceof Resizable) { ((Resizable) shape).resize(factor); } } } @Override public void resizeWidth(double factor) { // Resize width of all child shapes that implement Resizable for (Shape shape : shapes) { if (shape instanceof Resizable) { ((Resizable) shape).resizeWidth(factor); } } } @Override public void resizeHeight(double factor) { // Resize height of all child shapes that implement Resizable for (Shape shape : shapes) { if (shape instanceof Resizable) { ((Resizable) shape).resizeHeight(factor); } } } // Set color for all child shapes public void setAllShapesColor(String color) { for (Shape shape : shapes) { shape.setColor(color); } } // Get the number of shapes in the composite public int getShapeCount() { return shapes.size(); } @Override public String toString() { return "CompositeShape [color=" + color + ", shapeCount=" + shapes.size() + "]"; } }