(1)定义Rectangle类表示矩形类
(2)矩形类的属性包括:长、宽、颜色,在Rectangle类中定义相应的成员变量表示这些属性
(3)定义Rectangle类的构造方法,包括无参构造方法和有参构造方法
(4)在Rectangle类中定义以下方法:
① 分别定义设置长、宽、颜色的方法
② 分别定义获取长、宽、颜色的方法
③ 计算长方形周长的方法
④ 计算长方形面积的方法
(5)创建2个长方形类的对象,第一个长方形长为5,宽为2,颜色为red。第二个长方形长为10,宽为5,颜色为blue。输出这2个长方形的长、宽、高、颜色、面积、周长等信息。
(6)将第二个长方形的长设置为12,宽设置为6,颜色设置为yellow,并输出修改后的信息。
多看多了解,自己动手做出来才会印象深刻
public class Rectangle {
private double length;
private double width;
private String color;
// 无参构造方法
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
this.color = "";
}
// 有参构造方法
public Rectangle(double length, double width, String color) {
this.length = length;
this.width = width;
this.color = color;
}
// 设置长
public void setLength(double length) {
this.length = length;
}
// 获取长
public double getLength() {
return this.length;
}
// 设置宽
public void setWidth(double width) {
this.width = width;
}
// 获取宽
public double getWidth() {
return this.width;
}
// 设置颜色
public void setColor(String color) {
this.color = color;
}
// 获取颜色
public String getColor() {
return this.color;
}
// 计算周长
public double getPerimeter() {
return 2 * (this.length + this.width);
}
// 计算面积
public double getArea() {
return this.length * this.width;
}
}
public class Main {
public static void main(String[] args) {
// 创建两个长方形对象
Rectangle rectangle1 = new Rectangle(5.0, 2.0, "red");
Rectangle rectangle2 = new Rectangle(10.0, 5.0, "blue");
// 输出长方形的属性信息
System.out.println("rectangle1: length = " + rectangle1.getLength()
+ ", width = " + rectangle1.getWidth() + ", color = " + rectangle1.getColor()
+ ", area = " + rectangle1.getArea() + ", perimeter = " + rectangle1.getPerimeter());
System.out.println("rectangle2: length = " + rectangle2.getLength()
+ ", width = " + rectangle2.getWidth() + ", color = " + rectangle2.getColor()
+ ", area = " + rectangle2.getArea() + ", perimeter = " + rectangle2.getPerimeter());
// 修改第二个长方形的属性
rectangle2.setLength(12.0);
rectangle2.setWidth(6.0);
rectangle2.setColor("yellow");
// 输出修改后的长方形的属性信息
System.out.println("After modification:");
System.out.println("rectangle2: length = " + rectangle2.getLength()
+ ", width = " + rectangle2.getWidth() + ", color = " + rectangle2.getColor()
+ ", area = " + rectangle2.getArea() + ", perimeter = " + rectangle2.getPerimeter());
}
}
public class CSDNQ7920441{
public static void main(String[] args){
Rectangle r1=new Rectangle(5,2,"red");
Rectangle r2=new Rectangle(10,5,"blue");
System.out.println("第一个长方形的长:"+r1.gL()+",宽:"+r1.gW()+",颜色:"+r1.gC()+",面积:"+r1.aR()+",周长:"+r1.pR());
System.out.println("第二个长方形的长:"+r2.gL()+",宽:"+r2.gW()+",颜色:"+r2.gC()+",面积:"+r2.aR()+",周长:"+r2.pR());
r2.sL(12);
r2.sW(6);
r2.sC("yellow");
System.out.println("修改后的第二个长方形的长:"+r2.gL()+",宽:"+r2.gW()+",颜色:"+r2.gC()+",面积:"+r2.aR()+",周长:"+r2.pR());
}
}
class Rectangle{
private int l,w;
private String c;
public Rectangle(){}
public Rectangle(int l,int w,String c){
this.l=l;
this.w=w;
this.c=c;
}
public void sL(int l){this.l=l;}
public void sW(int w){this.w=w;}
public void sC(String c){this.c=c;}
public int gL(){return l;}
public int gW(){return w;}
public String gC(){return c;}
public int pR(){return 2*(l+w);}
public int aR(){return l*w;}
}
```
不知道你这个问题是否已经解决, 如果还没有解决的话: