From 8b56c2800445d052bcee26b0a6592c3329f1ca3c Mon Sep 17 00:00:00 2001 From: p5wft6kpg <2734174142@qq.com> Date: Sat, 25 Oct 2025 14:27:29 +0800 Subject: [PATCH] ADD file via upload --- ShapeStatisticsAnalyzer.java | 180 +++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 ShapeStatisticsAnalyzer.java diff --git a/ShapeStatisticsAnalyzer.java b/ShapeStatisticsAnalyzer.java new file mode 100644 index 0000000..553bbf4 --- /dev/null +++ b/ShapeStatisticsAnalyzer.java @@ -0,0 +1,180 @@ +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()); + } + } +} \ No newline at end of file