You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
50 lines
1.5 KiB
package jinjieti;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 图形统计分析器(进阶题功能)
|
|
*/
|
|
public class ShapeStatisticsAnalyzer {
|
|
/**
|
|
* 统计每种图形的数量
|
|
*/
|
|
public Map<String, Integer> countShapeTypes(List<Shape> shapes) {
|
|
Map<String, Integer> countMap = new HashMap<>();
|
|
if (shapes == null) return countMap;
|
|
|
|
for (Shape shape : shapes) {
|
|
String type = shape.getClass().getSimpleName();
|
|
countMap.put(type, countMap.getOrDefault(type, 0) + 1);
|
|
}
|
|
return countMap;
|
|
}
|
|
|
|
/**
|
|
* 计算每种图形的面积占比
|
|
*/
|
|
public Map<String, Double> calculateAreaRatio(List<Shape> shapes) {
|
|
Map<String, Double> areaMap = new HashMap<>();
|
|
double totalArea = 0.0;
|
|
|
|
// 计算总面积和各类型面积
|
|
if (shapes != null) {
|
|
for (Shape shape : shapes) {
|
|
String type = shape.getClass().getSimpleName();
|
|
double area = shape.getArea();
|
|
areaMap.put(type, areaMap.getOrDefault(type, 0.0) + area);
|
|
totalArea += area;
|
|
}
|
|
}
|
|
|
|
// 计算占比(避免除零)
|
|
if (totalArea > 0) {
|
|
for (Map.Entry<String, Double> entry : areaMap.entrySet()) {
|
|
areaMap.put(entry.getKey(), entry.getValue() / totalArea);
|
|
}
|
|
}
|
|
return areaMap;
|
|
}
|
|
} |