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.
25 lines
866 B
25 lines
866 B
public class SquareToCircleConverter implements ShapeConverter<Square, Circle> {
|
|
@Override
|
|
public Circle convert(Square source) {
|
|
if (!canConvert(source)) {
|
|
throw new IllegalArgumentException("Cannot convert null square");
|
|
}
|
|
|
|
// Calculate radius of circle with equal area to the square
|
|
double squareArea = source.calculateArea();
|
|
double circleRadius = Math.sqrt(squareArea / Math.PI);
|
|
|
|
// Create circle with the same color as the square
|
|
return new Circle(circleRadius, source.getColor());
|
|
}
|
|
|
|
@Override
|
|
public boolean canConvert(Square source) {
|
|
return source != null && source.getSide() > 0;
|
|
}
|
|
|
|
@Override
|
|
public String getConversionDescription() {
|
|
return "Converts a square to a circle with equal area and same color";
|
|
}
|
|
} |