定义一个名为Triangle的三角形类,使其继承Shape抽象类,覆盖Shape类中的抽象方法perimeter()和area()。编写程序测试Triangle类的使用。
时间: 2023-06-06 13:04:47 浏览: 178
软件测试 三角形的测试
好的,这个问题属于编程问题,可以为您提供答案。下面是定义Triangle类的代码示例:
```
abstract class Shape {
public abstract double perimeter();
public abstract double area();
}
class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double perimeter() {
return a + b + c;
}
@Override
public double area() {
double p = perimeter() / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}
public class TestTriangle {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("The perimeter of the triangle is: " + triangle.perimeter());
System.out.println("The area of the triangle is: " + triangle.area());
}
}
```
您可以使用上述代码进行测试。
阅读全文