定义一个矩形类Rectangle、梯形类Ladder、测试类AreaTest,这三个类处于同一个包com.test中,AreaTest类为主类,在main()方法中创建矩形类和梯形类的相应对象,调用其成员函数计算矩形、梯形的面积
时间: 2024-05-12 22:20:38 浏览: 138
Rectangle类:
```
package com.test;
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea() {
return length * width;
}
}
```
Ladder类:
```
package com.test;
public class Ladder {
private double upperWidth;
private double lowerWidth;
private double height;
public Ladder(double upperWidth, double lowerWidth, double height) {
this.upperWidth = upperWidth;
this.lowerWidth = lowerWidth;
this.height = height;
}
public double getUpperWidth() {
return upperWidth;
}
public void setUpperWidth(double upperWidth) {
this.upperWidth = upperWidth;
}
public double getLowerWidth() {
return lowerWidth;
}
public void setLowerWidth(double lowerWidth) {
this.lowerWidth = lowerWidth;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return (upperWidth + lowerWidth) * height / 2;
}
}
```
AreaTest类:
```
package com.test;
public class AreaTest {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 10);
Ladder ladder = new Ladder(5, 10, 8);
System.out.println("矩形的面积为:" + rectangle.getArea());
System.out.println("梯形的面积为:" + ladder.getArea());
}
}
```
阅读全文