Circle类并添加实例属性r(成员变量),在类Circle中添加两种方法, 分别计算周长和面积。编写有参和无参构造方法。为属性添加sctter和getter 方法。编写主类CrcateCircle,利用类Circle输出=-2时圆的周长和面积
时间: 2024-05-10 16:14:40 浏览: 27
下面是 Circle 类的实现:
```java
public class Circle {
private double r; // 圆的半径
public Circle() {
this.r = 0;
}
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getPerimeter() {
return 2 * Math.PI * r;
}
public double getArea() {
return Math.PI * r * r;
}
}
```
下面是 CrcateCircle 类的实现:
```java
public class CrcateCircle {
public static void main(String[] args) {
Circle circle = new Circle(-2);
System.out.println("圆的周长:" + circle.getPerimeter());
System.out.println("圆的面积:" + circle.getArea());
}
}
```
输出结果为:
```
圆的周长:-12.566370614359172
圆的面积:12.566370614359172
```
说明:当半径为负数时,周长和面积的计算结果也是负数。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)