创建名为CompareableRectangle的类

[复制链接]
查看11 | 回复1 | 2010-6-13 23:51:44 | 显示全部楼层 |阅读模式
创建名为CompareableRectangle的类,它扩展Rectangle类并实现Compareable接口。画出UML图并实现compareTo方法,使其根据面积比较两个矩形。编写测试程序找出两个CompareableRectangle对象的实例中的较大者。
Rectangle类如下:
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {

this.width = width;

this.height = height;
}
/** Return width */
public double getWidth() {

return width;
}
/** Set a new width */
public void setWidth(double width) {

this.width = width;
}
/** Return height */
public double getHeight() {

return height;
}
/** Set a new height */
public void setHeight(double height) {

this.height = height;
}
/** Return area */
public double getArea() {

return width * height;
}
/** Return perimeter */
public double getPerimeter() {

return 2 * (width + height);
}
}

回复

使用道具 举报

千问 | 2010-6-13 23:51:44 | 显示全部楼层
class CompareableRectangle extends Rectangle implements Comparable{public CompareableRectangle() { super();}public CompareableRectangle(double width, double height) { super(width, height);} public int compareTo(Rectangle o){return getArea() - o.getArea();; }}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行