java写一个名为Rectangle的类表示矩形。

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

java写一个名为Rectangle的类表示矩形。。 其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:  (1) 使用构造函数完成各属性的初始赋值。  (2) 使用get()和set()的形式完成属性的访问及修改。  (3) 提供计算面积的getArea()方法。 (4)创建实例并测试 


public class Rectangle {
    private double height;
    private double width;
    private String color;

    public Rectangle() {

    }
    public Rectangle(double height,double width,String color) {
        this.color = color;
        this.height = height;
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    public double getArea() {
        return width*height;
    }

    public static void main(String[] args) {
        Rectangle rect = new Rectangle(100,100,"red");
        System.out.println("area=" + rect.getArea());
    }
}


 public class Rectangle {     
        private double width;
        private double height;
        private String color;
        Rectangle(){
             this.width=10;
              this.height=10;
              this.color="red";
        }
        public void setWidth(double width) {
            this.width=width;
        }
        public void setcolor(String color) {
            this.color=color;
        }
        public void setHeight(double height) {
            this.height=height;
        }
        public String getcolor() {
            return color;
        }
        public double getWidth() {
            return width;
        }
        public double getHeight() {
            return height;
        }
        public  double getArea() {          //计算面积
            return width*height;
        }

            public static void main(String args[]) {
                Rectangle r=new Rectangle();
                r.setWidth(29);
                r.setHeight(30);
                r.setcolor("blue");
                System.out.println("高 ="+r.getHeight());
                System.out.println("宽 ="+r.getWidth());
                System.out.println("颜色为:"+r.getcolor());
                System.out.println("面积为:"+r.getArea());

        }
        
    }

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632