作业帮 > 综合 > 作业

JAVA:定义矩形Rectangle

来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:综合作业 时间:2024/08/03 20:18:53
JAVA:定义矩形Rectangle
定义矩形Rectangle ,矩形信息包括宽 高 提供含两个参数的构造方法,为字段提供get和set方法.提供计算面积的calcArea方法,提供计算周长calcCircum方法,重写equals方法.面积相等则相等.
JAVA:定义矩形Rectangle
public class Rectangle {

private float length;
private float width;

public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}

public float calcArea() {
return this.length * this.width;
}

public float calcCircum() {
return (this.length + this.width) * 2;
}

@Override
public int hashCode() {
return new Float(this.length * this.width).hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof Rectangle)) {
return false;
}
Rectangle rectangle = (Rectangle) obj;
if (this.calcArea() == rectangle.calcArea()) {
return true;
}
return false;
}

public float getLength() {
return length;
}

public void setLength(float length) {
this.length = length;
}

public float getWidth() {
return width;
}

public void setWidth(float width) {
this.width = width;
}
}