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

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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据