请用java创建一个Shape公共类,该类具有length和height两个属性,具有带参数和不带参数的构造方法和每个属性的get和set方法,编写通过两个属性计算该图形面积和周长的方法。并创建一个Test测试类,创建Shape类的对象,并计算相应对象的面积和周长。
时间: 2024-05-01 13:24:13 浏览: 42
好的,下面是Java代码实现。
Shape类:
```java
public class Shape {
private double length;
private double height;
public Shape() {}
public Shape(double length, double height) {
this.length = length;
this.height = height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return length * height;
}
public double getPerimeter() {
return 2 * (length + height);
}
}
```
Test类:
```java
public class Test {
public static void main(String[] args) {
Shape shape = new Shape(2.5, 3.7);
System.out.println("面积:" + shape.getArea());
System.out.println("周长:" + shape.getPerimeter());
}
}
```
输出结果:
```
面积:9.25
周长:12.4
```
阅读全文