public class Main {
public static void main(String[] args) throws Exception {
Circle c1 = new Circle();
c1.setRadius(5);
Circle c2 = new Circle(8);
int compareResult = compareCircle(c1, c2);
if (compareResult > 0) {
System.out.println("c1 比 c2 大");
} else if (compareResult < 0) {
System.out.println("c1 比 c2 小");
} else {
System.out.println("c1 和 c2 大小一样");
}
}
public static int compareCircle(Circle c1, Circle c2) {
if (c1.getRadius() > c2.getRadius()) { // c1半径大于c2
return 1;
} else if (c1.getRadius() < c2.getRadius()) { // c1半径小于c2
return -1;
}
return 0; // c1半径等于c2
}
}
class Circle {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
运行结果: