通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用 矩形的左上角坐标来标识,包含: 添加两个属性:矩形左上角坐标startX和startY。 两个构造方法: 带4个参数的构造方法,用于对startX、startY、width和height属性 初始化; 不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0 的矩形; 添加一个方法: 判断某个点是否在矩形内部的方法

package b;

public class Rect {
    Double width;
    Double height;
    public Double getWidth() {
        return width;
    }
    public void setWidth(Double width) {
        this.width = width;
    }
    public Double getHeight() {
        return height;
    }
    public void setHeight(Double height) {
        this.height = height;
    }
    public Rect(Double width, Double height) {
        super();
        this.width = width;
        this.height = height;
    }
    Rect()
    {
        width=10.0;
        height=10.0;    
    }
    
    public void area()
    {
        if(width<0||height<0)
        {
            System.out.println("数据错误");
        }
        else
        System.out.println("矩形的面积为 width*height="+(width*height));
    }
    public void perimeter()
    {
        if(width<0||height<0)
        {
            System.out.println("数据错误");
        }
        else
        System.out.println("矩形的周长为 2*(width+height)="+(2*(width+height)));
    }
}
public class PlainRect extends Rect {
    private double startX;
    private double startY;
    public double getStartX() {
        return startX;
    }
    public void setStartX(double startX) {
        this.startX = startX;
    }
    public double getStartY() {
        return startY;
    }
    public void setStartY(double startY) {
        this.startY = startY;
    }
    public PlainRect(Double width, Double height, double startX, double startY) {
        super(width, height);
        this.startX = startX;
        this.startY = startY;
    }
    PlainRect()
    {
        width=0.0;
        height=0.0;
        startX=0.0;
        startY=0.0;                
    }
    boolean isInside(double x,double y)
    {
        if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height))
        {
            
            System.out.println("("+x+","+y+")在矩形内");
            return true;
        }
        else
        {
            System.out.println("("+x+","+y+")不在矩形内");
            return false;
        }
    }
    

}
package b;

public class TestRect {

    public static void main(String[] args) {
        PlainRect juxing=new PlainRect();
        juxing.setWidth(10.0);
        juxing.setHeight(20.0);
        juxing.area();
        juxing.perimeter();
        juxing.setStartX(10);
        juxing.setStartY(10);
        juxing.isInside(25.5, 13);
        

    }

}

posted @ 2016-05-23 15:17  凌零聆  阅读(2861)  评论(0编辑  收藏  举报