用java设计一个长方形类,成员变量包括长和宽.方法:计算面积和周长,有相应的set和get方法
答案:2 悬赏:80
解决时间 2021-03-21 14:49
- 提问者网友:沉默的哀伤
- 2021-03-20 16:00
用java设计一个长方形类,成员变量包括长和宽.方法:计算面积和周长,有相应的set和get方法
最佳答案
- 二级知识专家网友:旧脸谱
- 2021-03-20 17:14
class Rectangle{
private double width;
private double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
public double getArea(){
return this.width * this.height;
}
public double getGirth(){
return 2 * (this.width + this.height);
}
public double getWidth(){
return this.width;
}
public void setWidth(double width){
this.width = width;
}
public double getHeight(){
return this.height;
}
public void setHeight(double height){
this.height = height;
}
}
public class Test {
public static void main(String[] args){
Rectangle rect = new Rectangle(2,3);
System.out.println("长方形的宽 :" + rect.getWidth());
System.out.println("长方形的高 :" + rect.getHeight());
System.out.println("长方形的面积:" + rect.getArea());
System.out.println("长方形的周长:" + rect.getGirth());
}
}
全部回答
- 1楼网友:第幾種人
- 2021-03-20 17:48
public class Rectangle
{
private double length;
private double width;
public void setEdge()
{
this.length=3;
this.width=4;
}
public double getArea()
{
return(this.length*this.width);
}
public double getCircumference()
{
return((this.length+this.width)*2);
}
public static void main(String args[])
{
Rectangle b=new Rectangle();
b.setEdge();
System.out.println("the area is "+b.getArea());
System.out.println("the circumference is "+b.getCircumference());
}
}
{
private double length;
private double width;
public void setEdge()
{
this.length=3;
this.width=4;
}
public double getArea()
{
return(this.length*this.width);
}
public double getCircumference()
{
return((this.length+this.width)*2);
}
public static void main(String args[])
{
Rectangle b=new Rectangle();
b.setEdge();
System.out.println("the area is "+b.getArea());
System.out.println("the circumference is "+b.getCircumference());
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯