请给我Triangle类
时间: 2024-02-12 11:05:55 浏览: 18
好的,以下是Triangle类的代码:
```java
public class Triangle {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public String getType() {
if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
return "Invalid";
} else if (side1 + side2 <= side3 || side2 + side3 <= side1 || side1 + side3 <= side2) {
return "Invalid";
} else if (side1 == side2 && side2 == side3) {
return "Equilateral";
} else if (side1 == side2 || side1 == side3 || side2 == side3) {
return "Isosceles";
} else {
return "Scalene";
}
}
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
}
```
Triangle类有三个私有属性,分别是三边的长度。构造方法用于初始化这三个属性。getType()方法用于返回三角形的类型,getArea()方法用于返回三角形的面积。注意,在getType()方法中,我们先判断三边长度是否合法,然后再根据三边长度判断三角形的类型。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)