定义一个名为circle的类表圆,其中含有double型的成员变量centerx、centery表示圆心坐标,radius表示圆的半径。定义求圆面积的方法getarea()方法和求圆周长的方法getp
时间: 2023-05-01 15:07:05 浏览: 268
该类表达了一个名为 circle 的圆,其中包含有 double 类型的成员变量 centerx、centery 分别表示圆心坐标,radius 表示圆的半径。定义了求圆面积的方法 getarea() 和求圆周长的方法 getp()。
相关问题
: 定义一个名为Circle的类表圆,其中含有double型的成员变量centerX、centerY表示圆心坐标,radius表示圆的半径。定义求圆面积的方法getAreaO方法和求圆周长的方法getPerimeterO. 为半径radius定义访问方法和修改方法。定义默认构造方法,在该方法中调用有参数构造方法,将圆的半径设置为1.0。 画出该类的UML图。 编写程序测试这个圆类的所有方法。
好的,以下是我的回答:
Circle类的UML图如下:
```
+----------------+
| Circle |
+----------------+
| - centerX: double |
| - centerY: double |
| - radius: double |
+----------------+
| + Circle() |
| + Circle(radius: double) |
| + getAreaO(): double |
| + getPerimeterO(): double |
| + getRadius(): double |
| + setRadius(radius: double): void |
+----------------+
```
下面是Circle类的完整代码:
```java
public class Circle {
private double centerX;
private double centerY;
private double radius;
public Circle() {
this(1.0);
}
public Circle(double radius) {
this.centerX = 0.0;
this.centerY = 0.0;
this.radius = radius;
}
public double getAreaO() {
return Math.PI * radius * radius;
}
public double getPerimeterO() {
return 2 * Math.PI * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
```
你可以使用以下代码来测试Circle类的所有方法:
```java
public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle();
Circle c2 = new Circle(2.0);
System.out.println("c1's radius is " + c1.getRadius());
System.out.println("c1's area is " + c1.getAreaO());
System.out.println("c1's perimeter is " + c1.getPerimeterO());
System.out.println("c2's radius is " + c2.getRadius());
System.out.println("c2's area is " + c2.getAreaO());
System.out.println("c2's perimeter is " + c2.getPerimeterO());
c1.setRadius(3.0);
System.out.println("c1's radius is now " + c1.getRadius());
System.out.println("c1's area is now " + c1.getAreaO());
System.out.println("c1's perimeter is now " + c1.getPerimeterO());
}
}
```
输出结果应该为:
```
c1's radius is 1.0
c1's area is 3.141592653589793
c1's perimeter is 6.283185307179586
c2's radius is 2.0
c2's area is 12.566370614359172
c2's perimeter is 12.566370614359172
c1's radius is now 3.0
c1's area is now 28.274333882308138
c1's perimeter is now 18.84955592153876
```
定义一个名为Circle的类,其中含有double型的成员变量centerX和centerY表示圆心坐标,radius表示圆的半径。定义求圆面积的方法getArea()方法和求圆周长的方法getPerimeter().为半径radius定义访问方法和修改方法。定义一个带参数构造方法,通过给出圆的半径创建对象。定义默认构造方法,在该方法中调用有参数构造方法,将圆的半径设置为1.0.画出该类的UML图。编写程序测试这个圆类的所有方法
以下是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
```
阅读全文