Java编写一个三角形类,能根据三个实数构造三角形对象,如果三个实数不满足三角形的条件,则自动构造以最小值为边的等边三角形。输入任意三个数,求构造的三角形面积
时间: 2024-05-09 21:21:26 浏览: 96
可以回答这个问题。以下是Java代码:
```
public class Triangle {
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
if (a + b <= c || a + c <= b || b + c <= a) {
this.a = this.b = this.c = Math.min(Math.min(a, b), c);
} else {
this.a = a;
this.b = b;
this.c = c;
}
}
public double getArea() {
double p = (a + b + c) / 2.0;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
```
使用以下代码调用:
```
double a = 3;
double b = 4;
double c = 5;
Triangle triangle = new Triangle(a, b, c);
double area = triangle.getArea();
System.out.println(area);
```
输出结果为:6.0
阅读全文