编写一个 Java 应用程序,该程序中有三个类: Triangle 、 Trapezoid 和 Circle 分别用来刻画"三角形"、"梯形"和"圆形"。具体要求如下: Triangle 类具有类型为 double 的三个边,以及周长、面积属性, Triangle 类具有返回周长、面积以及修改三个边的功能。此外, Triangle 类还有一个 boolean 型的属性,该属性用来判断三个数能够构成一个三角形。完成上述操作后,给 Triangle 再增加3个方法,分别用来返回3个边 sideA , sideB , sideC 的值。Lander 类具有类型为 double 的上底、下底、高、面积属性,具有返回面积的功能。 Circle 类具有类型为 double 的半径、周长和面积属性,具有返回周长、面积的功能。 编写一个测试类 Test ,分别创建类 Triangle 、 Trapezoid 和 Circle 的对象,并计算三角形、梯形和圆形的周长和面积。
时间: 2024-02-23 14:00:48 浏览: 83
好的,以下是 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;
}
public void setSideA(double sideA) {
this.sideA = sideA;
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 void setSideB(double sideB) {
this.sideB = sideB;
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 void setSideC(double sideC) {
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));
}
}
}
```
Trapezoid 类:
```
public class Trapezoid {
private double top;
private double bottom;
private double height;
private double area;
public Trapezoid(double top, double bottom, double height) {
this.top = top;
this.bottom = bottom;
this.height = height;
this.area = (top + bottom) * height / 2;
}
public double getTop() {
return top;
}
public void setTop(double top) {
this.top = top;
this.area = (top + bottom) * height / 2;
}
public double getBottom() {
return bottom;
}
public void setBottom(double bottom) {
this.bottom = bottom;
this.area = (top + bottom) * height / 2;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
this.area = (top + bottom) * height / 2;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
```
Circle 类:
```
public class Circle {
private double radius;
private double circumference;
private double area;
public Circle(double radius) {
this.radius = radius;
this.circumference = 2 * Math.PI * radius;
this.area = Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
this.circumference = 2 * Math.PI * radius;
this.area = Math.PI * radius * radius;
}
public double getCircumference() {
return circumference;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
```
Test 类:
```
public class Test {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
if (triangle.isTriangle()) {
System.out.println("Triangle perimeter: " + triangle.getPerimeter());
System.out.println("Triangle area: " + triangle.getArea());
} else {
System.out.println("Not a triangle.");
}
Trapezoid trapezoid = new Trapezoid(3, 5, 4);
System.out.println("Trapezoid area: " + trapezoid.getArea());
Circle circle = new Circle(2.5);
System.out.println("Circle circumference: " + circle.getCircumference());
System.out.println("Circle area: " + circle.getArea());
}
}
```
以上是三个类和测试类的实现。在测试类中分别创建 Triangle 、 Trapezoid 和 Circle 的对象,并计算周长和面积。其中 Triangle 类中增加了修改三个边的方法 setSideA()、setSideB() 和 setSideC(),其他两个类中增加了修改面积的方法 setArea()。
阅读全文