Java编写一个矩形类,并计算面积和周长?

Java编写一个矩形类Class Rectangle(要求矩形自带长1和宽2,且该类具有计算面积与周长的功能)。再编写一个主类,在主方法main中输出该矩形的周长和面积。求解答,谢谢!

class Rectangle{
private int width = 2;
private int length = 1;
public int getWidth(){

return this.width;
}
public void setWidth(int w){

this.width = w;
}
public int getLength(){

return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}

public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("长方形的面积是:");
System.out.println(rect.getArea());
System.out.printf("长方形的周长是:%d\n", rect.getCircumference());
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-04-18
class Rectangle{
private int width = 2;
private int length = 1;
public int getWidth(){

return this.width;
}
相似回答