编写一个java应用程序,该程序中有3个类:Trangle,Lader和Circle,分别用来刻画“三角形”、“梯形”和“圆形”。具体要求如下: Trangle类具有类型为double的三个边,Trangle类具有返回周长、面积以及修改三个边的方法。另外,Trangle类还具有一个boolean型的属性,该属性用来判断三个数能否构成一个三角形。 Lader类具有类型为double的上底、下底、高属性,具有返回面积的方法。 Circle类具有类型为double的半径属性,具有返回周长、面积的功能。
时间: 2023-06-18 22:07:34 浏览: 226
以下是符合要求的Java程序:
Trangle类:
```java
public class Trangle {
private double side1;
private double side2;
private double side3;
private boolean isValid;
public Trangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
// 判断三个数能否构成三角形
if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
this.isValid = true;
} else {
this.isValid = false;
}
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public boolean isValid() {
return isValid;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public double getArea() {
double p = getPerimeter() / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
}
```
Lader类:
```java
public class Lader {
private double upperBottom;
private double lowerBottom;
private double height;
public Lader(double upperBottom, double lowerBottom, double height) {
this.upperBottom = upperBottom;
this.lowerBottom = lowerBottom;
this.height = height;
}
public double getUpperBottom() {
return upperBottom;
}
public double getLowerBottom() {
return lowerBottom;
}
public double getHeight() {
return height;
}
public double getArea() {
return (upperBottom + lowerBottom) * height / 2;
}
}
```
Circle类:
```java
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
```
可以在主函数中进行测试:
```java
public static void main(String[] args) {
// 测试Trangle类
Trangle trangle = new Trangle(3, 4, 5);
System.out.println("Trangle perimeter: " + trangle.getPerimeter());
System.out.println("Trangle area: " + trangle.getArea());
// 修改边长
trangle.setSide1(5);
System.out.println("Trangle perimeter: " + trangle.getPerimeter());
System.out.println("Trangle area: " + trangle.getArea());
// 测试Lader类
Lader lader = new Lader(3, 5, 4);
System.out.println("Lader area: " + lader.getArea());
// 测试Circle类
Circle circle = new Circle(2);
System.out.println("Circle perimeter: " + circle.getPerimeter());
System.out.println("Circle area: " + circle.getArea());
}
```
阅读全文