编写一个 Java 应用程序,该程序中有三个类: Triangle 、 Trapezoid 和 Circle ,分别用来刻画"三角形"、"梯形"和"圆形"。具体要求如下: Triangle 类具有类型为 double 的三个边,以及周长、面积属性, Triangle 类具有返回周长、面积以及修改三个边的功能。此外, Triangle 类还有一个 boolean 型的属性,该属性用来判断三个数能够构成一个三角形。完成上述操作后,给 Triangle 再增加3个方法,分别用来返回3个边 sideA , sideB , sideC 的值。
时间: 2024-02-23 08:00:38 浏览: 71
好的,以下是 Java 代码实现:
Triangle 类:
```
public class Triangle {
private double sideA;
private double sideB;
private double sideC;
private double perimeter;
private double area;
private boolean isTriangle;
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
this.isTriangle = (sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB + sideC > sideA);
if (isTriangle) {
this.perimeter = sideA + sideB + sideC;
double p = this.perimeter / 2;
this.area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
}
}
public boolean isTriangle() {
return isTriangle;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public double getSideA() {
return sideA;
}
public double getSideB() {
return sideB;
}
public double getSideC() {
return sideC;
}
}
```
Trapezoid 类:
```
public class Trapezoid {
private double top;
private double bottom;
private double height;
private double left;
private double right;
private double area;
public Trapezoid(double top, double bottom, double height, double left, double right) {
this.top = top;
this.bottom = bottom;
this.height = height;
this.left = left;
this.right = right;
this.area = (top + bottom) * height / 2;
}
public double getTop() {
return top;
}
public double getBottom() {
return bottom;
}
public double getHeight() {
return height;
}
public double getLeft() {
return left;
}
public double getRight() {
return right;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
```
Circle 类:
```
public class Circle {
private double radius;
private double area;
public Circle(double radius) {
this.radius = radius;
this.area = Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
```
以上是三个类的实现。其中 Triangle 类增加了返回三条边的方法 getSideA()、getSideB() 和 getSideC(),其他两个类分别计算并返回面积。
阅读全文