12345jsjsms 2022-03-19 15:29 采纳率: 100%
浏览 903
已结题

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

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

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

  • 写回答

3条回答 默认 最新

  • 关注
    
    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());
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月19日

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件