定义一个名为Circle的类,其中含有double型的成员变量centerX和centerY表示圆心坐标,radius表示圆的半径。定义求圆面积的方法getArea()方法和求圆周长的方法getPerimeter().为半径radius定义访问方法和修改方法。定义一个带参数构造方法,通过给出圆的半径创建对象。定义默认构造方法,在该方法中调用有参数构造方法,将圆的半径设置为1.0.画出该类的UML图。编写程序测试这个圆类的所有方法
时间: 2024-02-03 08:15:57 浏览: 166
c#创建圆形类Circle、矩形类实现代码
以下是Circle类的UML图:
```
---------------------
| Circle |
---------------------
| - centerX: double |
| - centerY: double |
| - radius: double |
---------------------
| + Circle(radius: double) |
| + getArea(): double |
| + getPerimeter(): double |
| + setRadius(radius: double): void |
| + getRadius(): double |
---------------------
```
以下是Circle类的Java代码实现:
```java
public class Circle {
private double centerX;
private double centerY;
private double radius;
public Circle(double radius) {
setRadius(radius);
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
```
以下是Circle类的测试程序:
```java
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle(2.0);
System.out.println("Radius of c1: " + c1.getRadius());
System.out.println("Area of c1: " + c1.getArea());
System.out.println("Perimeter of c1: " + c1.getPerimeter());
c1.setRadius(3.0);
System.out.println("Radius of c1: " + c1.getRadius());
System.out.println("Area of c1: " + c1.getArea());
System.out.println("Perimeter of c1: " + c1.getPerimeter());
Circle c2 = new Circle(1.0);
System.out.println("Radius of c2: " + c2.getRadius());
System.out.println("Area of c2: " + c2.getArea());
System.out.println("Perimeter of c2: " + c2.getPerimeter());
}
}
```
输出结果为:
```
Radius of c1: 2.0
Area of c1: 12.566370614359172
Perimeter of c1: 12.566370614359172
Radius of c1: 3.0
Area of c1: 28.274333882308138
Perimeter of c1: 18.84955592153876
Radius of c2: 1.0
Area of c2: 3.141592653589793
Perimeter of c2: 6.283185307179586
```
阅读全文