请在 下方输入 要搜索的题目:

类的定义写一个名为Rectangle的类表示矩形。属性为宽width、高height和颜色color,widthhe height是double型的,color是String型的。假定所有矩形颜色相同,用一个类变量(类属性)表示颜色。要求提供访问器方法和计算矩形面积的findArea()方法。写一个用户程序测试Rectangle类:创建两个Rectangle对象。给两个对象设置任意的宽和高。设矩形颜色为黄色。显示两个对象的属性并求面积。

类的定义写一个名为Rectangle的类表示矩形。属性为宽width、高height和颜色color,widthhe height是double型的,color是String型的。假定所有矩形颜色相同,用一个类变量(类属性)表示颜色。要求提供访问器方法和计算矩形面积的findArea()方法。写一个用户程序测试Rectangle类:创建两个Rectangle对象。给两个对象设置任意的宽和高。设矩形颜色为黄色。显示两个对象的属性并求面积。

发布时间:2025-03-07 05:05:06
推荐参考答案 ( 由 专技宝 官方老师解答 )
答案:public class Rectangle{private double width = 1;private double height = 1;private static String color = “white”;public Rectangle(){}public Rectangle(double width, double height , String color){this.width = width;this.height = height;this.color = color;}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 static String getColor(){return color;}public static void setColor(String color){this.color = color;}public double findArea(){return width*height;}public static void main(String[] args){Rectangle r1 = new Rectangle();Rectangle r2 = new Rectangle();r1.setWidth(2.0);r1.setHeight(3.0);r1.setColor(“红色”);r2.setWidth(5.0);r2.setHeight(4.0);r2.setColor(“黄色”);System.out.println(r1.getWidth());System.out.println(r1.getHeight());System.out.println(r1.getColor());System.out.println(r1.findArea());System.out.println(r2.getWidth());System.out.println(r2.getHeight());System.out.println(r2.getColor());System.out.println(r2.findArea());}}
专业技术学习
专业技术学习