用java1.编写一个圆类,圆类有半径Radius和圆心心center等属性,这些属性可以查看get 也可以重新设置set,另外还可以统计圆的个数,返回其面积及周长。 2.编写一个圆环类 Ring。圆环类有内半径innerRadius,外半径outerRadius和圆心center等属性,这些属性可以查看get 也可以重新设置set,另外,圆环还可以返回其面积及外周长。 3.创建圆环类的对象,并输出对象的面积和周长;创建多个圆,输出总的个数,每个圆的面积周长等。
时间: 2023-05-19 08:06:31 浏览: 104
1. 圆类的代码如下:
```
public class Circle {
private double radius;
private Point center;
private static int count = 0;
public Circle(double radius, Point center) {
this.radius = radius;
this.center = center;
count++;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public static int getCount() {
return count;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
```
2. 圆环类的代码如下:
```
public class Ring {
private double innerRadius;
private double outerRadius;
private Point center;
public Ring(double innerRadius, double outerRadius, Point center) {
this.innerRadius = innerRadius;
this.outerRadius = outerRadius;
this.center = center;
}
public double getInnerRadius() {
return innerRadius;
}
public void setInnerRadius(double innerRadius) {
this.innerRadius = innerRadius;
}
public double getOuterRadius() {
return outerRadius;
}
public void setOuterRadius(double outerRadius) {
this.outerRadius = outerRadius;
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public double getArea() {
return Math.PI * (outerRadius * outerRadius - innerRadius * innerRadius);
}
public double getPerimeter() {
return 2 * Math.PI * (outerRadius + innerRadius);
}
}
```
3. 创建圆环类的对象并输出面积和周长的代码如下:
```
Ring ring = new Ring(3, 5, new Point(0, 0));
System.out.println("Ring area: " + ring.getArea());
System.out.println("Ring perimeter: " + ring.getPerimeter());
```
创建多个圆并输出总个数、每个圆的面积和周长的代码如下:
```
Circle circle1 = new Circle(2, new Point(0, 0));
Circle circle2 = new Circle(3, new Point(1, 1));
Circle circle3 = new Circle(4, new Point(2, 2));
System.out.println("Total number of circles: " + Circle.getCount());
System.out.println("Circle 1 area: " + circle1.getArea());
System.out.println("Circle 1 perimeter: " + circle1.getPerimeter());
System.out.println("Circle 2 area: " + circle2.getArea());
System.out.println("Circle 2 perimeter: " + circle2.getPerimeter());
System.out.println("Circle 3 area: " + circle3.getArea());
System.out.println("Circle 3 perimeter: " + circle3.getPerimeter());
```
阅读全文