62,615
社区成员
发帖
与我相关
我的任务
分享
public class Rectangle {
// constructor
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 getPerimeter() {
return 2 * length + 2 * width;
}
//求面积
public double getArea() {
return length * width;
}
private double length; //长
private double width; //宽
}
public class Rectangle {
// constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
//第一种方法,通过setLength求的:
public double getLengthByAPerimeter(double perimeter) {
this.setLength((perimeter - 2 * this.width) / 2);
return this.length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getPerimeter() {
return 2 * length + 2 * width;
}
public double getArea() {
return length * width;
}
//第二种方法,通过静态方法求的:
public static double getLengthByPerimeterAndWidth(
double perimeter, double width) {
return (perimeter - 2 * width) / 2;
}
private double length;
private double width;
}