JAVA设计三角形类,继承图形抽象类,计算三角形面积和周长
时间: 2024-02-15 17:52:44 浏览: 81
计算三角形的面积和周长(java)
5星 · 资源好评率100%
public class Triangle extends Shape {
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;
}
@Override
public double getArea() {
double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
}
阅读全文