import java.util.HashMap; import java.util.List; import java.util.Map; public class ShapeStatisticsAnalyzer { private List shapes; public ShapeStatisticsAnalyzer(List shapes) { this.shapes = shapes; } // Count shapes by their type public Map countShapesByType() { Map typeCount = new HashMap<>(); for (Shape shape : shapes) { String typeName = shape.getClass().getSimpleName(); typeCount.put(typeName, typeCount.getOrDefault(typeName, 0) + 1); } return typeCount; } // Count shapes by their color public Map countShapesByColor() { Map colorCount = new HashMap<>(); for (Shape shape : shapes) { String color = shape.getColor().toLowerCase(); colorCount.put(color, colorCount.getOrDefault(color, 0) + 1); } return colorCount; } // Calculate the total area of all shapes public double calculateTotalArea() { double totalArea = 0; for (Shape shape : shapes) { totalArea += shape.calculateArea(); } return totalArea; } // Calculate area percentage by shape type public Map calculateAreaPercentageByType() { Map typeArea = new HashMap<>(); double totalArea = calculateTotalArea(); if (totalArea == 0) { return new HashMap<>(); } // First calculate total area for each type for (Shape shape : shapes) { String typeName = shape.getClass().getSimpleName(); double currentArea = typeArea.getOrDefault(typeName, 0.0); typeArea.put(typeName, currentArea + shape.calculateArea()); } // Calculate percentages Map percentageMap = new HashMap<>(); for (Map.Entry entry : typeArea.entrySet()) { percentageMap.put(entry.getKey(), (entry.getValue() / totalArea) * 100); } return percentageMap; } // Calculate area percentage by color public Map calculateAreaPercentageByColor() { Map colorArea = new HashMap<>(); double totalArea = calculateTotalArea(); if (totalArea == 0) { return new HashMap<>(); } // First calculate total area for each color for (Shape shape : shapes) { String color = shape.getColor().toLowerCase(); double currentArea = colorArea.getOrDefault(color, 0.0); colorArea.put(color, currentArea + shape.calculateArea()); } // Calculate percentages Map percentageMap = new HashMap<>(); for (Map.Entry entry : colorArea.entrySet()) { percentageMap.put(entry.getKey(), (entry.getValue() / totalArea) * 100); } return percentageMap; } // Get the shape with the largest area public Shape getLargestShape() { if (shapes == null || shapes.isEmpty()) { return null; } Shape largest = shapes.get(0); for (Shape shape : shapes) { if (shape.calculateArea() > largest.calculateArea()) { largest = shape; } } return largest; } // Get the shape with the smallest area public Shape getSmallestShape() { if (shapes == null || shapes.isEmpty()) { return null; } Shape smallest = shapes.get(0); for (Shape shape : shapes) { if (shape.calculateArea() < smallest.calculateArea()) { smallest = shape; } } return smallest; } // Print all statistics public void printStatistics() { System.out.println("=== Shape Statistics ==="); // Print type statistics System.out.println("\nShape count by type:"); for (Map.Entry entry : countShapesByType().entrySet()) { System.out.println(" " + entry.getKey() + ": " + entry.getValue()); } // Print color statistics System.out.println("\nShape count by color:"); for (Map.Entry entry : countShapesByColor().entrySet()) { System.out.println(" " + entry.getKey() + ": " + entry.getValue()); } // Print total area System.out.println("\nTotal area: " + calculateTotalArea()); // Print area percentage by type System.out.println("\nArea percentage by type:"); for (Map.Entry entry : calculateAreaPercentageByType().entrySet()) { System.out.printf(" %s: %.2f%%\n", entry.getKey(), entry.getValue()); } // Print area percentage by color System.out.println("\nArea percentage by color:"); for (Map.Entry entry : calculateAreaPercentageByColor().entrySet()) { System.out.printf(" %s: %.2f%%\n", entry.getKey(), entry.getValue()); } // Print largest and smallest shapes Shape largest = getLargestShape(); Shape smallest = getSmallestShape(); if (largest != null) { System.out.println("\nLargest shape:"); System.out.printf(" Type: %s, Color: %s, Area: %.2f\n", largest.getClass().getSimpleName(), largest.getColor(), largest.calculateArea()); } if (smallest != null) { System.out.println("\nSmallest shape:"); System.out.printf(" Type: %s, Color: %s, Area: %.2f\n", smallest.getClass().getSimpleName(), smallest.getColor(), smallest.calculateArea()); } } }